-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_test.go
61 lines (50 loc) · 1.53 KB
/
link_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
package dndm
import (
"context"
"testing"
"time"
"github.com/itohio/dndm/testutil"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)
func Test_NewLink(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
defer cancel()
intent := &MockIntentInternal{}
interest := &MockInterestInternal{}
intent.On("Link", (chan<- proto.Message)(nil))
intent.On("Ctx").Return(ctx)
interest.On("Ctx").Return(ctx)
link := NewLink(ctx, intent, interest)
onClose := testutil.NewFunc(ctx, t, "close link")
link.AddOnClose(onClose.F)
assert.NotNil(t, link)
assert.Equal(t, intent, link.intent)
assert.Equal(t, interest, link.interest)
onClose.NotCalled()
}
func TestLink_Link(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
intent := &MockIntentInternal{}
interest := &MockInterestInternal{}
ch := make(chan proto.Message)
intent.On("Link", (chan<- proto.Message)(ch))
intent.On("Link", (chan<- proto.Message)(nil))
intent.On("Ctx").Return(ctx)
intent.On("Notify").Twice()
interest.On("MsgC").Return((chan<- proto.Message)(ch))
interest.On("Ctx").Return(ctx)
link := NewLink(ctx, intent, interest)
link.Link()
link.Notify()
onClose := testutil.NewFunc(ctx, t, "close link")
link.AddOnClose(onClose.F)
link.Close()
onClose.WaitCalled()
assert.True(t, testutil.CtxRecv(ctx, link.Ctx().Done()))
time.Sleep(time.Millisecond)
// Expect Link to have been called with msgC and nil
intent.AssertExpectations(t)
interest.AssertExpectations(t)
}