-
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.
website/api/v1: try and test the SSE handler
These tests are pretty low quality right now but it atleast tests the very basic functionality of the code.
- Loading branch information
Showing
8 changed files
with
166 additions
and
74 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
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 was deleted.
Oops, something went wrong.
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
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,152 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net" | ||
"net/http/httptest" | ||
"reflect" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
radio "github.com/R-a-dio/valkyrie" | ||
"github.com/R-a-dio/valkyrie/mocks" | ||
"github.com/R-a-dio/valkyrie/templates" | ||
"github.com/leanovate/gopter" | ||
"github.com/leanovate/gopter/arbitrary" | ||
"github.com/leanovate/gopter/gen" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/alevinval/sse/pkg/eventsource" | ||
) | ||
|
||
func TestStream(t *testing.T) { | ||
exec := &mocks.ExecutorMock{ | ||
ExecuteAllFunc: func(input templates.TemplateSelectable) (map[string][]byte, error) { | ||
jsonData, err := json.Marshal(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return map[string][]byte{ | ||
"json": jsonData, | ||
}, nil | ||
}, | ||
} | ||
|
||
var muExpected sync.Mutex | ||
var expected radio.Status | ||
|
||
stream := NewStream(exec) | ||
server := httptest.NewUnstartedServer(stream) | ||
server.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context { | ||
return templates.SetTheme(ctx, "json") | ||
} | ||
server.Start() | ||
|
||
t.Log(server.URL) | ||
es1, err := eventsource.New(server.URL) | ||
require.NoError(t, err) | ||
|
||
sync := make(chan struct{}) | ||
go func() { | ||
a := arbitrary.DefaultArbitraries() | ||
timeGen := func(gp *gopter.GenParameters) *gopter.GenResult { | ||
v := time.Date(2000, 10, 9, 8, 7, 6, 5, time.UTC) | ||
res := gen.Time()(gp) | ||
res.Result = v | ||
return res | ||
} | ||
a.RegisterGen(timeGen) | ||
a.RegisterGen(gen.PtrOf(timeGen)) | ||
|
||
genStatus := a.GenForType(reflect.TypeOf(radio.Status{})) | ||
param := gopter.DefaultGenParameters() | ||
|
||
for i := 0; i < 100; i++ { | ||
res := genStatus(param) | ||
<-sync | ||
sendStatus := res.Result.(radio.Status) | ||
muExpected.Lock() | ||
expected = sendStatus | ||
muExpected.Unlock() | ||
stream.SendNowPlaying(sendStatus) | ||
} | ||
}() | ||
|
||
ch1 := es1.MessageEvents() | ||
|
||
jsonEvent := <-ch1 | ||
|
||
assert.Equal(t, EventTime, jsonEvent.Name) | ||
|
||
for i := 0; i < 100; i++ { | ||
sync <- struct{}{} | ||
jsonEvent = <-ch1 | ||
assert.Equal(t, EventMetadata, jsonEvent.Name) | ||
|
||
var status radio.Status | ||
|
||
// check the json version | ||
err = json.Unmarshal([]byte(jsonEvent.Data), &status) | ||
assert.NoError(t, err) | ||
muExpected.Lock() | ||
assert.Equal(t, expected, status) | ||
muExpected.Unlock() | ||
} | ||
} | ||
|
||
func TestStreamSendInputs(t *testing.T) { | ||
var name string | ||
|
||
exec := &mocks.ExecutorMock{ | ||
ExecuteAllFunc: func(input templates.TemplateSelectable) (map[string][]byte, error) { | ||
name = input.TemplateName() | ||
return nil, nil | ||
}, | ||
} | ||
|
||
stream := NewStream(exec) | ||
defer stream.Shutdown() | ||
|
||
t.Run("SendStreamer", func(t *testing.T) { | ||
stream.SendStreamer(radio.User{}) | ||
assert.Equal(t, "streamer", name) | ||
}) | ||
|
||
t.Run("SendNowPlaying", func(t *testing.T) { | ||
stream.SendNowPlaying(radio.Status{}) | ||
assert.Equal(t, "nowplaying", name) | ||
}) | ||
|
||
t.Run("SendQueue", func(t *testing.T) { | ||
stream.SendQueue([]radio.QueueEntry{}) | ||
assert.Equal(t, "queue", name) | ||
}) | ||
|
||
t.Run("SendLastPlayed", func(t *testing.T) { | ||
stream.SendLastPlayed([]radio.Song{}) | ||
assert.Equal(t, "lastplayed", name) | ||
}) | ||
} | ||
|
||
func TestStreamSlowSub(t *testing.T) { | ||
exec := &mocks.ExecutorMock{ | ||
ExecuteAllFunc: func(input templates.TemplateSelectable) (map[string][]byte, error) { | ||
return nil, nil | ||
}, | ||
} | ||
|
||
stream := NewStream(exec) | ||
|
||
ctx := templates.SetTheme(context.Background(), "default") | ||
req := httptest.NewRequest("GET", "/", nil) | ||
req = req.WithContext(ctx) | ||
w := httptest.NewRecorder() | ||
|
||
go func() { | ||
stream.ServeHTTP(w, req) | ||
}() | ||
} |