forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
67 lines (57 loc) · 1.52 KB
/
message_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
package kafka
import (
"bufio"
"math/rand"
"testing"
"time"
)
func TestMessageSetReaderEmpty(t *testing.T) {
m := messageSetReader{empty: true}
noop := func(*bufio.Reader, int, int) (int, error) {
return 0, nil
}
offset, timestamp, headers, err := m.readMessage(0, noop, noop)
if offset != 0 {
t.Errorf("expected offset of 0, get %d", offset)
}
if timestamp != 0 {
t.Errorf("expected timestamp of 0, get %d", timestamp)
}
if headers != nil {
t.Errorf("expected nil headers, got %v", headers)
}
if err != RequestTimedOut {
t.Errorf("expected RequestTimedOut, got %v", err)
}
if m.remaining() != 0 {
t.Errorf("expected 0 remaining, got %d", m.remaining())
}
if m.discard() != nil {
t.Errorf("unexpected error from discard(): %v", m.discard())
}
}
func TestMessageSize(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for i := 0; i < 20; i++ {
t.Run("Run", func(t *testing.T) {
msg := Message{
Key: make([]byte, rand.Intn(200)),
Value: make([]byte, rand.Intn(200)),
Time: randate(),
}
expSize := msg.message(nil).size()
gotSize := msg.size()
if expSize != gotSize {
t.Errorf("Expected size %d, but got size %d", expSize, gotSize)
}
})
}
}
// https://stackoverflow.com/questions/43495745/how-to-generate-random-date-in-go-lang/43497333#43497333
func randate() time.Time {
min := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2070, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
delta := max - min
sec := rand.Int63n(delta) + min
return time.Unix(sec, 0)
}