Debian Codegraph @

api/v0/detector/standalone_test.go

raw symbols SCIP
1 package detector
2
3 import (
4 "sync"
5 "testing"
6 "time"
7
8 "github.com/gogo/protobuf/proto"
9 mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
10 "github.com/mesos/mesos-go/api/v0/upid"
11 "github.com/stretchr/testify/assert"
12 "golang.org/x/net/context"
13 )
14
15 const (
16 localhost = uint32(2130706433) // packed uint32 for 127.0.0.1 IPv4
17 )
18
19 func TestStandalone_nil(t *testing.T) {
20 d := NewStandalone(nil)
21 select {
22 case <-d.Done(): // expected
23 t.Fatalf("expected detector to stay alive since we haven't done anything with it")
24 case <-time.After(500 * time.Millisecond):
25 }
26 d.Detect(nil)
27 select {
28 case <-d.Done(): // expected
29 case <-time.After(1 * time.Second):
30 t.Fatalf("expected detector to shutdown since it has no master")
31 }
32 }
33
34 func TestStandalone_pollerIncompleteInfo(t *testing.T) {
35 d := NewStandalone(&mesos.MasterInfo{})
36 f := fetcherFunc(func(context.Context, string) (*upid.UPID, error) {
37 return nil, nil
38 })
39 ch := make(chan struct{})
40 go func() {
41 defer close(ch)
42 d.poller(f)
43 }()
44 select {
45 case <-ch: // expected
46 case <-time.After(1 * time.Second):
47 t.Fatalf("expected poller to shutdown since master info is incomplete")
48 }
49 select {
50 case <-d.Done(): // expected
51 case <-time.After(1 * time.Second):
52 t.Fatalf("expected detector to shutdown since it has no master")
53 }
54 }
55
56 func TestStandalone_pollerFetched(t *testing.T) {
57 assert := assert.New(t)
58 // presence of IP address allows fecher to be called
59 d := NewStandalone(&mesos.MasterInfo{Ip: proto.Uint32(localhost)})
60 defer d.Cancel()
61
62 fetched := make(chan struct{})
63 pid := &upid.UPID{
64 ID: "[email protected]:5050",
65 Host: "127.0.0.1",
66 Port: "5050",
67 }
68 f := fetcherFunc(func(ctx context.Context, addr string) (*upid.UPID, error) {
69 defer close(fetched)
70 assert.Equal("127.0.0.1:5050", addr)
71 return pid, nil
72 })
73
74 go d.poller(f)
75
76 // fetch called
77 select {
78 case <-fetched: // expected
79 case <-time.After(1 * time.Second):
80 t.Fatalf("expected fetch")
81 }
82
83 // read MasterInfo
84 select {
85 case mi := <-d.ch:
86 assert.Equal(mi, CreateMasterInfo(pid))
87 case <-time.After(1 * time.Second):
88 t.Fatalf("expected poller to send master info")
89 }
90 }
91
92 func TestStandalone_pollerFetchedMulti(t *testing.T) {
93 assert := assert.New(t)
94 // presence of IP address allows fecher to be called
95 d := NewStandalone(&mesos.MasterInfo{Ip: proto.Uint32(localhost)})
96 defer d.Cancel()
97 d.leaderSyncInterval = 500 * time.Millisecond
98
99 i := 0
100 var wg sync.WaitGroup
101 wg.Add(4)
102 f := fetcherFunc(func(ctx context.Context, addr string) (*upid.UPID, error) {
103 defer func() { i++ }()
104 switch i {
105 case 0:
106 wg.Done()
107 assert.Equal("127.0.0.1:5050", addr)
108 return &upid.UPID{ID: "[email protected]:5050", Host: "127.0.0.1", Port: "5050"}, nil
109 case 1:
110 wg.Done()
111 assert.Equal("127.0.0.1:5050", addr)
112 return &upid.UPID{ID: "[email protected]:5050", Host: "127.0.0.2", Port: "5050"}, nil
113 case 2:
114 wg.Done()
115 return nil, context.DeadlineExceeded
116 case 3:
117 wg.Done()
118 assert.Equal("127.0.0.1:5050", addr)
119 return &upid.UPID{ID: "[email protected]:5050", Host: "127.0.0.3", Port: "5050"}, nil
120 default:
121 d.Cancel()
122 return nil, context.Canceled
123 }
124 })
125
126 go d.poller(f)
127
128 // fetches complete
129 ch := make(chan struct{})
130 go func() {
131 defer close(ch)
132 wg.Wait()
133 }()
134
135 changed := make(chan struct{})
136 go func() {
137 defer close(changed)
138 for i := 0; i < 4; i++ {
139 if mi, ok := <-d.ch; !ok {
140 t.Fatalf("failed to read master info on cycle %v", i)
141 break
142 } else {
143 switch i {
144 case 0:
145 assert.Equal(CreateMasterInfo(&upid.UPID{ID: "[email protected]:5050", Host: "127.0.0.1", Port: "5050"}), mi)
146 case 1:
147 assert.Equal(CreateMasterInfo(&upid.UPID{ID: "[email protected]:5050", Host: "127.0.0.2", Port: "5050"}), mi)
148 case 2:
149 assert.Nil(mi)
150 case 3:
151 assert.Equal(CreateMasterInfo(&upid.UPID{ID: "[email protected]:5050", Host: "127.0.0.3", Port: "5050"}), mi)
152 }
153 }
154 }
155 }()
156
157 started := time.Now()
158 select {
159 case <-ch: // expected
160 case <-time.After(3 * time.Second):
161 t.Fatalf("expected fetches all complete")
162 }
163
164 select {
165 case <-changed: // expected
166 case <-time.After((3 * time.Second) - time.Now().Sub(started)):
167 t.Fatalf("expected to have received all master info changes")
168 }
169 }