diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-03-24 03:39:03 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-03-24 03:39:03 +0300 |
commit | a0c274cb5c114689264a14d5126c3f10c8c9a777 (patch) | |
tree | 635e6265197dee2286185117e3a798760340a195 /test/acceptance | |
parent | 2b0d654d661e4ad031b652760f56c9d086d62c90 (diff) |
Add lock to stubOptions
Diffstat (limited to 'test/acceptance')
-rw-r--r-- | test/acceptance/helpers_test.go | 19 | ||||
-rw-r--r-- | test/acceptance/serving_test.go | 20 |
2 files changed, 23 insertions, 16 deletions
diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index 6def0eb9..d2c3c31e 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -372,7 +372,7 @@ func getPagesArgs(t *testing.T, listeners []ListenSpec, promPort string, extraAr func contains(slice []string, s string) bool { for _, e := range slice { - if e == s { + if strings.Contains(e, s) { return true } } @@ -545,6 +545,7 @@ func waitForRoundtrips(t *testing.T, listeners []ListenSpec, timeout time.Durati } type stubOpts struct { + m sync.RWMutex apiCalled bool statusReadyCount int statusHandler http.HandlerFunc @@ -585,6 +586,20 @@ func NewGitlabDomainsSourceStub(t *testing.T, opts *stubOpts) *httptest.Server { return httptest.NewServer(mux) } +func (o *stubOpts) setAPICalled(v bool) { + o.m.Lock() + defer o.m.Unlock() + + o.apiCalled = v +} + +func (o *stubOpts) getAPICalled() bool { + o.m.RLock() + defer o.m.RUnlock() + + return o.apiCalled +} + func lookupFromFile(t *testing.T, domain string, w http.ResponseWriter) { fixture, err := os.Open("../../shared/lookups/" + domain + ".json") if os.IsNotExist(err) { @@ -612,7 +627,7 @@ func defaultAPIHandler(t *testing.T, opts *stubOpts) http.HandlerFunc { return } - opts.apiCalled = true + opts.setAPICalled(true) if opts.pagesStatusResponse != 0 { w.WriteHeader(opts.pagesStatusResponse) diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index b3098912..7824dda9 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -8,7 +8,6 @@ import ( "os" "path" "strings" - "sync" "testing" "time" @@ -528,7 +527,7 @@ func TestDomainsSource(t *testing.T) { require.Equal(t, tt.want.content, string(body), "content mismatch") } - require.Equal(t, tt.want.apiCalled, opts.apiCalled, "api called mismatch") + require.Equal(t, tt.want.apiCalled, opts.getAPICalled(), "api called mismatch") }) } } @@ -554,17 +553,17 @@ func TestGitLabSourceBecomesUnauthorized(t *testing.T) { failedResponse, err := GetPageFromListener(t, httpListener, domain, "/") require.NoError(t, err) - require.True(t, opts.apiCalled, "API should be called") + require.True(t, opts.getAPICalled(), "API should be called") require.Equal(t, http.StatusBadGateway, failedResponse.StatusCode, "first response should fail with 502") // make request again - opts.apiCalled = false + opts.setAPICalled(false) response, err := GetPageFromListener(t, httpListener, domain, "/") require.NoError(t, err) defer response.Body.Close() - require.False(t, opts.apiCalled, "API should not be called after the first failure") + require.False(t, opts.getAPICalled(), "API should not be called after the first failure") require.Equal(t, http.StatusOK, response.StatusCode, "second response should succeed") body, err := ioutil.ReadAll(response.Body) @@ -616,8 +615,6 @@ func TestDomainResolverError(t *testing.T) { }, } - m := sync.RWMutex{} - for name, test := range tests { t.Run(name, func(t *testing.T) { // handler setup @@ -627,10 +624,7 @@ func TestDomainResolverError(t *testing.T) { return } - m.Lock() - defer m.Unlock() - - opts.apiCalled = true + opts.setAPICalled(true) if test.panic { panic("server failed") } @@ -656,9 +650,7 @@ func TestDomainResolverError(t *testing.T) { require.NoError(t, err) defer response.Body.Close() - m.RLock() - require.True(t, opts.apiCalled, "api must have been called") - m.RUnlock() + require.True(t, opts.getAPICalled(), "api must have been called") require.Equal(t, http.StatusBadGateway, response.StatusCode) |