dissertation-2-code/udp/flow_test.go

87 lines
1.9 KiB
Go
Raw Normal View History

2020-11-27 20:17:59 +00:00
package udp
import (
2021-03-30 20:57:53 +01:00
"context"
2020-11-27 20:17:59 +00:00
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"mpbl3p/mocks"
"mpbl3p/proxy"
"mpbl3p/udp/congestion"
"testing"
"time"
)
func TestFlow_Consume(t *testing.T) {
testContent := []byte("A test string is the content of this packet.")
2020-11-28 17:15:56 +00:00
testPacket := proxy.SimplePacket(testContent)
2020-11-27 20:17:59 +00:00
testMac := mocks.AlmostUselessMac{}
t.Run("Length", func(t *testing.T) {
testConn := mocks.NewMockPerfectBiPacketConn(10)
flowA := newFlow(congestion.NewNone(), testMac)
flowA.writer = testConn.SideB()
flowA.isAlive = true
2021-03-30 20:57:53 +01:00
err := flowA.Consume(context.Background(), testPacket, testMac)
2020-11-27 20:17:59 +00:00
require.Nil(t, err)
buf := make([]byte, 100)
n, _, err := testConn.SideA().ReadFromUDP(buf)
require.Nil(t, err)
// 12 header, 8 timestamp, 4 MAC
assert.Equal(t, len(testContent)+12+8+4, n)
})
}
func TestFlow_Produce(t *testing.T) {
testContent := []byte("A test string is the content of this packet.")
testPacket := Packet{
ack: 42,
nack: 26,
seq: 128,
2020-11-28 17:15:56 +00:00
data: proxy.SimplePacket(testContent),
2020-11-27 20:17:59 +00:00
}
testMac := mocks.AlmostUselessMac{}
testMarshalled := proxy.AppendMac(testPacket.Marshal(), testMac)
t.Run("Length", func(t *testing.T) {
done := make(chan struct{})
go func() {
testConn := mocks.NewMockPerfectBiPacketConn(10)
_, err := testConn.SideA().Write(testMarshalled)
require.Nil(t, err)
flowA := newFlow(congestion.NewNone(), testMac)
flowA.writer = testConn.SideB()
flowA.isAlive = true
go func() {
2021-03-30 20:57:53 +01:00
err := flowA.readQueuePacket(context.Background(), testConn.SideB())
2020-11-27 20:17:59 +00:00
assert.Nil(t, err)
}()
2021-03-30 20:57:53 +01:00
p, err := flowA.Produce(context.Background(), testMac)
2020-11-27 20:17:59 +00:00
require.Nil(t, err)
assert.Len(t, p.Contents(), len(testContent))
done <- struct{}{}
}()
timer := time.NewTimer(500 * time.Millisecond)
select {
case <-done:
case <-timer.C:
fmt.Println("timed out")
t.FailNow()
}
})
}