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) + } +} diff --git a/config/config_test.go b/config/config_test.go index 756784191..a2c4f9618 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,8 @@ package config import ( + "reflect" + "sync" "testing" "github.com/mholt/caddy/server" @@ -62,3 +64,61 @@ func TestResolveAddr(t *testing.T) { } } } + +func TestMakeOnces(t *testing.T) { + directives := []directive{ + {"dummy", nil}, + {"dummy2", nil}, + } + directiveOrder = directives + onces := makeOnces() + if len(onces) != len(directives) { + t.Errorf("onces had len %d , expected %d", len(onces), len(directives)) + } + expected := map[string]*sync.Once{ + "dummy": new(sync.Once), + "dummy2": new(sync.Once), + } + if !reflect.DeepEqual(onces, expected) { + t.Errorf("onces was %v, expected %v", onces, expected) + } +} + +func TestMakeStorages(t *testing.T) { + directives := []directive{ + {"dummy", nil}, + {"dummy2", nil}, + } + directiveOrder = directives + storages := makeStorages() + if len(storages) != len(directives) { + t.Errorf("storages had len %d , expected %d", len(storages), len(directives)) + } + expected := map[string]interface{}{ + "dummy": nil, + "dummy2": nil, + } + if !reflect.DeepEqual(storages, expected) { + t.Errorf("storages was %v, expected %v", storages, expected) + } +} + +func TestValidDirective(t *testing.T) { + directives := []directive{ + {"dummy", nil}, + {"dummy2", nil}, + } + directiveOrder = directives + for i, test := range []struct { + directive string + valid bool + }{ + {"dummy", true}, + {"dummy2", true}, + {"dummy3", false}, + } { + if actual, expected := validDirective(test.directive), test.valid; actual != expected { + t.Errorf("Test %d: valid was %t, expected %t", i, actual, expected) + } + } +}