Skip to content

Commit

Permalink
tracker: add benchmark test
Browse files Browse the repository at this point in the history
tracker: fix race condition in tests and add more of them
  • Loading branch information
Wessie committed May 2, 2024
1 parent 1720475 commit 624690b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
21 changes: 21 additions & 0 deletions tracker/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
}
}
29 changes: 26 additions & 3 deletions tracker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand/v2"
"sync"
"sync/atomic"
"testing"
"time"
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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")
}
}

0 comments on commit 624690b

Please sign in to comment.