summaryrefslogtreecommitdiff
path: root/internal/rpc/client_test.go
blob: 1814170c75125e095e0b40be5868f6fc2f2ff682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package rpc_test

import (
	"net"
	"os"
	"testing"

	"github.com/roadrunner-server/roadrunner/v2/internal/rpc"
	"github.com/stretchr/testify/require"

	"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("test/config_rpc_empty.yaml", nil)

	assert.Nil(t, c)
	assert.EqualError(t, err, "rpc service not specified in the configuration. Tip: add\n rpc:\n\r listen: rr_rpc_address")
}

func TestNewClient_WrongRcpConfiguration(t *testing.T) {
	c, err := rpc.NewClient("test/config_rpc_wrong.yaml", nil)

	assert.Nil(t, c)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "invalid socket DSN")
}

func TestNewClient_ConnectionError(t *testing.T) {
	c, err := rpc.NewClient("test/config_rpc_conn_err.yaml", nil)

	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:55555")
	assert.NoError(t, err)

	defer func() { assert.NoError(t, l.Close()) }()

	c, err := rpc.NewClient("test/config_rpc_ok.yaml", nil)

	assert.NotNil(t, c)
	assert.NoError(t, err)

	defer func() { assert.NoError(t, c.Close()) }()
}

func TestNewClient_SuccessfullyConnectedOverride(t *testing.T) {
	l, err := net.Listen("tcp", "127.0.0.1:55555")
	assert.NoError(t, err)

	defer func() { assert.NoError(t, l.Close()) }()

	c, err := rpc.NewClient("test/config_rpc_empty.yaml", []string{"rpc.listen=tcp://127.0.0.1:55555"})

	assert.NotNil(t, c)
	assert.NoError(t, err)

	defer func() { assert.NoError(t, c.Close()) }()
}

func TestNewClient_SuccessfullyConnectedEnv(t *testing.T) {
	l, err := net.Listen("tcp", "127.0.0.1:55556")
	assert.NoError(t, err)

	defer func() { assert.NoError(t, l.Close()) }()

	require.NoError(t, os.Setenv("RR_RPC_LISTEN", "tcp://127.0.0.1:55556"))
	c, err := rpc.NewClient("test/config_rpc_ok.yaml", nil)

	assert.NotNil(t, c)
	assert.NoError(t, err)

	defer func() { assert.NoError(t, c.Close()) }()
}

// ${} syntax
func TestNewClient_SuccessfullyConnectedEnvDollarSyntax(t *testing.T) {
	l, err := net.Listen("tcp", "127.0.0.1:55556")
	assert.NoError(t, err)

	defer func() { assert.NoError(t, l.Close()) }()

	require.NoError(t, os.Setenv("RPC", "tcp://127.0.0.1:55556"))
	c, err := rpc.NewClient("test/config_rpc_ok_env.yaml", nil)

	assert.NotNil(t, c)
	assert.NoError(t, err)

	defer func() { assert.NoError(t, c.Close()) }()
}