diff options
author | Valery Piashchynski <[email protected]> | 2022-01-15 12:08:20 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-01-15 12:08:20 +0300 |
commit | 5254c8eb27311e2a8a53a4c90c3829cf1238c563 (patch) | |
tree | b51c9a4c1dd4c25adc511498ce0380a7078c5572 /internal/rpc | |
parent | 13609dd03dd0d2fa85b9fb850be787bf4e2ea67f (diff) |
Repository content update
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal/rpc')
-rw-r--r-- | internal/rpc/client.go | 33 | ||||
-rw-r--r-- | internal/rpc/client_test.go | 60 |
2 files changed, 93 insertions, 0 deletions
diff --git a/internal/rpc/client.go b/internal/rpc/client.go new file mode 100644 index 00000000..f371a51c --- /dev/null +++ b/internal/rpc/client.go @@ -0,0 +1,33 @@ +// Package prc contains wrapper around RPC client ONLY for internal usage. +package rpc + +import ( + "net/rpc" + + "github.com/spiral/errors" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" + "github.com/spiral/roadrunner-plugins/v2/config" + rpcPlugin "github.com/spiral/roadrunner-plugins/v2/rpc" +) + +// NewClient creates client ONLY for internal usage (communication between our application with RR side). +// Client will be connected to the RPC. +func NewClient(cfgPlugin *config.Plugin) (*rpc.Client, error) { + if !cfgPlugin.Has(rpcPlugin.PluginName) { + return nil, errors.E("rpc service disabled") + } + + rpcConfig := &rpcPlugin.Config{} + if err := cfgPlugin.UnmarshalKey(rpcPlugin.PluginName, rpcConfig); err != nil { + return nil, err + } + + rpcConfig.InitDefaults() + + conn, err := rpcConfig.Dialer() + if err != nil { + return nil, err + } + + return rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)), nil +} diff --git a/internal/rpc/client_test.go b/internal/rpc/client_test.go new file mode 100644 index 00000000..b39788a2 --- /dev/null +++ b/internal/rpc/client_test.go @@ -0,0 +1,60 @@ +package rpc_test + +import ( + "net" + "testing" + + "github.com/spiral/roadrunner-binary/v2/internal/rpc" + + "github.com/spiral/roadrunner-plugins/v2/config" + "github.com/stretchr/testify/assert" +) + +func TestNewClient_RpcServiceDisabled(t *testing.T) { + cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte{}} + assert.NoError(t, cfgPlugin.Init()) + + c, err := rpc.NewClient(cfgPlugin) + + assert.Nil(t, c) + assert.EqualError(t, err, "rpc service disabled") +} + +func TestNewClient_WrongRcpConfiguration(t *testing.T) { + cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n $foo bar")} + assert.NoError(t, cfgPlugin.Init()) + + c, err := rpc.NewClient(cfgPlugin) + + assert.Nil(t, c) + assert.Error(t, err) + assert.Contains(t, err.Error(), "config_plugin_unmarshal_key") +} + +func TestNewClient_ConnectionError(t *testing.T) { + cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n listen: tcp://127.0.0.1:0")} + assert.NoError(t, cfgPlugin.Init()) + + c, err := rpc.NewClient(cfgPlugin) + + assert.Nil(t, c) + assert.Error(t, err) + assert.Contains(t, err.Error(), "connection refused") +} + +func TestNewClient_SuccessfullyConnected(t *testing.T) { + l, err := net.Listen("tcp", "127.0.0.1:0") + assert.NoError(t, err) + + defer func() { assert.NoError(t, l.Close()) }() + + cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("rpc:\n listen: tcp://" + l.Addr().String())} + assert.NoError(t, cfgPlugin.Init()) + + c, err := rpc.NewClient(cfgPlugin) + + assert.NotNil(t, c) + assert.NoError(t, err) + + defer func() { assert.NoError(t, c.Close()) }() +} |