diff options
Diffstat (limited to 'pipe_factory_test.go')
-rwxr-xr-x[-rw-r--r--] | pipe_factory_test.go | 143 |
1 files changed, 109 insertions, 34 deletions
diff --git a/pipe_factory_test.go b/pipe_factory_test.go index 14cf1272..bdb861de 100644..100755 --- a/pipe_factory_test.go +++ b/pipe_factory_test.go @@ -1,16 +1,19 @@ package roadrunner import ( - "github.com/stretchr/testify/assert" + "context" "os/exec" "testing" "time" + + "github.com/stretchr/testify/assert" ) func Test_Pipe_Start(t *testing.T) { + ctx := context.Background() cmd := exec.Command("php", "tests/client.php", "echo", "pipes") - w, err := NewPipeFactory().SpawnWorker(cmd) + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) assert.NoError(t, err) assert.NotNil(t, w) @@ -18,7 +21,7 @@ func Test_Pipe_Start(t *testing.T) { assert.NoError(t, w.Wait()) }() - assert.NoError(t, w.Stop()) + assert.NoError(t, w.Stop(ctx)) } func Test_Pipe_StartError(t *testing.T) { @@ -28,7 +31,8 @@ func Test_Pipe_StartError(t *testing.T) { t.Errorf("error running the command: error %v", err) } - w, err := NewPipeFactory().SpawnWorker(cmd) + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) assert.Error(t, err) assert.Nil(t, w) } @@ -40,7 +44,8 @@ func Test_Pipe_PipeError(t *testing.T) { t.Errorf("error creating the STDIN pipe: error %v", err) } - w, err := NewPipeFactory().SpawnWorker(cmd) + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) assert.Error(t, err) assert.Nil(t, w) } @@ -52,14 +57,16 @@ func Test_Pipe_PipeError2(t *testing.T) { t.Errorf("error creating the STDIN pipe: error %v", err) } - w, err := NewPipeFactory().SpawnWorker(cmd) + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) assert.Error(t, err) assert.Nil(t, w) } func Test_Pipe_Failboot(t *testing.T) { cmd := exec.Command("php", "tests/failboot.php") - w, err := NewPipeFactory().SpawnWorker(cmd) + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) assert.Nil(t, w) assert.Error(t, err) @@ -68,27 +75,32 @@ func Test_Pipe_Failboot(t *testing.T) { func Test_Pipe_Invalid(t *testing.T) { cmd := exec.Command("php", "tests/invalid.php") - - w, err := NewPipeFactory().SpawnWorker(cmd) + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) assert.Error(t, err) assert.Nil(t, w) } func Test_Pipe_Echo(t *testing.T) { cmd := exec.Command("php", "tests/client.php", "echo", "pipes") - - w, _ := NewPipeFactory().SpawnWorker(cmd) - go func() { - assert.NoError(t, w.Wait()) - }() + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) + if err != nil { + t.Fatal(err) + } defer func() { - err := w.Stop() + err = w.Stop(ctx) if err != nil { - t.Errorf("error stopping the worker: error %v", err) + t.Errorf("error stopping the WorkerProcess: error %v", err) } }() - res, err := w.Exec(&Payload{Body: []byte("hello")}) + sw, err := NewSyncWorker(w) + if err != nil { + t.Fatal(err) + } + + res, err := sw.Exec(Payload{Body: []byte("hello")}) assert.NoError(t, err) assert.NotNil(t, res) @@ -100,38 +112,41 @@ func Test_Pipe_Echo(t *testing.T) { func Test_Pipe_Broken(t *testing.T) { cmd := exec.Command("php", "tests/client.php", "broken", "pipes") - - w, _ := NewPipeFactory().SpawnWorker(cmd) - go func() { - err := w.Wait() - - assert.Error(t, err) - assert.Contains(t, err.Error(), "undefined_function()") - }() + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) + if err != nil { + t.Fatal(err) + } defer func() { time.Sleep(time.Second) - err := w.Stop() - assert.NoError(t, err) + err = w.Stop(ctx) + assert.Error(t, err) }() - res, err := w.Exec(&Payload{Body: []byte("hello")}) + sw, err := NewSyncWorker(w) + if err != nil { + t.Fatal(err) + } + + res, err := sw.Exec(Payload{Body: []byte("hello")}) assert.Error(t, err) - assert.Nil(t, res) + assert.Nil(t, res.Body) + assert.Nil(t, res.Context) } func Benchmark_Pipe_SpawnWorker_Stop(b *testing.B) { f := NewPipeFactory() for n := 0; n < b.N; n++ { cmd := exec.Command("php", "tests/client.php", "echo", "pipes") - w, _ := f.SpawnWorker(cmd) + w, _ := f.SpawnWorkerWithContext(context.Background(), cmd) go func() { if w.Wait() != nil { b.Fail() } }() - err := w.Stop() + err := w.Stop(context.Background()) if err != nil { b.Errorf("error stopping the worker: error %v", err) } @@ -141,7 +156,13 @@ func Benchmark_Pipe_SpawnWorker_Stop(b *testing.B) { func Benchmark_Pipe_Worker_ExecEcho(b *testing.B) { cmd := exec.Command("php", "tests/client.php", "echo", "pipes") - w, _ := NewPipeFactory().SpawnWorker(cmd) + w, _ := NewPipeFactory().SpawnWorkerWithContext(context.Background(), cmd) + sw, err := NewSyncWorker(w) + if err != nil { + b.Fatal(err) + } + b.ReportAllocs() + b.ResetTimer() go func() { err := w.Wait() if err != nil { @@ -149,14 +170,68 @@ func Benchmark_Pipe_Worker_ExecEcho(b *testing.B) { } }() defer func() { - err := w.Stop() + err := w.Stop(context.Background()) if err != nil { b.Errorf("error stopping the worker: error %v", err) } }() for n := 0; n < b.N; n++ { - if _, err := w.Exec(&Payload{Body: []byte("hello")}); err != nil { + if _, err := sw.Exec(Payload{Body: []byte("hello")}); err != nil { + b.Fail() + } + } +} + +func Benchmark_Pipe_Worker_ExecEcho3(b *testing.B) { + cmd := exec.Command("php", "tests/client.php", "echo", "pipes") + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) + if err != nil { + b.Fatal(err) + } + + defer func() { + err = w.Stop(ctx) + if err != nil { + b.Errorf("error stopping the WorkerProcess: error %v", err) + } + }() + + sw, err := NewSyncWorker(w) + if err != nil { + b.Fatal(err) + } + + for n := 0; n < b.N; n++ { + if _, err := sw.Exec(Payload{Body: []byte("hello")}); err != nil { + b.Fail() + } + } +} + +func Benchmark_Pipe_Worker_ExecEchoWithoutContext(b *testing.B) { + cmd := exec.Command("php", "tests/client.php", "echo", "pipes") + ctx := context.Background() + w, err := NewPipeFactory().SpawnWorkerWithContext(ctx, cmd) + if err != nil { + b.Fatal(err) + } + + defer func() { + err = w.Stop(ctx) + if err != nil { + b.Errorf("error stopping the WorkerProcess: error %v", err) + } + }() + + sw, err := NewSyncWorker(w) + if err != nil { + b.Fatal(err) + } + + for n := 0; n < b.N; n++ { + if _, err := sw.Exec(Payload{Body: []byte("hello")}); err != nil { b.Fail() } } |