-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tracker: moved the periodic UpdateListeners to a separate function tracker: listener_joined now only returns OK if the client fits out criteria
- Loading branch information
Showing
6 changed files
with
211 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package tracker | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestListenerAddAndRemove(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
recorder := NewRecorder() | ||
dummy := NewServer(ctx, "", recorder) | ||
|
||
srv := httptest.NewServer(dummy.Handler) | ||
defer srv.Close() | ||
client := srv.Client() | ||
|
||
t.Run("join then leave", func(t *testing.T) { | ||
id := ClientID(500) | ||
|
||
// ======================== | ||
// Do a normal join request | ||
resp, err := client.PostForm(srv.URL+"/listener_joined", url.Values{ | ||
ICECAST_CLIENTID_FIELD_NAME: []string{id.String()}, | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp) | ||
resp.Body.Close() | ||
|
||
// status should be OK | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
// and we should have the OK header that icecast needs | ||
require.Equal(t, "1", resp.Header.Get(ICECAST_AUTH_HEADER)) | ||
|
||
// we also should have a listener in the recorder | ||
require.Eventually(t, func() bool { | ||
return assert.Equal(t, int64(1), recorder.ListenerAmount()) | ||
}, eventuallyDelay, eventuallyTick) | ||
testListenerLength(t, recorder, 1) | ||
|
||
// ========================= | ||
// Do a normal leave request | ||
resp, err = client.PostForm(srv.URL+"/listener_left", url.Values{ | ||
ICECAST_CLIENTID_FIELD_NAME: []string{id.String()}, | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp) | ||
resp.Body.Close() | ||
|
||
// status should be OK again | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
// and the listener should now be gone | ||
require.Eventually(t, func() bool { | ||
return assert.Equal(t, int64(0), recorder.ListenerAmount()) | ||
}, eventuallyDelay, eventuallyTick) | ||
|
||
testListenerLength(t, recorder, 0) | ||
}) | ||
|
||
for _, uri := range []string{"/listener_joined", "/listener_left"} { | ||
t.Run("empty client"+uri, func(t *testing.T) { | ||
// ======================================== | ||
// Try an empty client ID, this should fail | ||
resp, err := client.PostForm(srv.URL+uri, url.Values{ | ||
ICECAST_CLIENTID_FIELD_NAME: []string{}, | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp) | ||
resp.Body.Close() | ||
|
||
// status should still be OK | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
// but it should not have the OK header | ||
require.Zero(t, resp.Header.Get(ICECAST_AUTH_HEADER)) | ||
}) | ||
|
||
t.Run("non-integer client"+uri, func(t *testing.T) { | ||
// ======================================== | ||
// Try a non-integer client ID, this should fail | ||
resp, err := client.PostForm(srv.URL+uri, url.Values{ | ||
ICECAST_CLIENTID_FIELD_NAME: []string{"not an integer"}, | ||
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp) | ||
resp.Body.Close() | ||
|
||
// status should still be OK | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
// but it should not have the OK header | ||
require.Zero(t, resp.Header.Get(ICECAST_AUTH_HEADER)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tracker | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand/v2" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/R-a-dio/valkyrie/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPeriodicallyUpdateListeners(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
done := make(chan struct{}) | ||
|
||
recorder := NewRecorder() | ||
var last atomic.Int64 | ||
var count int | ||
|
||
manager := &mocks.ManagerServiceMock{ | ||
UpdateListenersFunc: func(contextMoqParam context.Context, new int64) error { | ||
// we're done after 10 updates | ||
if count++; count > 10 { | ||
close(done) | ||
} | ||
// every 5 updates return an error | ||
if count%5 == 0 { | ||
return fmt.Errorf("that's an error") | ||
} | ||
|
||
// otherwise our new value should equal what we set it to previously | ||
if !assert.Equal(t, last.Load(), new) { | ||
close(done) | ||
} | ||
|
||
adjustment := rand.Int64() | ||
recorder.listenerAmount.Store(adjustment) | ||
last.Store(adjustment) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
// set the tickrate a bit higher for testing purposes | ||
UpdateListenersTickrate = time.Millisecond * 10 | ||
go PeriodicallyUpdateListeners(ctx, manager, recorder) | ||
|
||
<-done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters