forked from ninibe/netlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic_test.go
100 lines (81 loc) · 1.95 KB
/
topic_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package netlog
import (
"testing"
"time"
)
func TestTopicCreateGetDelete(t *testing.T) {
t.Parallel()
nl := tempNetLog()
topicName := randStr(6)
top, err := nl.Topic(topicName)
if err != ErrTopicNotFound || top != nil {
t.Error("Non existing topic should have been not found")
}
top, err = nl.CreateTopic(topicName, TopicSettings{})
if err != nil {
t.Fatal(err)
}
top2, err := nl.Topic(topicName)
if err != nil {
t.Error(err)
}
if top != top2 {
t.Error("failed to retrieve topic by name")
}
err = nl.DeleteTopic(topicName, false)
if err != nil {
t.Error(err)
}
top, err = nl.Topic(topicName)
if err != ErrTopicNotFound || top != nil {
t.Error("Invalid topic should have been not found")
}
}
func TestParseOffset(t *testing.T) {
t.Parallel()
var parseTests = []struct {
str string
offset int64
err error
}{
{"3", 3, nil},
{"first", 0, nil},
{"start", 0, nil},
{"beginning", 0, nil},
{"oldest", 0, nil},
{"last", 19, nil},
{"latest", 19, nil},
{"end", 20, nil},
{"now", 20, nil},
{"", 0, nil},
{"2a2", -1, ErrInvalidOffset},
{"1s", 10, nil},
{"10s", 0, nil},
}
nl := tempNetLog()
top, err := nl.CreateTopic(randStr(6), TopicSettings{})
panicOn(err)
for i := 1; i <= 10; i++ {
_, err := top.Write(randData(1000))
panicOn(err)
}
time.Sleep(2 * time.Second)
for i := 1; i <= 10; i++ {
_, err := top.Write(randData(1000))
panicOn(err)
}
err = top.bl.Sync()
panicOn(err)
for _, tt := range parseTests {
offset, err := top.ParseOffset(tt.str)
if offset != tt.offset {
t.Errorf("invalid parsed offset %q Expected: %d Actual: %d", tt.str, tt.offset, offset)
}
if err != tt.err {
t.Errorf("invalid parsed offset error %q Expected: %s Actual: %s", tt.str, tt.err, err)
}
}
}