-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrev_test.go
101 lines (87 loc) · 2.1 KB
/
rev_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
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
package main
import (
"fmt"
"net"
"testing"
"time"
)
func TestOutOfOrderDetector_CheckMessage(t *testing.T) {
tests := []struct {
messages []string
expectedMissed int
expectedReceived int
}{
{
messages: []string{"SequenceNumber:1", "SequenceNumber:2", "SequenceNumber:3"},
expectedMissed: 0,
expectedReceived: 3,
},
{
messages: []string{"SequenceNumber:1", "SequenceNumber:3"},
expectedMissed: 1,
expectedReceived: 2,
},
{
messages: []string{"SequenceNumber:1", "SequenceNumber:5"},
expectedMissed: 3,
expectedReceived: 2,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("Case%d", i+1), func(t *testing.T) {
detector := NewOutOfOrderDetector()
missedCount := 0
for _, msg := range tt.messages {
detector.CheckMessage(msg)
}
// Calculate the number of missed messages
missedCount = detector.expectedSeqNum - tt.expectedReceived - 1
if missedCount != tt.expectedMissed {
t.Errorf("Expected %d missed messages, but got %d", tt.expectedMissed, missedCount)
}
})
}
}
func TestReceiver_Start(t *testing.T) {
address := "224.0.0.1:9999"
receiver, err := NewReceiver(address)
if err != nil {
t.Fatal("Error creating receiver:", err)
}
receivedCh := make(chan string, 10)
// Run Receiver in a goroutine
go func() {
buf := make([]byte, 1024)
n, _, err := receiver.conn.ReadFromUDP(buf)
if err != nil {
t.Error("Error reading:", err)
return
}
receivedCh <- string(buf[:n])
}()
// Wait for Receiver to start receiving
time.Sleep(100 * time.Millisecond)
// Send data to the multicast address
go func() {
conn, err := net.Dial("udp", address)
if err != nil {
t.Error("Error reading:", err)
return
}
defer conn.Close()
_, err = conn.Write([]byte("Test Message"))
if err != nil {
t.Error("Error reading:", err)
return
}
}()
select {
case received := <-receivedCh:
if received != "Test Message" {
t.Errorf("Expected 'Test Message', got '%s'", received)
}
case <-time.After(1 * time.Second):
t.Error("Timeout waiting for message")
}
receiver.Stop()
}