Skip to content

Commit

Permalink
synchornized
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Mar 22, 2022
1 parent 0179f8a commit e01a744
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions httprc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestCache(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var muCalled sync.Mutex
var called int
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
Expand All @@ -36,7 +37,9 @@ func TestCache(t *testing.T) {
default:
}

muCalled.Lock()
called++
muCalled.Unlock()
w.Header().Set(`Cache-Control`, fmt.Sprintf(`max-age=%d`, 3))
w.WriteHeader(http.StatusOK)
}))
Expand All @@ -61,9 +64,11 @@ func TestCache(t *testing.T) {
return
}
}
muCalled.Lock()
if !assert.Equal(t, 1, called, `there should only be one fetch request`) {
return
}
muCalled.Unlock()

time.Sleep(4 * time.Second)
for i := 0; i < 3; i++ {
Expand All @@ -72,9 +77,12 @@ func TestCache(t *testing.T) {
return
}
}

muCalled.Lock()
if !assert.Equal(t, 2, called, `there should only be one fetch request`) {
return
}
muCalled.Unlock()

if !assert.True(t, len(errSink.errors) == 0) {
return
Expand Down

0 comments on commit e01a744

Please sign in to comment.