mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 12:16:16 +08:00
ad057ab873
Conflicts: caddy/parse/parse.go caddy/parse/parsing.go config/config.go config/setup/controller.go main.go server/server.go
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestSetCPU(t *testing.T) {
|
|
currentCPU := runtime.GOMAXPROCS(-1)
|
|
maxCPU := runtime.NumCPU()
|
|
for i, test := range []struct {
|
|
input string
|
|
output int
|
|
shouldErr bool
|
|
}{
|
|
{"1", 1, false},
|
|
{"-1", currentCPU, true},
|
|
{"0", currentCPU, true},
|
|
{"100%", maxCPU, false},
|
|
{"50%", int(0.5 * float32(maxCPU)), false},
|
|
{"110%", currentCPU, true},
|
|
{"-10%", currentCPU, true},
|
|
{"invalid input", currentCPU, true},
|
|
{"invalid input%", currentCPU, true},
|
|
{"9999", maxCPU, false}, // over available CPU
|
|
} {
|
|
err := setCPU(test.input)
|
|
if test.shouldErr && err == nil {
|
|
t.Errorf("Test %d: Expected error, but there wasn't any", i)
|
|
}
|
|
if !test.shouldErr && err != nil {
|
|
t.Errorf("Test %d: Expected no error, but there was one: %v", i, err)
|
|
}
|
|
if actual, expected := runtime.GOMAXPROCS(-1), test.output; actual != expected {
|
|
t.Errorf("Test %d: GOMAXPROCS was %d but expected %d", i, actual, expected)
|
|
}
|
|
// teardown
|
|
runtime.GOMAXPROCS(currentCPU)
|
|
}
|
|
}
|