-
Notifications
You must be signed in to change notification settings - Fork 4
/
threads_test.go
63 lines (59 loc) · 2.13 KB
/
threads_test.go
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
56
57
58
59
60
61
62
63
package dads
import (
"os"
"testing"
)
func TestGetThreadsNum(t *testing.T) {
// Environment context parse
var ctx Ctx
FatalOnError(os.Setenv("DA_DS", "ds"))
ctx.Init()
// Get actual number of threads available
nThreads := GetThreadsNum(&ctx)
ResetThreadsNum(&ctx)
// Set context's ST/NCPUs manually (don't need to repeat tests from context_test.go)
var testCases = []struct {
ST bool
NCPUs int
NCPUsScale float64
expected int
}{
{ST: false, NCPUs: 0, NCPUsScale: 1.0, expected: nThreads},
{ST: false, NCPUs: 1, NCPUsScale: 1.0, expected: 1},
{ST: false, NCPUs: -1, NCPUsScale: 1.0, expected: nThreads},
{ST: false, NCPUs: 2, NCPUsScale: 1.0, expected: 2},
{ST: true, NCPUs: 0, NCPUsScale: 1.0, expected: 1},
{ST: true, NCPUs: 1, NCPUsScale: 1.0, expected: 1},
{ST: true, NCPUs: -1, NCPUsScale: 1.0, expected: 1},
{ST: true, NCPUs: 2, NCPUsScale: 1.0, expected: 1},
{ST: false, NCPUs: 2, NCPUsScale: 1.0, expected: 2},
{ST: false, NCPUs: nThreads + 1, NCPUsScale: 1.0, expected: nThreads},
{ST: true, NCPUs: nThreads + 1, NCPUsScale: 1.0, expected: 1},
{ST: false, NCPUs: 0, NCPUsScale: 2.0, expected: nThreads * 2},
{ST: false, NCPUs: 1, NCPUsScale: 2.0, expected: 1},
{ST: false, NCPUs: -1, NCPUsScale: 2.0, expected: nThreads * 2},
{ST: false, NCPUs: 2, NCPUsScale: 2.0, expected: 2},
{ST: true, NCPUs: 0, NCPUsScale: 2.0, expected: 1},
{ST: true, NCPUs: 1, NCPUsScale: 2.0, expected: 1},
{ST: true, NCPUs: -1, NCPUsScale: 2.0, expected: 1},
{ST: false, NCPUs: 2, NCPUsScale: 2.0, expected: 2},
{ST: false, NCPUs: nThreads + 1, NCPUsScale: 2.0, expected: nThreads + 1},
{ST: true, NCPUs: 2, NCPUsScale: 2.0, expected: 1},
{ST: true, NCPUs: nThreads + 1, NCPUsScale: 2.0, expected: 1},
}
// Execute test cases
for index, test := range testCases {
ctx.ST = test.ST
ctx.NCPUs = test.NCPUs
ctx.NCPUsScale = test.NCPUsScale
expected := test.expected
got := GetThreadsNum(&ctx)
if got != expected {
t.Errorf(
"test number %d, expected to return %d threads, got %d (default is %d on this machine)",
index+1, expected, got, nThreads,
)
}
ResetThreadsNum(&ctx)
}
}