summaryrefslogtreecommitdiff
path: root/server_config_test.go
blob: 303eba908f54c19e3be6d055d078a16743fe61bb (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package roadrunner

import (
	"github.com/stretchr/testify/assert"
	"runtime"
	"testing"
	"time"
)

func Test_ServerConfig_PipeFactory(t *testing.T) {
	cfg := &ServerConfig{Relay: "pipes"}
	f, err := cfg.makeFactory()

	assert.NoError(t, err)
	assert.IsType(t, &PipeFactory{}, f)

	cfg = &ServerConfig{Relay: "pipe"}
	f, err = cfg.makeFactory()
	assert.NoError(t, err)
	assert.NotNil(t, f)
	defer func() {
		err := f.Close()
		if err != nil {
			t.Errorf("error closing factory or underlying connections: error %v", err)
		}
	}()

	assert.NoError(t, err)
	assert.IsType(t, &PipeFactory{}, f)
}

func Test_ServerConfig_SocketFactory(t *testing.T) {
	cfg := &ServerConfig{Relay: "tcp://:9111"}
	f1, err := cfg.makeFactory()
	assert.NoError(t, err)
	assert.NotNil(t, f1)
	defer func() {
		err := f1.Close()

		if err != nil {
			t.Errorf("error closing factory or underlying connections: error %v", err)
		}
	}()

	assert.NoError(t, err)
	assert.IsType(t, &SocketFactory{}, f1)
	assert.Equal(t, "tcp", f1.(*SocketFactory).ls.Addr().Network())
	assert.Equal(t, "[::]:9111", f1.(*SocketFactory).ls.Addr().String())

	cfg = &ServerConfig{Relay: "tcp://localhost:9112"}
	f, err := cfg.makeFactory()
	assert.NoError(t, err)
	assert.NotNil(t, f)
	defer func() {
		err := f.Close()
		if err != nil {
			t.Errorf("error closing factory or underlying connections: error %v", err)
		}
	}()

	assert.NoError(t, err)
	assert.IsType(t, &SocketFactory{}, f)
	assert.Equal(t, "tcp", f.(*SocketFactory).ls.Addr().Network())
	assert.Equal(t, "127.0.0.1:9112", f.(*SocketFactory).ls.Addr().String())
}

func Test_ServerConfig_UnixSocketFactory(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("not supported on " + runtime.GOOS)
	}

	cfg := &ServerConfig{Relay: "unix://unix.sock"}
	f, err := cfg.makeFactory()
	if err != nil {
		t.Error(err)
	}

	defer func() {
		err := f.Close()
		if err != nil {
			t.Errorf("error closing factory or underlying connections: error %v", err)
		}
	}()

	assert.NoError(t, err)
	assert.IsType(t, &SocketFactory{}, f)
	assert.Equal(t, "unix", f.(*SocketFactory).ls.Addr().Network())
	assert.Equal(t, "unix.sock", f.(*SocketFactory).ls.Addr().String())
}

func Test_ServerConfig_ErrorFactory(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("not supported on " + runtime.GOOS)
	}

	cfg := &ServerConfig{Relay: "uni:unix.sock"}
	f, err := cfg.makeFactory()
	assert.Nil(t, f)
	assert.Error(t, err)
	assert.Equal(t, "invalid relay DSN (pipes, tcp://:6001, unix://rr.sock)", err.Error())
}

func Test_ServerConfig_ErrorMethod(t *testing.T) {
	cfg := &ServerConfig{Relay: "xinu://unix.sock"}

	f, err := cfg.makeFactory()
	assert.Nil(t, f)
	assert.Error(t, err)
}

func Test_ServerConfig_Cmd(t *testing.T) {
	cfg := &ServerConfig{
		Command: "php tests/client.php pipes",
	}

	cmd := cfg.makeCommand()
	assert.NotNil(t, cmd)
}

func Test_ServerConfig_SetEnv(t *testing.T) {
	cfg := &ServerConfig{
		Command: "php tests/client.php pipes",
		Relay:   "pipes",
	}

	cfg.SetEnv("key", "value")

	cmd := cfg.makeCommand()
	assert.NotNil(t, cmd)

	c := cmd()

	assert.Contains(t, c.Env, "KEY=value")
	assert.Contains(t, c.Env, "RR_RELAY=pipes")
}

func Test_ServerConfig_SetEnv_Relay(t *testing.T) {
	cfg := &ServerConfig{
		Command: "php tests/client.php pipes",
		Relay:   "unix://rr.sock",
	}

	cfg.SetEnv("key", "value")

	cmd := cfg.makeCommand()
	assert.NotNil(t, cmd)

	c := cmd()

	assert.Contains(t, c.Env, "KEY=value")
	assert.Contains(t, c.Env, "RR_RELAY=unix://rr.sock")
}

func Test_ServerConfigDefaults(t *testing.T) {
	cfg := &ServerConfig{
		Command: "php tests/client.php pipes",
	}

	err := cfg.InitDefaults()
	if err != nil {
		t.Errorf("error during the InitDefaults: error %v", err)
	}

	assert.Equal(t, "pipes", cfg.Relay)
	assert.Equal(t, time.Minute, cfg.Pool.AllocateTimeout)
	assert.Equal(t, time.Minute, cfg.Pool.DestroyTimeout)
}

func Test_Config_Upscale(t *testing.T) {
	cfg := &ServerConfig{
		Command:      "php tests/client.php pipes",
		RelayTimeout: 1,
		Pool: &Config{
			AllocateTimeout: 1,
			DestroyTimeout:  1,
		},
	}

	cfg.UpscaleDurations()
	assert.Equal(t, time.Second, cfg.RelayTimeout)
	assert.Equal(t, time.Second, cfg.Pool.AllocateTimeout)
	assert.Equal(t, time.Second, cfg.Pool.DestroyTimeout)
}