summaryrefslogtreecommitdiff
path: root/test/partitiontest/filtering.go
blob: de738ed71d9b8da3e8c5721839068d4a219b7df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package partitiontest

import (
	"hash/fnv"
	"os"
	"runtime"
	"strconv"
	"testing"
)

// PartitionTest checks if the current partition should run this test, and skips it if not.
func PartitionTest(t *testing.T) {
	pt, found := os.LookupEnv("PARTITION_TOTAL")
	if !found {
		return
	}
	partitions, err := strconv.Atoi(pt)
	if err != nil {
		return
	}
	pid := os.Getenv("PARTITION_ID")
	partitionID, err := strconv.Atoi(pid)
	if err != nil {
		return
	}
	name := t.Name()
	_, file, _, _ := runtime.Caller(1) // get filename of caller to PartitionTest
	nameNumber := stringToUint64(file + ":" + name)
	idx := nameNumber % uint64(partitions)
	if idx != uint64(partitionID) {
		t.Skipf("skipping %s due to partitioning: assigned to %d but I am %d of %d", name, idx, partitionID, partitions)
	}
}

func stringToUint64(str string) uint64 {
	h := fnv.New64a()
	h.Write([]byte(str))
	return h.Sum64()
}