From 0372634076cdfd7cda740cd040e16e629ea662e4 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 11 May 2020 22:02:58 -0700 Subject: Fix race conditions with logrus testing logrus fires a Goroutine to write logs, so the tests could fail if they checked the event queue before the logrus have fired. Since there isn't an easy way to flush all outstanding hooks, we just retry every 100 ms for up to a second for log to arrive in the queue. Closes https://gitlab.com/gitlab-org/gitlab-shell/-/issues/450 --- client/client_test.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'client/client_test.go') diff --git a/client/client_test.go b/client/client_test.go index 5e852dc..5c5fda8 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -123,6 +123,7 @@ func testSuccessfulGet(t *testing.T, client *GitlabNetClient) { assert.NoError(t, err) assert.Equal(t, string(responseBody), "Hello") + require.True(t, testhelper.WaitForLogEvent(hook)) entries := hook.AllEntries() assert.Equal(t, 1, len(entries)) assert.Equal(t, logrus.InfoLevel, entries[0].Level) @@ -147,6 +148,7 @@ func testSuccessfulPost(t *testing.T, client *GitlabNetClient) { assert.NoError(t, err) assert.Equal(t, "Echo: {\"key\":\"value\"}", string(responseBody)) + require.True(t, testhelper.WaitForLogEvent(hook)) entries := hook.AllEntries() assert.Equal(t, 1, len(entries)) assert.Equal(t, logrus.InfoLevel, entries[0].Level) @@ -163,6 +165,7 @@ func testMissing(t *testing.T, client *GitlabNetClient) { assert.EqualError(t, err, "Internal API error (404)") assert.Nil(t, response) + require.True(t, testhelper.WaitForLogEvent(hook)) entries := hook.AllEntries() assert.Equal(t, 1, len(entries)) assert.Equal(t, logrus.InfoLevel, entries[0].Level) @@ -177,6 +180,7 @@ func testMissing(t *testing.T, client *GitlabNetClient) { assert.EqualError(t, err, "Internal API error (404)") assert.Nil(t, response) + require.True(t, testhelper.WaitForLogEvent(hook)) entries := hook.AllEntries() assert.Equal(t, 1, len(entries)) assert.Equal(t, logrus.InfoLevel, entries[0].Level) @@ -208,6 +212,7 @@ func testBrokenRequest(t *testing.T, client *GitlabNetClient) { assert.EqualError(t, err, "Internal API unreachable") assert.Nil(t, response) + require.True(t, testhelper.WaitForLogEvent(hook)) entries := hook.AllEntries() assert.Equal(t, 1, len(entries)) assert.Equal(t, logrus.InfoLevel, entries[0].Level) @@ -223,6 +228,7 @@ func testBrokenRequest(t *testing.T, client *GitlabNetClient) { assert.EqualError(t, err, "Internal API unreachable") assert.Nil(t, response) + require.True(t, testhelper.WaitForLogEvent(hook)) entries := hook.AllEntries() assert.Equal(t, 1, len(entries)) assert.Equal(t, logrus.InfoLevel, entries[0].Level) -- cgit v1.2.1