-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdriver_test.go
More file actions
82 lines (63 loc) · 1.84 KB
/
driver_test.go
File metadata and controls
82 lines (63 loc) · 1.84 KB
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
package bindingsdriver
import (
"fmt"
"io"
"math/rand/v2"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func loopbackAddr(port int32) string {
return fmt.Sprintf("127.0.0.1:%d", port)
}
func randomPort() int32 {
p := 30000 + rand.IntN(1000)
return int32(p)
}
func testConnectionHandler(conn net.Conn) error {
defer conn.Close()
_, err := conn.Write([]byte("hello world"))
return err
}
func TestBindingsListener(t *testing.T) {
port := randomPort()
bl, err := newBindingsListener(
loopbackAddr(port),
testConnectionHandler,
)
assert.NoError(t, err)
assert.NotNil(t, bl)
// test that we can connect to the listener
conn, err := net.DialTimeout("tcp", loopbackAddr(port), 10*time.Millisecond)
assert.NoError(t, err)
out, err := io.ReadAll(conn)
assert.NoError(t, err)
assert.Equal(t, "hello world", string(out))
assert.NotPanics(t, func() { bl.Stop() })
// test that we can't connect to the listener after it's stopped
conn, err = net.DialTimeout("tcp", loopbackAddr(port), 10*time.Millisecond)
assert.Error(t, err)
assert.Nil(t, conn)
// test that we can stop the listener multiple times
assert.NotPanics(t, func() { bl.Stop() })
}
func TestBindingsDriver(t *testing.T) {
b := New()
assert.NotNil(t, b)
port := randomPort()
err := b.Listen(port, testConnectionHandler)
assert.NoError(t, err)
// test that we can connect to the listener
conn, err := net.Dial("tcp", loopbackAddr(port))
assert.NoError(t, err)
out, err := io.ReadAll(conn)
assert.NoError(t, err)
assert.Equal(t, "hello world", string(out))
// test that trying to start a listener on the same port doesn't cause an error
// and that only one listener exists
assert.NoError(t, b.Listen(port, testConnectionHandler))
assert.Len(t, b.listenerMap, 1)
assert.NotPanics(t, func() { b.Close(port) })
assert.NotPanics(t, func() { b.Close(port) })
}