【发布时间】:2021-12-01 11:58:27
【问题描述】:
我们的 CI 中有一个强大的服务器,我们希望利用它并在同一台机器上并行化我们的 cypress 测试套件。我们知道cypress doesn't encourage it,但这应该是可能的!
我们有一个 bash 脚本,它将所有测试文件拆分到 n 组中,并在后台的新端口上的每个组上运行 cypress:
npx cypress run --spec $specFiles --port $port --headless &
原则上,这应该可以工作,因为每个进程都将在单独的无头浏览器上运行其文件。但是,如果有超过 4 个工人,我们会遇到各种错误:
我们正在尽最大努力避免将每个 cypress 实例作为新的 docker 容器运行,以避免额外的 CI 复杂性,但如有必要,我们会深入研究。我们是否遗漏了一些明显的东西?
这是完整的脚本供参考:
#!/bin/bash
nThreads=3
print_usage() {
printf "Usage:
./run_tests_parallel.sh -n <number of threads to use>
Defaults to $nThreads threads\n"
exit 0;
}
while true; do
case "$1" in
-n | --threads ) nThreads=$2; shift 2 ;;
-h | --help ) print_usage ; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo Using $nThreads threads
# Return non-zero if any of the subprocesses
# returns non-zero
set -eu
testFiles=`find . -name "*.test.ts" -not -path "./node_modules/*"`
# init testF
testFilesPerThread=()
for (( n=0; n<$nThreads; n++ )); do
testFilesPerThread+=("")
done
i=0
for testFile in $testFiles; do
testFilesPerThread[$i]="${testFilesPerThread[$i]} $testFile"
i=$((($i + 1)%$nThreads))
done
pids=()
for (( i=0; i<${#testFilesPerThread[@]}; i++ )); do
echo Thread $i has files: ${testFilesPerThread[$i]}
# strip string and join files with ","
specFiles=`echo ${testFilesPerThread[$i]} | xargs | tr -s "\ " ","`
port=$((30001+$i))
# run tests in background
npx cypress run --spec $specFiles --port $port --headless &
pids+=($!)
echo "Spawned PID ${pids[${#pids[@]}-1]} for thread $i on port $port"
done
for pid in ${pids[@]} ; do
echo "Waiting for PID $pid."
wait $pid
done
echo DONE.
【问题讨论】:
标签: bash parallel-processing frontend cypress race-condition