This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mux_test.go
141 lines (129 loc) · 3.07 KB
/
mux_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package quark
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewMux(t *testing.T) {
t.Run("New mux", func(t *testing.T) {
mux := NewMux()
assert.NotNil(t, mux)
})
}
var defaultMuxAddTestingSuite = []struct {
c *Consumer
expected int
}{
{c: &Consumer{topics: []string{"alex.trades", "bob.trades"}}, expected: 2},
{c: &Consumer{topics: []string{"alex.trades"}}, expected: 1},
{c: nil, expected: 0},
}
func TestDefaultMux_Add(t *testing.T) {
for _, tt := range defaultMuxAddTestingSuite {
t.Run("Default mux add", func(t *testing.T) {
mux := NewMux()
mux.Add(tt.c)
assert.Equal(t, tt.expected, len(mux.List()))
})
}
}
var defaultMuxTopicTestingSuite = []struct {
t string
}{
{"bob.notification"},
{""},
}
func TestDefaultMux_Topic(t *testing.T) {
for _, tt := range defaultMuxTopicTestingSuite {
t.Run("Default mux add topic", func(t *testing.T) {
mux := NewMux()
mux.Topic(tt.t)
if tt.t == "" {
assert.Nil(t, mux.Get(tt.t))
return
}
assert.NotNil(t, mux.Get(tt.t))
})
}
}
var defaultMuxContainsTestingSuite = []struct {
t []string
f string
expected bool
}{
{[]string{"bob.notification", "alice.trades", "john.products"}, "alice.trades", true},
{[]string{"bob.notification", "john.products"}, "alice.trades", false},
{[]string{}, "alice.trades", false},
}
func TestDefaultMux_Contains(t *testing.T) {
for _, tt := range defaultMuxContainsTestingSuite {
t.Run("Default mux contains", func(t *testing.T) {
mux := NewMux()
mux.Topics(tt.t...)
assert.Equal(t, tt.expected, mux.Contains(tt.f))
})
}
}
var defaultMuxDelTestingSuite = []struct {
t []string
d string
expected int
}{
{[]string{"bob.notification", "alice.trades", "john.products"}, "alice.trades", 2},
{[]string{"bob.notification", "john.products"}, "alice.trades", 2},
{[]string{}, "alice.trades", 0},
}
func TestDefaultMux_Del(t *testing.T) {
for _, tt := range defaultMuxDelTestingSuite {
t.Run("Default mux delete", func(t *testing.T) {
mux := NewMux()
mux.Topics(tt.t...)
mux.Del(tt.d)
assert.Equal(t, tt.expected, len(mux.List()))
})
}
}
func BenchmarkMux(b *testing.B) {
mux := NewMux()
mux.Topics("foo", "bar", "baz")
b.Run("New Event Mux", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = NewMux()
}
})
b.Run("Event Mux data insertion", func(b *testing.B) {
b.ReportAllocs()
mux := NewMux()
for i := 0; i < b.N; i++ {
mux.Topics("foo", "bar", "baz")
}
})
b.Run("Event Mux single data insertion", func(b *testing.B) {
b.ReportAllocs()
mux := NewMux()
for i := 0; i < b.N; i++ {
mux.Topic("foo")
}
})
b.Run("Event Mux data removal", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mux.Del("baz")
}
})
b.Run("Event Mux contains item", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mux.Contains("baz")
}
})
b.Run("Event Mux data manipulation", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
mux.Get("baz")
mux.Contains("bar")
mux.List()
mux.Del("baz")
}
})
}