-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery_test.go
63 lines (53 loc) · 2.07 KB
/
discovery_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
package main
import (
"github.com/stretchr/testify/assert"
"net"
"testing"
"time"
)
func TestDiscoveryServer_DiscoveryReply(t *testing.T) {
var server = NewDiscoverySever(32227, 12345)
go server.Start()
time.Sleep(1 * time.Second)
defer server.Close()
addr, _ := net.ResolveUDPAddr("udp4", "127.0.0.1:32227")
conn, _ := net.ListenPacket("udp", ":0")
conn.WriteTo([]byte("alpacadiscovery1"), addr)
buf := make([]byte, 22)
conn.ReadFrom(buf)
conn.Close()
msg := string(buf)
assert.Equal(t, "{\n\"AlpacaPort\":12345\n}", msg, "they should be equal")
}
func TestDiscoveryServer_BindPortDefault(t *testing.T) {
var server = NewDiscoverySever(32227, 11111)
assert.Equal(t, ":32227", server.ListenString, "they should be equal")
}
func TestDiscoveryServer_BindPortCustom(t *testing.T) {
var server = NewDiscoverySever(33227, 11111)
assert.Equal(t, ":33227", server.ListenString, "they should be equal")
}
func TestDiscoveryServer_BindPortOutOfRange(t *testing.T) {
var server = NewDiscoverySever(70000, 11111)
assert.Equal(t, ":32227", server.ListenString, "they should be equal")
}
func TestDiscoveryServer_BindPortOutOfRange2(t *testing.T) {
var server = NewDiscoverySever(0, 11111)
assert.Equal(t, ":32227", server.ListenString, "they should be equal")
}
func TestDiscoveryServer_AlpacaPortReply(t *testing.T) {
var server = NewDiscoverySever(32227, 11111)
assert.Equal(t, "{\n\"AlpacaPort\":11111\n}", server.composeDiscoveryReply(), "they should be equal")
}
func TestDiscoveryServer_AlpacaPortReplyCustom(t *testing.T) {
var server = NewDiscoverySever(32227, 80)
assert.Equal(t, "{\n\"AlpacaPort\":80\n}", server.composeDiscoveryReply(), "they should be equal")
}
func TestDiscoveryServer_AlpacaPortOutOfRange(t *testing.T) {
var server = NewDiscoverySever(32227, 70000)
assert.Equal(t, "{\n\"AlpacaPort\":11111\n}", server.composeDiscoveryReply(), "they should be equal")
}
func TestDiscoveryServer_AlpacaPortOutOfRange2(t *testing.T) {
var server = NewDiscoverySever(32227, 0)
assert.Equal(t, "{\n\"AlpacaPort\":11111\n}", server.composeDiscoveryReply(), "they should be equal")
}