diff options
Diffstat (limited to 'tests/plugins/status/plugin_test.go')
-rw-r--r-- | tests/plugins/status/plugin_test.go | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/tests/plugins/status/plugin_test.go b/tests/plugins/status/plugin_test.go index a7322353..85e07fcf 100644 --- a/tests/plugins/status/plugin_test.go +++ b/tests/plugins/status/plugin_test.go @@ -188,3 +188,201 @@ func checkRPCStatus(t *testing.T) { assert.NoError(t, err) assert.Equal(t, st.Code, 200) } + +func TestReadyHttp(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-status-init.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &status.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + stopCh := make(chan struct{}, 1) + + go func() { + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + time.Sleep(time.Second) + t.Run("CheckerGetReadiness", checkHTTPReadiness) + + stopCh <- struct{}{} + wg.Wait() +} + +const resp2 = `Service: http: Status: 204 +Service: rpc not found` + +func checkHTTPReadiness(t *testing.T) { + req, err := http.NewRequest("GET", "http://127.0.0.1:34333/ready?plugin=http&plugin=rpc", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, resp, string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func TestReadinessRPCWorkerNotReady(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel), endure.GracefulShutdownTimeout(time.Second*2)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-ready-init.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &status.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + stopCh := make(chan struct{}, 1) + + go func() { + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout, error here is OK, because in the PHP we are sleeping for the 300s + _ = cont.Stop() + return + } + } + }() + + time.Sleep(time.Second) + t.Run("DoHttpReq", doHTTPReq) + time.Sleep(time.Second * 5) + t.Run("CheckerGetReadiness2", checkHTTPReadiness2) + t.Run("CheckerGetRpcReadiness", checkRPCReadiness) + stopCh <- struct{}{} + wg.Wait() +} + +func doHTTPReq(t *testing.T) { + go func() { + req, err := http.NewRequest("GET", "http://localhost:11933", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, resp2, string(b)) + + err = r.Body.Close() + assert.NoError(t, err) + }() +} + +func checkHTTPReadiness2(t *testing.T) { + req, err := http.NewRequest("GET", "http://127.0.0.1:34334/ready?plugin=http&plugin=rpc", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, resp2, string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func checkRPCReadiness(t *testing.T) { + conn, err := net.Dial("tcp", "127.0.0.1:6007") + assert.NoError(t, err) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) + + st := &status.Status{} + + err = client.Call("status.Ready", "http", &st) + assert.NoError(t, err) + assert.Equal(t, st.Code, 204) +} |