diff options
-rwxr-xr-x | errors/errors.go | 1 | ||||
-rwxr-xr-x | errors/errors_test.go | 177 | ||||
-rwxr-xr-x | errors/marshal.go | 1 | ||||
-rwxr-xr-x | static_pool.go | 4 | ||||
-rw-r--r-- | supervisor_test.go | 4 | ||||
-rwxr-xr-x | worker_watcher.go | 5 |
6 files changed, 4 insertions, 188 deletions
diff --git a/errors/errors.go b/errors/errors.go index c9455367..ec621b67 100755 --- a/errors/errors.go +++ b/errors/errors.go @@ -51,7 +51,6 @@ func (k Kind) String() string { return "Watcher stopped" case TimeOut: return "TimedOut" - } return "unknown error kind" } diff --git a/errors/errors_test.go b/errors/errors_test.go deleted file mode 100755 index 50d3d422..00000000 --- a/errors/errors_test.go +++ /dev/null @@ -1,177 +0,0 @@ -// +build !debug - -package errors - -import ( - "fmt" - "io" - "os" - "os/exec" - "testing" -) - -func TestDebug(t *testing.T) { - // Test with -tags debug to run the tests in debug_test.go - cmd := exec.Command("go", "test", "-tags", "prod") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - t.Fatalf("external go test failed: %v", err) - } -} - -func TestMarshal(t *testing.T) { - // Single error. No user is set, so we will have a zero-length field inside. - e1 := E(Op("Get"), Network, "caching in progress") - - // Nested error. - e2 := E(Op("Read"), Undefined, e1) - - b := MarshalError(e2) - e3 := UnmarshalError(b) - - in := e2.(*Error) - out := e3.(*Error) - - // Compare elementwise. - if in.Op != out.Op { - t.Errorf("expected Op %q; got %q", in.Op, out.Op) - } - if in.Kind != out.Kind { - t.Errorf("expected kind %d; got %d", in.Kind, out.Kind) - } - // Note that error will have lost type information, so just check its Error string. - if in.Err.Error() != out.Err.Error() { - t.Errorf("expected Err %q; got %q", in.Err, out.Err) - } -} - -func TestSeparator(t *testing.T) { - defer func(prev string) { - Separator = prev - }(Separator) - Separator = ":: " - - // Single error. No user is set, so we will have a zero-length field inside. - e1 := E(Op("Get"), Network, "network error") - - // Nested error. - e2 := E(Op("Get"), Network, e1) - - want := "Get: Network error:: Get: network error" - if errorAsString(e2) != want { - t.Errorf("expected %q; got %q", want, e2) - } -} - -func TestDoesNotChangePreviousError(t *testing.T) { - err := E(Network) - err2 := E(Op("I will NOT modify err"), err) - - expected := "I will NOT modify err: Network error" - if errorAsString(err2) != expected { - t.Fatalf("Expected %q, got %q", expected, err2) - } - kind := err.(*Error).Kind - if kind != Network { - t.Fatalf("Expected kind %v, got %v", Network, kind) - } -} - -//func TestNoArgs(t *testing.T) { -// defer func() { -// err := recover() -// if err == nil { -// t.Fatal("E() did not panic") -// } -// }() -// _ = E() -//} - -type matchTest struct { - err1, err2 error - matched bool -} - -const ( - op = Op("Op") - op1 = Op("Op1") - op2 = Op("Op2") -) - -var matchTests = []matchTest{ - // Errors not of type *Error fail outright. - {nil, nil, false}, - {io.EOF, io.EOF, false}, - {E(io.EOF), io.EOF, false}, - {io.EOF, E(io.EOF), false}, - // Success. We can drop fields from the first argument and still match. - {E(io.EOF), E(io.EOF), true}, - {E(op, Other, io.EOF), E(op, Other, io.EOF), true}, - {E(op, Other, io.EOF, "test"), E(op, Other, io.EOF, "test", "test"), true}, - {E(op, Other), E(op, Other, io.EOF, "test", "test"), true}, - {E(op), E(op, Other, io.EOF, "test", "test"), true}, - // Failure. - {E(io.EOF), E(io.ErrClosedPipe), false}, - {E(op1), E(op2), false}, - {E(Other), E(Network), false}, - {E("test"), E("test1"), false}, - {E(fmt.Errorf("error")), E(fmt.Errorf("error1")), false}, - {E(op, Other, io.EOF, "test", "test1"), E(op, Other, io.EOF, "test", "test"), false}, - {E("test", Str("something")), E("test"), false}, // Test nil error on rhs. - // Nested *Errors. - {E(op1, E("test")), E(op1, "1", E(op2, "2", "test")), true}, - {E(op1, "test"), E(op1, "1", E(op2, "2", "test")), false}, - {E(op1, E("test")), E(op1, "1", Str(E(op2, "2", "test").Error())), false}, -} - -func TestMatch(t *testing.T) { - for _, test := range matchTests { - matched := Match(test.err1, test.err2) - if matched != test.matched { - t.Errorf("Match(%q, %q)=%t; want %t", test.err1, test.err2, matched, test.matched) - } - } -} - -type kindTest struct { - err error - kind Kind - want bool -} - -var kindTests = []kindTest{ - //Non-Error errors. - {nil, Network, false}, - {Str("not an *Error"), Network, false}, - - // Basic comparisons. - {E(Network), Network, true}, - {E(Test), Network, false}, - {E("no kind"), Network, false}, - {E("no kind"), Other, false}, - - // Nested *Error values. - {E("Nesting", E(Network)), Network, true}, - {E("Nesting", E(Test)), Network, false}, - {E("Nesting", E("no kind")), Network, false}, - {E("Nesting", E("no kind")), Other, false}, -} - -func TestKind(t *testing.T) { - for _, test := range kindTests { - got := Is(test.kind, test.err) - if got != test.want { - t.Errorf("Is(%q, %q)=%t; want %t", test.kind, test.err, got, test.want) - } - } -} - -func errorAsString(err error) string { - if e, ok := err.(*Error); ok { - e2 := *e - e2.stack = stack{} - return e2.Error() - } - return err.Error() -} diff --git a/errors/marshal.go b/errors/marshal.go index a13ec01f..7c8a63ef 100755 --- a/errors/marshal.go +++ b/errors/marshal.go @@ -35,7 +35,6 @@ func MarshalErrorAppend(err error, b []byte) []byte { b = append(b, 'e') b = appendString(b, err.Error()) return b - } func MarshalError(err error) []byte { diff --git a/static_pool.go b/static_pool.go index 2337b2fd..be7ad6e3 100755 --- a/static_pool.go +++ b/static_pool.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os/exec" - "sync" "github.com/spiral/roadrunner/v2/errors" "github.com/spiral/roadrunner/v2/util" @@ -28,9 +27,6 @@ type StaticPool struct { // distributes the events events *util.EventHandler - // protects state of worker list, does not affect allocation - muw sync.RWMutex - // manages worker states and TTLs ww *workerWatcher } diff --git a/supervisor_test.go b/supervisor_test.go index b6aa8372..34172d7d 100644 --- a/supervisor_test.go +++ b/supervisor_test.go @@ -37,7 +37,7 @@ func TestSupervisedPool_Exec(t *testing.T) { defer p.Destroy(context.Background()) go func() { - for ; ; { + for { select { case <-stopCh: return @@ -49,7 +49,7 @@ func TestSupervisedPool_Exec(t *testing.T) { assert.NotNil(t, s) // since this is soft limit, double max memory limit watch if (s.MemoryUsage / MB) > cfgSupervised.Supervisor.MaxWorkerMemory*2 { - t.Fatal("max memory reached") + assert.Fail(t, "max memory reached") } } } diff --git a/worker_watcher.go b/worker_watcher.go index f49eeacf..d289750e 100755 --- a/worker_watcher.go +++ b/worker_watcher.go @@ -2,6 +2,7 @@ package roadrunner import ( "context" + "runtime" "sync" "time" @@ -9,8 +10,6 @@ import ( "github.com/spiral/roadrunner/v2/util" ) -//var = errors.New("watcher stopped") - type Stack struct { workers []WorkerBase mutex sync.RWMutex @@ -19,7 +18,7 @@ type Stack struct { func NewWorkersStack() *Stack { return &Stack{ - workers: make([]WorkerBase, 0), + workers: make([]WorkerBase, 0, runtime.NumCPU()), } } |