-
Notifications
You must be signed in to change notification settings - Fork 10
/
numcpus_test.go
128 lines (106 loc) · 3.36 KB
/
numcpus_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2019-2020 Tobias Klauser
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package numcpus_test
import (
"bytes"
"errors"
"os/exec"
"runtime"
"strconv"
"strings"
"testing"
"github.com/tklauser/numcpus"
)
func testGetconf(t *testing.T, got int, name, getconfWhich string) {
t.Helper()
getconf, err := exec.LookPath("getconf")
if err != nil {
t.Skipf("getconf not found in PATH: %v", err)
}
cmd := exec.Command(getconf, getconfWhich)
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
if err := cmd.Run(); err != nil {
t.Skipf("failed to run %v: %v (%v)", cmd, err, strings.TrimSpace(errb.String()))
}
want, err := strconv.ParseInt(strings.TrimSpace(outb.String()), 10, 64)
if err != nil {
t.Skipf("failed to parse getconf output: %v", err)
}
if got != int(want) {
t.Errorf("%s returned %v, want %v (getconf %s)", name, got, want, getconfWhich)
}
}
func confName(name string) string {
switch runtime.GOOS {
case "illumos", "netbsd", "openbsd", "solaris":
return strings.TrimPrefix(name, "_")
}
return name
}
func TestGetConfigured(t *testing.T) {
n, err := numcpus.GetConfigured()
if errors.Is(err, numcpus.ErrNotSupported) {
t.Skipf("GetConfigured not supported on %s", runtime.GOOS)
} else if err != nil {
t.Fatalf("GetConfigured: %v", err)
}
t.Logf("Configured = %v", n)
testGetconf(t, n, "GetConfigured", confName("_NPROCESSORS_CONF"))
}
func TestGetKernelMax(t *testing.T) {
n, err := numcpus.GetKernelMax()
if errors.Is(err, numcpus.ErrNotSupported) {
t.Skipf("GetKernelMax not supported on %s", runtime.GOOS)
} else if err != nil {
t.Fatalf("GetKernelMax: %v", err)
}
t.Logf("KernelMax = %v", n)
}
func testNumAndList(t *testing.T, name string, get func() (int, error), list func() ([]int, error)) int {
t.Helper()
n, errGet := get()
if errors.Is(errGet, numcpus.ErrNotSupported) {
t.Logf("Get%s not supported on %s", name, runtime.GOOS)
} else if errGet != nil {
t.Errorf("Get%s: %v", name, errGet)
} else {
t.Logf("%s = %v", name, n)
}
l, errList := list()
if errors.Is(errList, numcpus.ErrNotSupported) {
t.Skipf("List%s not supported on %s", name, runtime.GOOS)
} else if errList != nil {
t.Fatalf("List%s: %v", name, errList)
}
t.Logf("List%s = %v", name, l)
if errGet == nil && len(l) != n {
t.Errorf("number of online CPUs in list %v doesn't match expected number of CPUs %d", l, n)
}
return n
}
func TestOffline(t *testing.T) {
testNumAndList(t, "Offline", numcpus.GetOffline, numcpus.ListOffline)
}
func TestOnline(t *testing.T) {
n := testNumAndList(t, "Online", numcpus.GetOnline, numcpus.ListOnline)
testGetconf(t, n, "GetOnline", confName("_NPROCESSORS_ONLN"))
}
func TestPossible(t *testing.T) {
testNumAndList(t, "Possible", numcpus.GetPossible, numcpus.ListPossible)
}
func TestPresent(t *testing.T) {
testNumAndList(t, "Present", numcpus.GetPresent, numcpus.ListPresent)
}