-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpacket_test.go
70 lines (55 loc) · 1.76 KB
/
packet_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
package reliable
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/valyala/bytebufferpool"
"math"
"testing"
"testing/quick"
)
func TestEncodeDecodePacketHeader(t *testing.T) {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
f := func(seq, ack uint16, ackBits uint32) bool {
header := PacketHeader{Sequence: seq, ACK: ack, ACKBits: ackBits}
recovered, leftover, err := UnmarshalPacketHeader(header.AppendTo(buf.B[:0]))
return assert.NoError(t, err) && assert.Len(t, leftover, 0) && assert.EqualValues(t, header, recovered)
}
require.NoError(t, quick.Check(f, &quick.Config{MaxCount: 1000}))
}
func BenchmarkMarshalPacketHeader(b *testing.B) {
header := PacketHeader{Sequence: math.MaxUint16, ACK: math.MaxUint16, ACKBits: math.MaxUint32}
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.B = header.AppendTo(buf.B[:0])
}
}
func BenchmarkUnmarshalPacketHeader(b *testing.B) {
header := PacketHeader{Sequence: math.MaxUint16, ACK: math.MaxUint16, ACKBits: math.MaxUint32}
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
buf.B = header.AppendTo(buf.B)
var (
recovered PacketHeader
leftover []byte
err error
)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
recovered, leftover, err = UnmarshalPacketHeader(buf.B)
if err != nil {
b.Fatalf("failed to unmarshal packet header: %s", err)
}
if leftover := len(leftover); leftover != 0 {
b.Fatalf("got %d byte(s) leftover", leftover)
}
if recovered.Sequence != header.Sequence || recovered.ACK != header.ACK || recovered.ACKBits != header.ACKBits {
b.Fatalf("got %#v, expected %#v", recovered, header)
}
}
_ = recovered
}