-
Notifications
You must be signed in to change notification settings - Fork 0
/
interest_test.go
244 lines (202 loc) · 6.5 KB
/
interest_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package dndm
import (
"context"
"testing"
"time"
"github.com/itohio/dndm/testutil"
testtypes "github.com/itohio/dndm/types/test"
"google.golang.org/protobuf/proto"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func recvChan[T any](c <-chan T, t time.Duration) (T, error) {
ctx, cancel := context.WithTimeout(context.Background(), t)
defer cancel()
select {
case <-ctx.Done():
case v := <-c:
return v, nil
}
var z T
return z, ctx.Err()
}
func recvChan1[T any](c <-chan T, t time.Duration) error {
_, err := recvChan(c, t)
return err
}
func sendChan[T any](c chan<- T, v T, t time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), t)
defer cancel()
select {
case <-ctx.Done():
case c <- v:
return nil
}
return ctx.Err()
}
func TestNewInterest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
route, err := NewRoute("path", &testtypes.Foo{})
require.NoError(t, err)
interest := NewInterest(ctx, route, 10)
require.NotNil(t, interest)
onClose := testutil.NewFunc(ctx, t, "close interest")
interest.OnClose(nil)
interest.OnClose(onClose.F)
onClose.NotCalled()
assert.Equal(t, route, interest.Route())
assert.Equal(t, 10, cap(interest.MsgC()))
assert.NotNil(t, interest.C())
onClose.NotCalled()
require.NoError(t, interest.Close())
onClose.WaitCalled()
assert.True(t, testutil.CtxRecv(ctx, interest.C()))
}
func TestLocalInterest_ConcurrentAccess(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
route, err := NewRoute("path", &testtypes.Foo{})
require.NoError(t, err)
interest := NewInterest(ctx, route, 1000)
done1 := make(chan bool)
done2 := make(chan bool)
// Concurrent sending to MsgC
go func() {
for i := 0; i < 100; i++ {
select {
case <-interest.Ctx().Done():
close(done1)
return
case interest.MsgC() <- &testtypes.Foo{Text: "Assume this is correctly constructed"}:
}
}
close(done1)
}()
// Concurrent closing
go func() {
<-done1
interest.Close()
close(done2)
}()
assert.True(t, testutil.CtxRecv(ctx, done1))
assert.True(t, testutil.CtxRecv(ctx, done2))
require.Equal(t, context.Canceled, interest.Ctx().Err())
}
// ========== Interest router tests ==========
func TestInterestRouter_CreationAndOperation(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
route, err := NewRoute("path", &testtypes.Foo{})
require.NoError(t, err)
// // Mocking Interest for testing
mockInterest := &MockInterest{}
mockInterest.On("Route").Return(route)
ch := make(chan proto.Message, 10)
mockInterest.On("C").Return((<-chan proto.Message)(ch)) // Assuming a channel is returned here
mockInterest.On("OnClose", mock.Anything).Return(mockInterest)
router, err := NewInterestRouter(ctx, route, 10, mockInterest)
require.NoError(t, err)
require.NotNil(t, router)
assert.Equal(t, router.Route(), route)
router.OnClose(nil)
onClose := testutil.NewFunc(ctx, t, "close router")
router.OnClose(onClose.F)
w := router.Wrap()
assert.Equal(t, router, w.router)
assert.Equal(t, router.size, cap(w.c))
assert.Equal(t, w.Route(), route)
// Testing RemoveInterest
router.RemoveInterest(mockInterest)
// Ensure that interest is no longer in the router
assert.NotContains(t, router.interests, mockInterest)
assert.NoError(t, router.Close())
onClose.WaitCalled()
time.Sleep(time.Millisecond)
mockInterest.AssertExpectations(t)
}
func TestInterestWrapperOperations(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
route, err := NewRoute("path", &testtypes.Foo{})
require.NoError(t, err)
router, _ := NewInterestRouter(ctx, route, 10)
wrapper := router.Wrap()
// Test route retrieval and close operation
assert.Equal(t, route, wrapper.Route())
assert.Contains(t, router.wrappers, wrapper)
assert.NoError(t, wrapper.Close())
// Ensure the channel is properly closed after wrapper.Close()
_, ok := <-wrapper.C()
assert.False(t, ok)
assert.NotContains(t, router.wrappers, wrapper)
}
func TestInterestRouter_Close(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
route, err := NewRoute("path", &testtypes.Foo{})
require.NoError(t, err)
onClose := testutil.NewFunc(ctx, t, "close router")
router, _ := NewInterestRouter(ctx, route, 10)
router.OnClose(onClose.F)
assert.NoError(t, router.Close())
onClose.WaitCalled()
// Ensure the main message channel is closed
assert.True(t, testutil.CtxRecv(ctx, router.C()))
}
func TestInterestRouter_MessageRouting(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
route, err := NewRoute("path", &testtypes.Foo{})
require.NoError(t, err)
router, _ := NewInterestRouter(ctx, route, 10)
mockInterest1 := &MockInterest{}
mockInterest1.On("Route").Return(route)
ch1 := make(chan proto.Message, 10)
mockInterest1.On("C").Return((<-chan proto.Message)(ch1)) // Assuming a channel is returned here
mockInterest1.On("OnClose", mock.Anything).Return(mockInterest1)
msg1 := &testtypes.Foo{}
mockInterest2 := &MockInterest{}
mockInterest2.On("Route").Return(route)
ch2 := make(chan proto.Message, 10)
mockInterest2.On("C").Return((<-chan proto.Message)(ch2)) // Assuming a channel is returned here
mockInterest2.On("OnClose", mock.Anything).Return(mockInterest2)
msg2 := &testtypes.Foo{Text: "msg2"}
t.Log("Register 1st wrapper")
w1 := router.Wrap()
w1Done := make(chan struct{})
go func() {
receivedMsg := []proto.Message{<-w1.C(), <-w1.C()}
assert.Contains(t, receivedMsg, msg1)
assert.Contains(t, receivedMsg, msg2)
close(w1Done)
}()
t.Log("register interest")
router.AddInterest(mockInterest1)
router.AddInterest(mockInterest2)
t.Log("register second wrapper")
w2 := router.Wrap()
w2Done := make(chan struct{})
go func() {
receivedMsg := []proto.Message{<-w2.C(), <-w2.C()}
assert.Contains(t, receivedMsg, msg1)
assert.Contains(t, receivedMsg, msg2)
close(w2Done)
}()
t.Log("Simulate message reception and routing")
go func() {
ch1 <- msg1
ch2 <- msg2
}()
testutil.CtxRecv(ctx, w1Done)
testutil.CtxRecv(ctx, w2Done)
w2.Close()
assert.False(t, testutil.IsClosed(router.Ctx().Done()))
w1.Close()
assert.True(t, testutil.CtxRecv(ctx, router.Ctx().Done()))
<-router.Ctx().Done()
assert.Equal(t, context.Canceled, router.Ctx().Err())
mockInterest1.AssertExpectations(t)
mockInterest2.AssertExpectations(t)
}