File: numcpus_test.go

package info (click to toggle)
golang-github-tklauser-numcpus 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, experimental, forky, sid, trixie
  • size: 128 kB
  • sloc: makefile: 3
file content (116 lines) | stat: -rw-r--r-- 3,072 bytes parent folder | download | duplicates (3)
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
// 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 out bytes.Buffer
	cmd.Stdout = &out
	if err := cmd.Run(); err != nil {
		t.Skipf("failed to invoke getconf: %v", err)
	}

	want, err := strconv.ParseInt(strings.TrimSpace(out.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 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", "_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 TestGetOffline(t *testing.T) {
	n, err := numcpus.GetOffline()
	if errors.Is(err, numcpus.ErrNotSupported) {
		t.Skipf("GetOffline not supported on %s", runtime.GOOS)
	} else if err != nil {
		t.Fatalf("GetOffline: %v", err)
	}
	t.Logf("Offline = %v", n)
}

func TestGetOnline(t *testing.T) {
	n, err := numcpus.GetOnline()
	if errors.Is(err, numcpus.ErrNotSupported) {
		t.Skipf("GetOnline not supported on %s", runtime.GOOS)
	} else if err != nil {
		t.Fatalf("GetOnline: %v", err)
	}
	t.Logf("Online = %v", n)

	testGetconf(t, n, "GetOnline", "_NPROCESSORS_ONLN")
}

func TestGetPossible(t *testing.T) {
	n, err := numcpus.GetPossible()
	if errors.Is(err, numcpus.ErrNotSupported) {
		t.Skipf("GetPossible not supported on %s", runtime.GOOS)
	} else if err != nil {
		t.Fatalf("GetPossible: %v", err)
	}
	t.Logf("Possible = %v", n)
}

func TestGetPresent(t *testing.T) {
	n, err := numcpus.GetPresent()
	if errors.Is(err, numcpus.ErrNotSupported) {
		t.Skipf("GetPresent not supported on %s", runtime.GOOS)
	} else if err != nil {
		t.Fatalf("GetPresent: %v", err)
	}
	t.Logf("Present = %v", n)
}