#!/usr/bin/env bash

# Exits as soon as any line fails.
set -euo pipefail

source ci/scripts/common.sh

export RUST_LOG="info,risingwave_simulation=debug"
export LOGDIR=.risingwave/log
mkdir -p $LOGDIR

echo "--- Download artifacts"
buildkite-agent artifact download simulation-it-test.tar.zst .

# Manually extract instead of using `cargo nextest run --archive-file` to ensure `cargo llvm-cov`
# can find the object files later.
echo "--- Extract artifacts"
tar -xvf simulation-it-test.tar.zst
mkdir target/sim
mv target/ci-sim target/sim

if [[ -z "${1:-}" ]]; then
  TEST_PATTERN=""
elif [[ "$1" == "pull-request" ]]; then
  # exclude logstore tests
  TEST_PATTERN="-E 'not test(/^log_store/)'"
else
  TEST_PATTERN="-E 'test(/^$1/)'"
fi

# NOTE(kwannoel): for TEST_PATTERN, please consult: https://nexte.st/docs/filtersets/reference/.
# Using substring matching may run unexpected tests.
# For example, `scale::` will run all tests with a `scale` module prefix.
# This includes sink::scale::*, scale::*.
# We just want to run `scale::*`.

# `cargo-nextest` will spawn a process for each test. Avoid including `%p` in the file name pattern
# to avoid creating too many profraw files. Instead, use `%m` to reuse the profraw files for the
# same test binary. Here `%4m` means that there's a pool of 4 profraw files for each test binary.
# This achieves a good balance between the performance of running tests and generating reports.
# https://github.com/taiki-e/cargo-llvm-cov/issues/335#issuecomment-1890349373
export CARGO_LLVM_COV_TARGET_DIR="${REPO_ROOT}/target/sim"
export LLVM_PROFILE_FILE="${CARGO_LLVM_COV_TARGET_DIR}/risingwave-madsim-it-test-%4m.profraw"
export RW_COVERAGE_CARGO_PROFILE="ci-sim"

# Pre-install the pinned toolchain serially before the parallel fan-out below.
# `parallel -j 8` spawns 8 `cargo` processes at once; if the toolchain isn't
# already present in the CI image (e.g. right after a toolchain bump, before the
# image is rebuilt), each process triggers a concurrent `rustup` auto-install and
# they race on ~/.rustup/downloads, failing with
# "could not rename downloaded file ... No such file or directory".
# Running cargo once up front forces a single serial install. No-op once the
# image already bakes the pinned toolchain.
cargo --version

echo "--- Run integration tests in deterministic simulation mode"
seq "$TEST_NUM" | parallel -j 8 --line-buffer "MADSIM_TEST_SEED={} NEXTEST_PROFILE=ci-sim \
 cargo nextest run \
 $NEXTEST_PARTITION_ARG \
 --no-fail-fast \
 --cargo-metadata target/nextest/cargo-metadata.json \
 --binaries-metadata target/nextest/binaries-metadata.json \
 $TEST_PATTERN \
 2> $LOGDIR/it-test-{}.log && rm $LOGDIR/it-test-{}.log"
