Skip to content

Commit 7aa04a0

Browse files
Fix crash when in offline mode (#240)
1 parent fadb74f commit 7aa04a0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

feature_flag.go

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ func retrieveFlagsAndUpdateCache(config Config, cache cache.Manager) error {
152152

153153
// GetCacheRefreshDate gives the date of the latest refresh of the cache
154154
func (g *GoFeatureFlag) GetCacheRefreshDate() time.Time {
155+
if g.config.Offline {
156+
return time.Time{}
157+
}
155158
return g.cache.GetLatestUpdateDate()
156159
}
157160

feature_flag_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ func TestGoFeatureFlag_GetCacheRefreshDate(t *testing.T) {
297297
name string
298298
fields fields
299299
hasRefresh bool
300+
offline bool
300301
}{
301302
{
302303
name: "Should be refreshed",
@@ -308,20 +309,29 @@ func TestGoFeatureFlag_GetCacheRefreshDate(t *testing.T) {
308309
fields: fields{waitingDuration: 2 * time.Second, pollingInterval: 3 * time.Second},
309310
hasRefresh: false,
310311
},
312+
{
313+
name: "Should not crash in offline mode",
314+
fields: fields{waitingDuration: 2 * time.Second, pollingInterval: 3 * time.Second},
315+
hasRefresh: false,
316+
offline: true,
317+
},
311318
}
312319
for _, tt := range tests {
313320
t.Run(tt.name, func(t *testing.T) {
314321
gff, _ := ffclient.New(ffclient.Config{
315322
PollingInterval: tt.fields.pollingInterval,
316323
Retriever: &ffclient.FileRetriever{Path: "testdata/flag-config.yaml"},
324+
Offline: tt.offline,
317325
})
318326

319327
date1 := gff.GetCacheRefreshDate()
320328
time.Sleep(tt.fields.waitingDuration)
321329
date2 := gff.GetCacheRefreshDate()
322330

323-
assert.NotEqual(t, time.Time{}, date1)
324-
assert.NotEqual(t, time.Time{}, date2)
331+
if !tt.offline {
332+
assert.NotEqual(t, time.Time{}, date1)
333+
assert.NotEqual(t, time.Time{}, date2)
334+
}
325335
assert.Equal(t, tt.hasRefresh, date1.Before(date2))
326336
})
327337
}

0 commit comments

Comments
 (0)