-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_test.go
89 lines (75 loc) · 1.53 KB
/
create_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
package pubsubetcd
import (
"testing"
"time"
"github.com/google/uuid"
"go.etcd.io/etcd/clientv3"
)
func GetUuid() string {
u, _ := uuid.NewUUID()
return u.String()
}
type TestCase struct {
topic string
shouldFail bool
}
func NewTopicCase() TestCase {
return TestCase{
topic: GetUuid(),
shouldFail: false,
}
}
var testCases = []struct {
topic string
shouldFail bool
}{
// Invalid, bad name
{"abc$", true},
// Valid, new topic is created
{"abc", false},
{"def", false},
{GetUuid(), false},
}
func TestCreateGet(t *testing.T) {
// Connect to etcd
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:2379"},
DialTimeout: time.Duration(5) * time.Second,
})
if err != nil {
t.Errorf("%w", err)
}
// Create topics for tests that should succeed
for _, tc := range testCases {
if !tc.shouldFail {
CreateTopic(cli, tc.topic, 10)
}
}
// Get created topics. If the topic does not exist, an error is expected.
for _, tc := range testCases {
_, err = GetTopic(cli, tc.topic)
if tc.shouldFail && err == nil {
t.Errorf("%w", err)
}
}
}
func BenchmarkCreateGet(b *testing.B) {
// Connect to etcd
etcd, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:2379"},
DialTimeout: time.Duration(5) * time.Second,
})
if err != nil {
b.Errorf("%w", err)
}
for i := 0; i < b.N; i++ {
// Run and log tests
tc := NewTopicCase()
_, err = CreateTopic(etcd, tc.topic, 10)
if err != nil {
if !tc.shouldFail {
b.Errorf("%w", err)
}
}
}
}