Skip to content

Commit 476e0bf

Browse files
committed
Fix wait for implementation
1 parent eff3ea0 commit 476e0bf

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

internal/datasystem/fdv2_datasystem.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,22 @@ func (d *dataStatusProvider) WaitFor(desiredState interfaces.DataSourceState, ti
498498
ch := d.AddStatusListener()
499499
defer d.RemoveStatusListener(ch)
500500

501-
timer := time.NewTimer(timeout)
501+
switch d.system.getStatus().State {
502+
case desiredState:
503+
return true
504+
case interfaces.DataSourceStateOff:
505+
return false
506+
}
507+
508+
deadline := time.After(timeout)
502509

503510
for {
504511
select {
505512
case status := <-ch:
506513
if status.State == desiredState {
507514
return true
508515
}
509-
case <-timer.C:
516+
case <-deadline:
510517
return false
511518
}
512519
}

ldclient_listeners_fdv2_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,30 @@ func TestDataSourceStatusProviderV2(t *testing.T) {
209209

210210
t.Run("waitFor detects correct status", func(t *testing.T) {
211211
clientListenersV2Test(func(p clientListenersV2TestParams) {
212-
p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateValid, time.Second)
212+
// Can wait for the valid state
213+
foundIt := p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateValid, time.Second)
214+
assert.True(t, foundIt)
215+
216+
// Negative timeouts fire immediately
217+
start := time.Now()
218+
foundIt = p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateInterrupted, time.Second*-1)
219+
assert.WithinDuration(t, time.Now(), start, time.Millisecond*30)
220+
assert.False(t, foundIt)
221+
222+
// Make sure timeout will occur when a state cannot be found
223+
foundIt = p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateInterrupted, time.Second)
224+
assert.False(t, foundIt)
225+
226+
// Shut it down and make sure it's stopped.
213227
p.control.Close()
214-
p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateOff, time.Second)
228+
foundIt = p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateOff, time.Second)
229+
assert.True(t, foundIt)
230+
231+
// Ensure that an off status doesn't require the use of a timer.
232+
start = time.Now()
233+
foundIt = p.client.GetDataSourceStatusProvider().WaitFor(interfaces.DataSourceStateValid, time.Hour*100)
234+
assert.WithinDuration(t, time.Now(), start, time.Millisecond*30)
235+
assert.False(t, foundIt)
215236
}, httphelpers.HandlerWithStatus(401))
216237
})
217238
}

0 commit comments

Comments
 (0)