@@ -3,14 +3,14 @@ package ae
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "log"
7
- "os"
8
6
"reflect"
9
7
"sync"
10
8
"testing"
11
9
"time"
12
10
13
11
"github.com/hashicorp/consul/lib"
12
+ "github.com/hashicorp/consul/sdk/testutil"
13
+ "github.com/hashicorp/go-hclog"
14
14
"github.com/stretchr/testify/assert"
15
15
)
16
16
@@ -86,7 +86,7 @@ func TestAE_staggerDependsOnClusterSize(t *testing.T) {
86
86
libRandomStagger = func (d time.Duration ) time.Duration { return d }
87
87
defer func () { libRandomStagger = lib .RandomStagger }()
88
88
89
- l := testSyncer ()
89
+ l := testSyncer (t )
90
90
if got , want := l .staggerFn (10 * time .Millisecond ), 10 * time .Millisecond ; got != want {
91
91
t .Fatalf ("got %v want %v" , got , want )
92
92
}
@@ -106,7 +106,7 @@ func TestAE_Run_SyncFullBeforeChanges(t *testing.T) {
106
106
}
107
107
108
108
// indicate that we have partial changes before starting Run
109
- l := testSyncer ()
109
+ l := testSyncer (t )
110
110
l .State = state
111
111
l .ShutdownCh = shutdownCh
112
112
l .SyncChanges .Trigger ()
@@ -132,15 +132,15 @@ func TestAE_Run_Quit(t *testing.T) {
132
132
t .Fatal ("Run should panic" )
133
133
}
134
134
}()
135
- l := testSyncer ()
135
+ l := testSyncer (t )
136
136
l .ClusterSize = nil
137
137
l .Run ()
138
138
})
139
139
t .Run ("runFSM quits" , func (t * testing.T ) {
140
140
// start timer which explodes if runFSM does not quit
141
141
tm := time .AfterFunc (time .Second , func () { panic ("timeout" ) })
142
142
143
- l := testSyncer ()
143
+ l := testSyncer (t )
144
144
l .runFSM (fullSyncState , func (fsmState ) fsmState { return doneState })
145
145
// should just quit
146
146
tm .Stop ()
@@ -150,23 +150,23 @@ func TestAE_Run_Quit(t *testing.T) {
150
150
func TestAE_FSM (t * testing.T ) {
151
151
t .Run ("fullSyncState" , func (t * testing.T ) {
152
152
t .Run ("Paused -> retryFullSyncState" , func (t * testing.T ) {
153
- l := testSyncer ()
153
+ l := testSyncer (t )
154
154
l .Pause ()
155
155
fs := l .nextFSMState (fullSyncState )
156
156
if got , want := fs , retryFullSyncState ; got != want {
157
157
t .Fatalf ("got state %v want %v" , got , want )
158
158
}
159
159
})
160
160
t .Run ("SyncFull() error -> retryFullSyncState" , func (t * testing.T ) {
161
- l := testSyncer ()
161
+ l := testSyncer (t )
162
162
l .State = & mock {syncFull : func () error { return errors .New ("boom" ) }}
163
163
fs := l .nextFSMState (fullSyncState )
164
164
if got , want := fs , retryFullSyncState ; got != want {
165
165
t .Fatalf ("got state %v want %v" , got , want )
166
166
}
167
167
})
168
168
t .Run ("SyncFull() OK -> partialSyncState" , func (t * testing.T ) {
169
- l := testSyncer ()
169
+ l := testSyncer (t )
170
170
l .State = & mock {}
171
171
fs := l .nextFSMState (fullSyncState )
172
172
if got , want := fs , partialSyncState ; got != want {
@@ -178,7 +178,7 @@ func TestAE_FSM(t *testing.T) {
178
178
t .Run ("retryFullSyncState" , func (t * testing.T ) {
179
179
// helper for testing state transitions from retrySyncFullState
180
180
test := func (ev event , to fsmState ) {
181
- l := testSyncer ()
181
+ l := testSyncer (t )
182
182
l .retrySyncFullEvent = func () event { return ev }
183
183
fs := l .nextFSMState (retryFullSyncState )
184
184
if got , want := fs , to ; got != want {
@@ -208,7 +208,7 @@ func TestAE_FSM(t *testing.T) {
208
208
t .Run ("partialSyncState" , func (t * testing.T ) {
209
209
// helper for testing state transitions from partialSyncState
210
210
test := func (ev event , to fsmState ) {
211
- l := testSyncer ()
211
+ l := testSyncer (t )
212
212
l .syncChangesEvent = func () event { return ev }
213
213
fs := l .nextFSMState (partialSyncState )
214
214
if got , want := fs , to ; got != want {
@@ -225,7 +225,7 @@ func TestAE_FSM(t *testing.T) {
225
225
test (syncFullTimerEvent , fullSyncState )
226
226
})
227
227
t .Run ("syncChangesEvent+Paused -> partialSyncState" , func (t * testing.T ) {
228
- l := testSyncer ()
228
+ l := testSyncer (t )
229
229
l .Pause ()
230
230
l .syncChangesEvent = func () event { return syncChangesNotifEvent }
231
231
fs := l .nextFSMState (partialSyncState )
@@ -234,7 +234,7 @@ func TestAE_FSM(t *testing.T) {
234
234
}
235
235
})
236
236
t .Run ("syncChangesEvent+SyncChanges() error -> partialSyncState" , func (t * testing.T ) {
237
- l := testSyncer ()
237
+ l := testSyncer (t )
238
238
l .State = & mock {syncChanges : func () error { return errors .New ("boom" ) }}
239
239
l .syncChangesEvent = func () event { return syncChangesNotifEvent }
240
240
fs := l .nextFSMState (partialSyncState )
@@ -243,7 +243,7 @@ func TestAE_FSM(t *testing.T) {
243
243
}
244
244
})
245
245
t .Run ("syncChangesEvent+SyncChanges() OK -> partialSyncState" , func (t * testing.T ) {
246
- l := testSyncer ()
246
+ l := testSyncer (t )
247
247
l .State = & mock {}
248
248
l .syncChangesEvent = func () event { return syncChangesNotifEvent }
249
249
fs := l .nextFSMState (partialSyncState )
@@ -268,14 +268,14 @@ func TestAE_FSM(t *testing.T) {
268
268
t .Fatal ("invalid state should panic" )
269
269
}
270
270
}()
271
- l := testSyncer ()
271
+ l := testSyncer (t )
272
272
l .nextFSMState (fsmState ("invalid" ))
273
273
})
274
274
}
275
275
276
276
func TestAE_RetrySyncFullEvent (t * testing.T ) {
277
277
t .Run ("trigger shutdownEvent" , func (t * testing.T ) {
278
- l := testSyncer ()
278
+ l := testSyncer (t )
279
279
l .ShutdownCh = make (chan struct {})
280
280
evch := make (chan event )
281
281
go func () { evch <- l .retrySyncFullEvent () }()
@@ -285,7 +285,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
285
285
}
286
286
})
287
287
t .Run ("trigger shutdownEvent during FullNotif" , func (t * testing.T ) {
288
- l := testSyncer ()
288
+ l := testSyncer (t )
289
289
l .ShutdownCh = make (chan struct {})
290
290
evch := make (chan event )
291
291
go func () { evch <- l .retrySyncFullEvent () }()
@@ -297,7 +297,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
297
297
}
298
298
})
299
299
t .Run ("trigger syncFullNotifEvent" , func (t * testing.T ) {
300
- l := testSyncer ()
300
+ l := testSyncer (t )
301
301
l .serverUpInterval = 10 * time .Millisecond
302
302
evch := make (chan event )
303
303
go func () { evch <- l .retrySyncFullEvent () }()
@@ -307,7 +307,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
307
307
}
308
308
})
309
309
t .Run ("trigger syncFullTimerEvent" , func (t * testing.T ) {
310
- l := testSyncer ()
310
+ l := testSyncer (t )
311
311
l .retryFailInterval = 10 * time .Millisecond
312
312
evch := make (chan event )
313
313
go func () { evch <- l .retrySyncFullEvent () }()
@@ -319,7 +319,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
319
319
320
320
func TestAE_SyncChangesEvent (t * testing.T ) {
321
321
t .Run ("trigger shutdownEvent" , func (t * testing.T ) {
322
- l := testSyncer ()
322
+ l := testSyncer (t )
323
323
l .ShutdownCh = make (chan struct {})
324
324
evch := make (chan event )
325
325
go func () { evch <- l .syncChangesEvent () }()
@@ -329,7 +329,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
329
329
}
330
330
})
331
331
t .Run ("trigger shutdownEvent during FullNotif" , func (t * testing.T ) {
332
- l := testSyncer ()
332
+ l := testSyncer (t )
333
333
l .ShutdownCh = make (chan struct {})
334
334
evch := make (chan event )
335
335
go func () { evch <- l .syncChangesEvent () }()
@@ -341,7 +341,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
341
341
}
342
342
})
343
343
t .Run ("trigger syncFullNotifEvent" , func (t * testing.T ) {
344
- l := testSyncer ()
344
+ l := testSyncer (t )
345
345
l .serverUpInterval = 10 * time .Millisecond
346
346
evch := make (chan event )
347
347
go func () { evch <- l .syncChangesEvent () }()
@@ -351,7 +351,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
351
351
}
352
352
})
353
353
t .Run ("trigger syncFullTimerEvent" , func (t * testing.T ) {
354
- l := testSyncer ()
354
+ l := testSyncer (t )
355
355
l .Interval = 10 * time .Millisecond
356
356
evch := make (chan event )
357
357
go func () { evch <- l .syncChangesEvent () }()
@@ -360,7 +360,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
360
360
}
361
361
})
362
362
t .Run ("trigger syncChangesNotifEvent" , func (t * testing.T ) {
363
- l := testSyncer ()
363
+ l := testSyncer (t )
364
364
evch := make (chan event )
365
365
go func () { evch <- l .syncChangesEvent () }()
366
366
l .SyncChanges .Trigger ()
@@ -391,8 +391,12 @@ func (m *mock) SyncChanges() error {
391
391
return nil
392
392
}
393
393
394
- func testSyncer () * StateSyncer {
395
- logger := log .New (os .Stderr , "" , 0 )
394
+ func testSyncer (t * testing.T ) * StateSyncer {
395
+ logger := hclog .New (& hclog.LoggerOptions {
396
+ Level : 0 ,
397
+ Output : testutil .TestWriter (t ),
398
+ })
399
+
396
400
l := NewStateSyncer (nil , time .Second , nil , logger )
397
401
l .stagger = func (d time.Duration ) time.Duration { return d }
398
402
l .ClusterSize = func () int { return 1 }
0 commit comments