-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscriber_test.go
47 lines (39 loc) · 898 Bytes
/
subscriber_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
package noise
import (
"context"
"testing"
"time"
)
func TestSubscriberListen(t *testing.T) {
sub := newSubscriber()
session := mockSession(&mockConn{}, nil)
header := header{newPeer(session), NewPeerDetected}
signaling := Signal{header, ""}
canceled := make(chan struct{})
msg := make(chan Signal)
ctx, cancel := context.WithCancel(context.Background())
go func() {
// wait for new message emitted then cancel listening
<-msg
cancel()
}()
go func() {
// after stop listening loop expect trigger canceled
sub.Listen(ctx, msg)
canceled <- struct{}{}
}()
go func() {
// send message after 1 second
time.Sleep(1 * time.Second)
sub.Emit(signaling)
}()
// First to finish wins
select {
case <-canceled:
return
case <-time.After(3 * time.Second):
// Wait 1 second to receive message
t.Error("expected canceled listening after emit")
t.FailNow()
}
}