Skip to content

Commit

Permalink
Closing the client before reading error count solves race
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Nov 30, 2023
1 parent 33ac32e commit cd4dd25
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
20 changes: 4 additions & 16 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ func TestMetrics_ShouldNotCountMetricsForParentToggles(t *testing.T) {
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)

assert.Nil(err, "client should not return an error")
client.WaitForReady()
client.IsEnabled("child")

assert.EqualValues(client.metrics.bucket.Toggles["child"].Yes, 1)
assert.EqualValues(client.metrics.bucket.Toggles["parent"].Yes, 0)
client.Close()
err = client.Close()

assert.Nil(err, "client should not return an error")
assert.True(gock.IsDone(), "there should be no more mocks")
Expand Down Expand Up @@ -381,8 +381,8 @@ func TestMetrics_ShouldBackoffOn500(t *testing.T) {
client.IsEnabled("baz")

time.Sleep(320 * time.Millisecond)
assert.Equal(float64(3), client.metrics.errors)
err = client.Close()
assert.Equal(float64(3), client.metrics.errors)
assert.Nil(err, "Client should close without a problem")

}
Expand All @@ -406,24 +406,12 @@ func TestMetrics_ErrorCountShouldDecreaseIfSuccessful(t *testing.T) {
Post("/client/metrics").
Persist().
Reply(200)
mockListener := &MockedListener{}
mockListener.On("OnReady").Return()
mockListener.On("OnRegistered", mock.AnythingOfType("ClientData")).Return()
mockListener.On("OnCount", "foo", false).Return()
mockListener.On("OnCount", "bar", false).Return()
mockListener.On("OnCount", "baz", false).Return()
mockListener.On("OnWarning", mock.MatchedBy(func(e error) bool {
return strings.HasSuffix(e.Error(), "http://foo.com/client/metrics return 500")
})).Return()
mockListener.On("OnError", mock.Anything).Return()
mockListener.On("OnSent", mock.Anything).Return()

client, err := NewClient(
WithUrl(mockerServer),
WithMetricsInterval(50*time.Millisecond),
WithAppName(mockAppName),
WithInstanceId(mockInstanceId),
WithListener(mockListener),
)
assert.Nil(err, "client should not return an error")

Expand All @@ -434,7 +422,7 @@ func TestMetrics_ErrorCountShouldDecreaseIfSuccessful(t *testing.T) {
time.Sleep(360 * time.Millisecond)
client.IsEnabled("foo")
time.Sleep(100 * time.Millisecond)
assert.Equal(float64(0), client.metrics.errors)
err = client.Close()
assert.Equal(float64(0), client.metrics.errors)
assert.Nil(err, "Client should close without a problem")
}
6 changes: 3 additions & 3 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestRepository_ParseAPIResponse(t *testing.T) {
assert.Equal(0, len(response.Segments))
}


func TestRepository_backs_off_on_http_statuses(t *testing.T) {
a := assert.New(t)
testCases := []struct {
Expand Down Expand Up @@ -157,12 +158,11 @@ func TestRepository_backs_off_on_http_statuses(t *testing.T) {
)
a.Nil(err)
time.Sleep(20 * time.Millisecond)
a.Equal(tc.errorCount, client.repository.errors)
err = client.Close()
a.Equal(tc.errorCount, client.repository.errors)
a.Nil(err)
}
}

func TestRepository_back_offs_are_gradually_reduced_on_success(t *testing.T) {
a := assert.New(t)
defer gock.Off()
Expand All @@ -183,7 +183,7 @@ func TestRepository_back_offs_are_gradually_reduced_on_success(t *testing.T) {
)
a.Nil(err)
client.WaitForReady()
a.Equal(float64(3), client.repository.errors) // 4 failures, and then one success, should reduce error count to 3
err = client.Close()
a.Equal(float64(3), client.repository.errors) // 4 failures, and then one success, should reduce error count to 3
a.Nil(err)
}

0 comments on commit cd4dd25

Please sign in to comment.