Files
gocryptotrader/utils/utils_test.go
Adrian Gallagher 8ca1e5c36f Fix utils.AdjustGoMaxProcs
Since Go 1.5, Go will use the total number of logical processers that the
system has available. Caveats to this are if someone has set the
GOMAXPROCS env var set or wish to limit usage of the number of logical processers
between a range from 1 to NumCPUs
2019-10-03 10:45:59 +10:00

58 lines
1.2 KiB
Go

package utils
import (
"fmt"
"runtime"
"testing"
)
func TestAdjustGoMaxProcs(t *testing.T) {
// Test default settings
curr := runtime.GOMAXPROCS(-1)
numCPUs := runtime.NumCPU()
// This func both checks for an error of AdjustGoMaxProcs, plus
// ensures that the value it sets is the one that is expected
checker := func(setting, expected int) error {
if err := AdjustGoMaxProcs(setting); err != nil {
return err
}
if i := runtime.GOMAXPROCS(expected); i != expected {
return fmt.Errorf("expected %d, got %d", expected, i)
}
return nil
}
tester := []struct {
Setting int
Expected int
}{
{
// Test setting to current runtime val
Setting: curr,
Expected: curr,
},
{
// Test setting to num of logical CPUs
Setting: numCPUs,
Expected: numCPUs,
},
{
// Test crazy value and make sure it defaults to numCPUs
Setting: 1000,
Expected: numCPUs,
},
{
// Test another crazy value and make sure it defaults to numCPUs
Setting: -1,
Expected: numCPUs,
},
}
for x := range tester {
if err := checker(tester[x].Setting, tester[x].Expected); err != nil {
t.Errorf("%d failed. %s", tester[x], err)
}
}
}