-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathartnet_test.go
More file actions
109 lines (86 loc) · 2.41 KB
/
Copy pathartnet_test.go
File metadata and controls
109 lines (86 loc) · 2.41 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
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
package dmxcast
import (
"net"
"testing"
"time"
"github.com/jsimonetti/go-artnet/packet"
"github.com/stretchr/testify/require"
)
func listenUDP1270(t *testing.T) *net.UDPConn {
t.Helper()
c, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 0})
require.NoError(t, err)
return c
}
func recvArtDMX(t *testing.T, c *net.UDPConn) packet.ArtDMXPacket {
t.Helper()
_ = c.SetReadDeadline(time.Now().Add(300 * time.Millisecond))
buf := make([]byte, 2048)
n, _, err := c.ReadFromUDP(buf)
require.NoError(t, err)
var p packet.ArtDMXPacket
require.NoError(t, p.UnmarshalBinary(buf[:n]))
return p
}
func TestArtNetTransport_Send_UsesMapAndSendsPacket(t *testing.T) {
rx := listenUDP1270(t)
defer rx.Close()
txConn := listenUDP1270(t)
defer txConn.Close()
cfg := &ArtNetConfig{
DstIP: net.ParseIP("127.0.0.1"),
SubUni: 204,
}
dst := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: rx.LocalAddr().(*net.UDPAddr).Port}
tx := newArtNetTransportConn(cfg, txConn, dst)
var dmx [512]byte
dmx[0] = 1
dmx[1] = 2
dmx[2] = 3
dmx[511] = 255
require.NoError(t, tx.Send(dmx))
p := recvArtDMX(t, rx)
require.Equal(t, uint8(1), p.Sequence)
require.Equal(t, uint8(204), p.SubUni)
require.Equal(t, uint8(0), p.Net)
require.Equal(t, dmx, p.Data)
}
func TestArtNetTransport_Sequence_WrapsSkippingZero(t *testing.T) {
rx := listenUDP1270(t)
defer rx.Close()
txConn := listenUDP1270(t)
defer txConn.Close()
cfg := &ArtNetConfig{
DstIP: net.ParseIP("127.0.0.1"),
SubUni: 204,
}
dst := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: rx.LocalAddr().(*net.UDPAddr).Port}
tx := newArtNetTransportConn(cfg, txConn, dst)
var dmx [512]byte
dmx[0] = 123
// Drive sequence wrap: 255 increments -> wraps to 0 internally, then skip to 1.
// We check the first send and the 256th send.
var firstSeq, lastSeq uint8
for i := range 256 {
require.NoError(t, tx.Send(dmx))
p := recvArtDMX(t, rx)
if i == 0 {
firstSeq = p.Sequence
}
if i == 255 {
lastSeq = p.Sequence
}
}
require.Equal(t, uint8(1), firstSeq)
require.Equal(t, uint8(1), lastSeq)
}
// newArtNetTransportConn is for tests: it uses an existing UDPConn.
// dstAddr.Port can be set to something other than ArtNetPort.
func newArtNetTransportConn(cfg *ArtNetConfig, conn *net.UDPConn, dstAddr *net.UDPAddr) *ArtNetTransport {
return &ArtNetTransport{
cfg: cfg,
conn: conn,
dstAddr: dstAddr,
seq: 1,
}
}