diff options
author | Dmitry Patsura <[email protected]> | 2019-06-13 17:43:39 +0300 |
---|---|---|
committer | Dmitry Patsura <[email protected]> | 2019-06-13 20:00:56 +0300 |
commit | 82cfc4f8f89252083aa09e8370b9605d38808b70 (patch) | |
tree | a5846b0ca28248e895148aa866bea416e26fb69c /service/rpc | |
parent | 27e8cb3c4086012968b586fcbd5fd40737be2510 (diff) |
Feature: Extract code to util.CreateListener
Diffstat (limited to 'service/rpc')
-rw-r--r-- | service/rpc/config.go | 17 | ||||
-rw-r--r-- | service/rpc/config_test.go | 12 |
2 files changed, 10 insertions, 19 deletions
diff --git a/service/rpc/config.go b/service/rpc/config.go index fc8cfdbb..8a29c2d8 100644 --- a/service/rpc/config.go +++ b/service/rpc/config.go @@ -3,9 +3,9 @@ package rpc import ( "errors" "github.com/spiral/roadrunner/service" + "github.com/spiral/roadrunner/util" "net" "strings" - "syscall" ) // Config defines RPC service config. @@ -37,7 +37,7 @@ func (c *Config) InitDefaults() error { // Valid returns nil if config is valid. func (c *Config) Valid() error { if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 { - return errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") + return errors.New("invalid socket DSN (tcp://:6001, unix://file.sock)") } return nil @@ -45,23 +45,14 @@ func (c *Config) Valid() error { // Listener creates new rpc socket Listener. func (c *Config) Listener() (net.Listener, error) { - dsn := strings.Split(c.Listen, "://") - if len(dsn) != 2 { - return nil, errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") - } - - if dsn[0] == "unix" { - syscall.Unlink(dsn[1]) - } - - return net.Listen(dsn[0], dsn[1]) + return util.CreateListener(c.Listen); } // Dialer creates rpc socket Dialer. func (c *Config) Dialer() (net.Conn, error) { dsn := strings.Split(c.Listen, "://") if len(dsn) != 2 { - return nil, errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") + return nil, errors.New("invalid socket DSN (tcp://:6001, unix://file.sock)") } return net.Dial(dsn[0], dsn[1]) diff --git a/service/rpc/config_test.go b/service/rpc/config_test.go index 6a2138d2..6cf63eea 100644 --- a/service/rpc/config_test.go +++ b/service/rpc/config_test.go @@ -51,7 +51,7 @@ func TestConfig_ListenerUnix(t *testing.T) { t.Skip("not supported on " + runtime.GOOS) } - cfg := &Config{Listen: "unix://rpc.sock"} + cfg := &Config{Listen: "unix://file.sock"} ln, err := cfg.Listener() assert.NoError(t, err) @@ -59,7 +59,7 @@ func TestConfig_ListenerUnix(t *testing.T) { defer ln.Close() assert.Equal(t, "unix", ln.Addr().Network()) - assert.Equal(t, "rpc.sock", ln.Addr().String()) + assert.Equal(t, "file.sock", ln.Addr().String()) } func Test_Config_Error(t *testing.T) { @@ -71,7 +71,7 @@ func Test_Config_Error(t *testing.T) { ln, err := cfg.Listener() assert.Nil(t, ln) assert.Error(t, err) - assert.Equal(t, "invalid socket DSN (tcp://:6001, unix://rpc.sock)", err.Error()) + assert.Equal(t, "invalid socket DSN (tcp://:6001, unix://file.sock)", err.Error()) } func Test_Config_ErrorMethod(t *testing.T) { @@ -102,7 +102,7 @@ func TestConfig_DialerUnix(t *testing.T) { t.Skip("not supported on " + runtime.GOOS) } - cfg := &Config{Listen: "unix://rpc.sock"} + cfg := &Config{Listen: "unix://file.sock"} ln, _ := cfg.Listener() defer ln.Close() @@ -113,7 +113,7 @@ func TestConfig_DialerUnix(t *testing.T) { defer conn.Close() assert.Equal(t, "unix", conn.RemoteAddr().Network()) - assert.Equal(t, "rpc.sock", conn.RemoteAddr().String()) + assert.Equal(t, "file.sock", conn.RemoteAddr().String()) } func Test_Config_DialerError(t *testing.T) { @@ -125,7 +125,7 @@ func Test_Config_DialerError(t *testing.T) { ln, err := cfg.Dialer() assert.Nil(t, ln) assert.Error(t, err) - assert.Equal(t, "invalid socket DSN (tcp://:6001, unix://rpc.sock)", err.Error()) + assert.Equal(t, "invalid socket DSN (tcp://:6001, unix://file.sock)", err.Error()) } func Test_Config_DialerErrorMethod(t *testing.T) { |