From 0e0f74b7e3ba30e5f428d16b6ca44f117adde319 Mon Sep 17 00:00:00 2001 From: James Zern Date: Wed, 27 Oct 2021 13:05:03 -0700 Subject: [PATCH] infra/common.sh: add shard_should_run() Detects whether test block should be run in the current test shard. if shard_should_run; then ... fi Bug: b/185520507 Change-Id: I408d226651630a01c5e447dbff7d26b73426ce42 --- infra/common.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/infra/common.sh b/infra/common.sh index b3686e6c..38c82c84 100644 --- a/infra/common.sh +++ b/infra/common.sh @@ -71,3 +71,30 @@ setup_ccache() { export PATH="/usr/lib/ccache:${PATH}" fi } + +####################################### +# Detects whether test block should be run in the current test shard. +# Globals: +# TEST_TOTAL_SHARDS: Valid range: [1, N]. Defaults to 1. +# TEST_SHARD_INDEX: Valid range: [0, TEST_TOTAL_SHARDS). Defaults to 0. +# libwebp_test_id: current test number; incremented with each call. +# Arguments: +# None +# Returns: +# true if the shard is active +# false if the shard is inactive +####################################### +shard_should_run() { + TEST_TOTAL_SHARDS=${TEST_TOTAL_SHARDS:=1} + TEST_SHARD_INDEX=${TEST_SHARD_INDEX:=0} + libwebp_test_id=${libwebp_test_id:=-1} + : $((libwebp_test_id += 1)) + + if [[ "${TEST_SHARD_INDEX}" -lt 0 || + "${TEST_SHARD_INDEX}" -ge "${TEST_TOTAL_SHARDS}" ]]; then + log_err "Invalid TEST_SHARD_INDEX (${TEST_SHARD_INDEX})!" \ + "Expected [0, ${TEST_TOTAL_SHARDS})." + fi + + [[ "$((libwebp_test_id % TEST_TOTAL_SHARDS))" -eq "${TEST_SHARD_INDEX}" ]] +}