From 4c0d4dd780d0439f03d1dd7fb07329587bd8c031 Mon Sep 17 00:00:00 2001 From: Paulo L F Casaretto Date: Mon, 19 Oct 2015 23:19:12 -0200 Subject: [PATCH 1/4] Test makeOnces function --- config/config_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index 756784191..5c5fd1324 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,22 @@ 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) + } +} From 1db138ed5522f785b4a839a981bdf887198b48de Mon Sep 17 00:00:00 2001 From: Paulo L F Casaretto Date: Mon, 19 Oct 2015 23:23:18 -0200 Subject: [PATCH 2/4] Test makeStorages function --- config/config_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index 5c5fd1324..e48770161 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -83,3 +83,22 @@ func TestMakeOnces(t *testing.T) { 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) + } +} From 0feb0d9244339b15085f84bd2757769e800505b9 Mon Sep 17 00:00:00 2001 From: Paulo L F Casaretto Date: Mon, 19 Oct 2015 23:36:34 -0200 Subject: [PATCH 3/4] Test validDirective function --- config/config_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/config/config_test.go b/config/config_test.go index e48770161..a2c4f9618 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -102,3 +102,23 @@ func TestMakeStorages(t *testing.T) { 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) + } + } +} 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 4/4] 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) + } +}