From 0304f09d7e5fa0c76b86429a654aeb366cec6391 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Wed, 4 Nov 2020 15:43:16 +0300 Subject: Add rpc disabled test --- plugins/rpc/tests/.rr-rpc-disabled.yaml | 3 ++ plugins/rpc/tests/plugin2.go | 2 +- plugins/rpc/tests/rpc_test.go | 80 ++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 plugins/rpc/tests/.rr-rpc-disabled.yaml (limited to 'plugins') diff --git a/plugins/rpc/tests/.rr-rpc-disabled.yaml b/plugins/rpc/tests/.rr-rpc-disabled.yaml new file mode 100644 index 00000000..624fb3c5 --- /dev/null +++ b/plugins/rpc/tests/.rr-rpc-disabled.yaml @@ -0,0 +1,3 @@ +rpc: + listen: tcp://127.0.0.1:6001 + disabled: true \ No newline at end of file diff --git a/plugins/rpc/tests/plugin2.go b/plugins/rpc/tests/plugin2.go index feeae4af..854bf097 100644 --- a/plugins/rpc/tests/plugin2.go +++ b/plugins/rpc/tests/plugin2.go @@ -27,7 +27,7 @@ func (p2 *Plugin2) Serve() chan error { conn, err := net.Dial("tcp", "127.0.0.1:6001") if err != nil { - errCh <- err + errCh <- errors.E(errors.Serve, err) return } client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) diff --git a/plugins/rpc/tests/rpc_test.go b/plugins/rpc/tests/rpc_test.go index 85a11eec..0014cc2a 100644 --- a/plugins/rpc/tests/rpc_test.go +++ b/plugins/rpc/tests/rpc_test.go @@ -17,7 +17,7 @@ import ( // graph https://bit.ly/3ensdNb func TestRpcInit(t *testing.T) { - cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel)) if err != nil { t.Fatal(err) } @@ -93,3 +93,81 @@ func TestRpcInit(t *testing.T) { } } } + +// graph https://bit.ly/3ensdNb +func TestRpcDisabled(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + + err = cont.Register(&Plugin1{}) + if err != nil { + t.Fatal(err) + } + + err = cont.Register(&Plugin2{}) + if err != nil { + t.Fatal(err) + } + + v := &config.Viper{} + v.Path = ".rr-rpc-disabled.yaml" + v.Prefix = "rr" + err = cont.Register(v) + if err != nil { + t.Fatal(err) + } + + err = cont.Register(&rpc.Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = cont.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(err) + } + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + if err != nil { + t.Fatal(err) + } + + sig := make(chan os.Signal, 1) + + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + tt := time.NewTimer(time.Second * 10) + + for { + select { + case e := <-ch: + // RPC is turned off, should be and dial error + if errors.Is(errors.Disabled, e.Error) { + assert.FailNow(t, "should not be disabled error") + } + assert.Error(t, e.Error) + err = cont.Stop() + assert.Error(t, e.Error) + return + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + assert.Fail(t, "timeout") + } + } +} -- cgit v1.2.3