From 815231b1e029fedd94c7e7f9e8a33b9c712cb7e3 Mon Sep 17 00:00:00 2001 From: Paulo L F Casaretto Date: Tue, 20 Oct 2015 00:19:25 -0200 Subject: [PATCH] Test app.SetCPU function --- app/app_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/app_test.go diff --git a/app/app_test.go b/app/app_test.go new file mode 100644 index 000000000..e9dc640a0 --- /dev/null +++ b/app/app_test.go @@ -0,0 +1,42 @@ +package app_test + +import ( + "runtime" + "testing" + + "github.com/mholt/caddy/app" +) + +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 := app.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) + } +}