-
Notifications
You must be signed in to change notification settings - Fork 20
/
proxy_test.go
68 lines (58 loc) · 1.69 KB
/
proxy_test.go
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
package goetty
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
proxyAddress = "unix:///tmp/proxy.sock"
upstream1Address = "unix:///tmp/upstream-1.sock"
upstream2Address = "unix:///tmp/upstream-2.sock"
)
func TestProxy(t *testing.T) {
assert.NoError(t, os.RemoveAll(proxyAddress[7:]))
proxy := NewProxy[string, string](proxyAddress, nil)
assert.NoError(t, proxy.Start())
defer func() {
assert.NoError(t, proxy.Stop())
}()
upstream1 := newTestApp(t,
[]string{upstream1Address},
func(i IOSession[string, string], a string, u uint64) error {
return i.Write("upstream1", WriteOptions{Flush: true})
})
assert.NoError(t, upstream1.Start())
defer func() {
assert.NoError(t, upstream1.Stop())
}()
upstream2 := newTestApp(t,
[]string{upstream2Address},
func(i IOSession[string, string], a string, u uint64) error {
return i.Write("upstream2", WriteOptions{Flush: true})
})
assert.NoError(t, upstream2.Start())
defer func() {
assert.NoError(t, upstream2.Stop())
}()
proxy.AddUpStream(upstream1Address, time.Second)
proxy.AddUpStream(upstream2Address, time.Second)
c1 := newTestIOSession(t)
defer func() {
assert.NoError(t, c1.Close())
}()
assert.NoError(t, c1.Connect(proxyAddress, time.Second))
assert.NoError(t, c1.Write("test", WriteOptions{Flush: true}))
v, err := c1.Read(ReadOptions{})
assert.NoError(t, err)
assert.Equal(t, "upstream1", v)
c2 := newTestIOSession(t)
defer func() {
assert.NoError(t, c2.Close())
}()
assert.NoError(t, c2.Connect(proxyAddress, time.Second))
assert.NoError(t, c2.Write("test", WriteOptions{Flush: true}))
v, err = c2.Read(ReadOptions{})
assert.NoError(t, err)
assert.Equal(t, "upstream2", v)
}