From 624690b06a5de11d108f76c9f93ce5814eaf7603 Mon Sep 17 00:00:00 2001 From: Wessie Date: Thu, 2 May 2024 22:39:50 +0100 Subject: [PATCH] tracker: add benchmark test tracker: fix race condition in tests and add more of them --- tracker/http_test.go | 21 +++++++++++++++++++++ tracker/main_test.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/tracker/http_test.go b/tracker/http_test.go index 2a0d1b9d..9cd2f980 100644 --- a/tracker/http_test.go +++ b/tracker/http_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -97,3 +98,23 @@ func TestListenerAddAndRemove(t *testing.T) { }) } } + +func BenchmarkListenerAdd(b *testing.B) { + recorder := NewRecorder() + ctx := context.Background() + + handler := ListenerAdd(ctx, recorder) + + values := url.Values{ + ICECAST_CLIENTID_FIELD_NAME: []string{ClientID(50).String()}, + } + body := strings.NewReader(values.Encode()) + req := httptest.NewRequest(http.MethodPost, "/listener_joined", body) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + w := httptest.NewRecorder() + w.Body = nil + + for i := 0; i < b.N; i++ { + handler.ServeHTTP(w, req) + } +} diff --git a/tracker/main_test.go b/tracker/main_test.go index 1917054f..708327aa 100644 --- a/tracker/main_test.go +++ b/tracker/main_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math/rand/v2" + "sync" "sync/atomic" "testing" "time" @@ -20,12 +21,16 @@ func TestPeriodicallyUpdateListeners(t *testing.T) { recorder := NewRecorder() var last atomic.Int64 var count int + var closeOnce sync.Once manager := &mocks.ManagerServiceMock{ UpdateListenersFunc: func(contextMoqParam context.Context, new int64) error { // we're done after 10 updates if count++; count > 10 { - close(done) + closeOnce.Do(func() { + close(done) + }) + return nil } // every 5 updates return an error if count%5 == 0 { @@ -34,7 +39,10 @@ func TestPeriodicallyUpdateListeners(t *testing.T) { // otherwise our new value should equal what we set it to previously if !assert.Equal(t, last.Load(), new) { - close(done) + closeOnce.Do(func() { + close(done) + }) + return nil } adjustment := rand.Int64() @@ -47,7 +55,22 @@ func TestPeriodicallyUpdateListeners(t *testing.T) { // set the tickrate a bit higher for testing purposes UpdateListenersTickrate = time.Millisecond * 10 - go PeriodicallyUpdateListeners(ctx, manager, recorder) + finished := make(chan struct{}) + go func() { + defer close(finished) + PeriodicallyUpdateListeners(ctx, manager, recorder) + }() + // wait for the 10 updates <-done + + // cancel the context we gave the function, it should clean + // itself up + cancel() + + select { + case <-finished: + case <-time.After(eventuallyDelay): + t.Error("failed to cleanup") + } }