Skip to content

Commit

Permalink
restructure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoSchw committed Mar 11, 2024
1 parent 489967f commit 8f05b96
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
8 changes: 2 additions & 6 deletions internal/lobby/lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lobby
import (
"context"
"errors"
"time"

"github.com/google/uuid"
"github.com/shigde/sfu/internal/lobby/sessions"
Expand All @@ -17,11 +16,8 @@ type command interface {
}

var (
errNoSession = errors.New("no session exists")
ErrNoSession = errors.New("no session exists")
ErrSessionAlreadyExists = errors.New("session already exists")
errLobbyStopped = errors.New("error because lobby stopped")
errLobbyRequestTimeout = errors.New("lobby request timeout error")
lobbyReqTimeout = 10 * time.Second
)

type lobby struct {
Expand Down Expand Up @@ -79,5 +75,5 @@ func (l *lobby) handle(cmd command) {
if session, found := l.sessions.FindByUserId(cmd.GetUserId()); found {
cmd.Execute(l.ctx, session)
}
cmd.Fail(errNoSession)
cmd.Fail(ErrNoSession)
}
22 changes: 22 additions & 0 deletions internal/lobby/lobby_manager_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
package lobby

import (
"net/url"
"testing"

"github.com/google/uuid"
"github.com/shigde/sfu/internal/storage"
)

func testLobbyManagerSetup(t *testing.T) (*LobbyManager, uuid.UUID) {
t.Helper()
homeUrl, _ := url.Parse("http://localhost:1234/")
registerToken := "federation_registration_token"
rtp := newRtpEngineMock()
manager := NewLobbyManager(storage.NewTestStore(), rtp, homeUrl, registerToken)
lobbyId := uuid.New()
return manager, lobbyId
}

func TestLobbyManager_NewIngressResource(t *testing.T) {

}
1 change: 0 additions & 1 deletion internal/lobby/lobby_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (r *lobbyRepository) getOrCreateLobby(ctx context.Context, lobbyId uuid.UUI
_ = instances.NewHostSettings(entity, r.instanceActorUrl, r.registerToken)

lobby := newLobby(lobbyId, entity)

r.lobbies[lobbyId] = lobby
metric.RunningLobbyInc(lobby.entity.LiveStreamId.String(), lobbyId.String())
return lobby, nil
Expand Down
14 changes: 7 additions & 7 deletions internal/lobby/lobby_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
)

func testRtpStreamLobbyRepositorySetup(t *testing.T) *lobbyRepository {
func testLobbyRepositorySetup(t *testing.T) *lobbyRepository {
t.Helper()
store := storage.NewTestStore()
_ = store.GetDatabase().AutoMigrate(&LobbyEntity{})
Expand All @@ -22,24 +22,24 @@ func testRtpStreamLobbyRepositorySetup(t *testing.T) *lobbyRepository {

return repository
}
func TestStreamLobbyRepository(t *testing.T) {
func TestLobbyRepository(t *testing.T) {

t.Run("Get not existing lobby", func(t *testing.T) {
repo := testRtpStreamLobbyRepositorySetup(t)
repo := testLobbyRepositorySetup(t)
lobby, ok := repo.getLobby(uuid.New())
assert.False(t, ok)
assert.Nil(t, lobby)
})

t.Run("Create lobby", func(t *testing.T) {
repo := testRtpStreamLobbyRepositorySetup(t)
repo := testLobbyRepositorySetup(t)
lobby, _ := repo.getOrCreateLobby(context.Background(), uuid.New(), make(chan uuid.UUID))
assert.NotNil(t, lobby)
lobby.stop()
})

t.Run("Create and Get lobby", func(t *testing.T) {
repo := testRtpStreamLobbyRepositorySetup(t)
repo := testLobbyRepositorySetup(t)
id := uuid.New()
lobbyCreated, _ := repo.getOrCreateLobby(context.Background(), id, make(chan uuid.UUID))

Expand All @@ -51,7 +51,7 @@ func TestStreamLobbyRepository(t *testing.T) {
})

t.Run("Delete lobby", func(t *testing.T) {
repo := testRtpStreamLobbyRepositorySetup(t)
repo := testLobbyRepositorySetup(t)
id := uuid.New()
created, _ := repo.getOrCreateLobby(context.Background(), id, make(chan uuid.UUID))
assert.NotNil(t, created)
Expand All @@ -69,7 +69,7 @@ func TestStreamLobbyRepository(t *testing.T) {
createOn := 200
deleteOn := 500
id := uuid.New()
repo := testRtpStreamLobbyRepositorySetup(t)
repo := testLobbyRepositorySetup(t)

var wg sync.WaitGroup
wg.Add(wantedCount + 2)
Expand Down
10 changes: 8 additions & 2 deletions internal/lobby/lobby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func testLobbySetup(t *testing.T) (*lobby, uuid.UUID) {
lobby.newSession(user, nil)
return lobby, user
}
func TestLobby(t *testing.T) {
func TestLobby_handle(t *testing.T) {
t.Run("handle command successfully", func(t *testing.T) {
lobby, user := testLobbySetup(t)
cmd := &mockCmd{
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestLobby(t *testing.T) {
case <-cmd.Response:
t.Fatalf("test fails because no webrtc resource expected")
case err := <-cmd.Err:
assert.ErrorIs(t, err, errNoSession)
assert.ErrorIs(t, err, ErrNoSession)
case <-time.After(time.Second * 3):
t.Fatalf("test fails because run in timeout")
}
Expand Down Expand Up @@ -121,7 +121,9 @@ func TestLobby(t *testing.T) {
t.Fatalf("test fails because run in timeout")
}
})
}

func TestLobby_newSession(t *testing.T) {
t.Run("new session added", func(t *testing.T) {
lobby, _ := testLobbySetup(t)
user := uuid.New()
Expand All @@ -134,7 +136,9 @@ func TestLobby(t *testing.T) {
ok := lobby.newSession(user, nil)
assert.False(t, ok)
})
}

func TestLobby_removeSession(t *testing.T) {
t.Run("delete session if exits", func(t *testing.T) {
lobby, user := testLobbySetup(t)
ok := lobby.removeSession(user)
Expand All @@ -146,7 +150,9 @@ func TestLobby(t *testing.T) {
ok := lobby.removeSession(uuid.New())
assert.False(t, ok)
})
}

func TestLobby_sessionGarbageCollector(t *testing.T) {
t.Run("delete session if exits by garbage collector", func(t *testing.T) {
lobby, user := testLobbySetup(t)
session, foundBefore := lobby.sessions.FindByUserId(user)
Expand Down

0 comments on commit 8f05b96

Please sign in to comment.