Skip to content

Commit

Permalink
Use "T.Cleanup()" instead of manually deferred shutdown function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Apr 5, 2022
1 parent 81a5238 commit 2a491ec
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 166 deletions.
57 changes: 21 additions & 36 deletions backend_server_test.go
Expand Up @@ -52,19 +52,19 @@ var (
turnServers = strings.Split(turnServersString, ",")
)

func CreateBackendServerForTest(t *testing.T) (*goconf.ConfigFile, *BackendServer, NatsClient, *Hub, *mux.Router, *httptest.Server, func()) {
func CreateBackendServerForTest(t *testing.T) (*goconf.ConfigFile, *BackendServer, NatsClient, *Hub, *mux.Router, *httptest.Server) {
return CreateBackendServerForTestFromConfig(t, nil)
}

func CreateBackendServerForTestWithTurn(t *testing.T) (*goconf.ConfigFile, *BackendServer, NatsClient, *Hub, *mux.Router, *httptest.Server, func()) {
func CreateBackendServerForTestWithTurn(t *testing.T) (*goconf.ConfigFile, *BackendServer, NatsClient, *Hub, *mux.Router, *httptest.Server) {
config := goconf.NewConfigFile()
config.AddOption("turn", "apikey", turnApiKey)
config.AddOption("turn", "secret", turnSecret)
config.AddOption("turn", "servers", turnServersString)
return CreateBackendServerForTestFromConfig(t, config)
}

func CreateBackendServerForTestFromConfig(t *testing.T, config *goconf.ConfigFile) (*goconf.ConfigFile, *BackendServer, NatsClient, *Hub, *mux.Router, *httptest.Server, func()) {
func CreateBackendServerForTestFromConfig(t *testing.T, config *goconf.ConfigFile) (*goconf.ConfigFile, *BackendServer, NatsClient, *Hub, *mux.Router, *httptest.Server) {
r := mux.NewRouter()
registerBackendHandler(t, r)

Expand Down Expand Up @@ -103,17 +103,17 @@ func CreateBackendServerForTestFromConfig(t *testing.T, config *goconf.ConfigFil

go hub.Run()

shutdown := func() {
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()

WaitForHub(ctx, t, hub)
(nats).(*LoopbackNatsClient).waitForSubscriptionsEmpty(ctx, t)
nats.Close()
server.Close()
}
})

return config, b, nats, hub, r, server, shutdown
return config, b, nats, hub, r, server
}

func performBackendRequest(url string, body []byte) (*http.Response, error) {
Expand Down Expand Up @@ -161,8 +161,7 @@ func expectRoomlistEvent(n NatsClient, ch chan *nats.Msg, subject string, msgTyp
}

func TestBackendServer_NoAuth(t *testing.T) {
_, _, _, _, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, _, _, server := CreateBackendServerForTest(t)

roomId := "the-room-id"
data := []byte{'{', '}'}
Expand All @@ -188,8 +187,7 @@ func TestBackendServer_NoAuth(t *testing.T) {
}

func TestBackendServer_InvalidAuth(t *testing.T) {
_, _, _, _, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, _, _, server := CreateBackendServerForTest(t)

roomId := "the-room-id"
data := []byte{'{', '}'}
Expand Down Expand Up @@ -217,8 +215,7 @@ func TestBackendServer_InvalidAuth(t *testing.T) {
}

func TestBackendServer_OldCompatAuth(t *testing.T) {
_, _, _, _, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, _, _, server := CreateBackendServerForTest(t)

roomId := "the-room-id"
userid := "the-user-id"
Expand Down Expand Up @@ -267,8 +264,7 @@ func TestBackendServer_OldCompatAuth(t *testing.T) {
}

func TestBackendServer_InvalidBody(t *testing.T) {
_, _, _, _, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, _, _, server := CreateBackendServerForTest(t)

roomId := "the-room-id"
data := []byte{1, 2, 3, 4} // Invalid JSON
Expand All @@ -287,8 +283,7 @@ func TestBackendServer_InvalidBody(t *testing.T) {
}

func TestBackendServer_UnsupportedRequest(t *testing.T) {
_, _, _, _, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, _, _, server := CreateBackendServerForTest(t)

msg := &BackendServerRoomRequest{
Type: "lala",
Expand All @@ -314,8 +309,7 @@ func TestBackendServer_UnsupportedRequest(t *testing.T) {
}

func TestBackendServer_RoomInvite(t *testing.T) {
_, _, n, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, n, hub, _, server := CreateBackendServerForTest(t)

u, err := url.Parse(server.URL)
if err != nil {
Expand Down Expand Up @@ -382,8 +376,7 @@ func TestBackendServer_RoomInvite(t *testing.T) {
}

func TestBackendServer_RoomDisinvite(t *testing.T) {
_, _, n, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, n, hub, _, server := CreateBackendServerForTest(t)

u, err := url.Parse(server.URL)
if err != nil {
Expand Down Expand Up @@ -491,8 +484,7 @@ func TestBackendServer_RoomDisinvite(t *testing.T) {
}

func TestBackendServer_RoomDisinviteDifferentRooms(t *testing.T) {
_, _, _, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, hub, _, server := CreateBackendServerForTest(t)

client1 := NewTestClient(t, server, hub)
defer client1.CloseWithBye()
Expand Down Expand Up @@ -618,8 +610,7 @@ func TestBackendServer_RoomDisinviteDifferentRooms(t *testing.T) {
}

func TestBackendServer_RoomUpdate(t *testing.T) {
_, _, n, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, n, hub, _, server := CreateBackendServerForTest(t)

u, err := url.Parse(server.URL)
if err != nil {
Expand Down Expand Up @@ -704,8 +695,7 @@ func TestBackendServer_RoomUpdate(t *testing.T) {
}

func TestBackendServer_RoomDelete(t *testing.T) {
_, _, n, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, n, hub, _, server := CreateBackendServerForTest(t)

u, err := url.Parse(server.URL)
if err != nil {
Expand Down Expand Up @@ -786,8 +776,7 @@ func TestBackendServer_RoomDelete(t *testing.T) {
}

func TestBackendServer_ParticipantsUpdatePermissions(t *testing.T) {
_, _, _, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, hub, _, server := CreateBackendServerForTest(t)

client1 := NewTestClient(t, server, hub)
defer client1.CloseWithBye()
Expand Down Expand Up @@ -901,8 +890,7 @@ func TestBackendServer_ParticipantsUpdatePermissions(t *testing.T) {
}

func TestBackendServer_ParticipantsUpdateEmptyPermissions(t *testing.T) {
_, _, _, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, hub, _, server := CreateBackendServerForTest(t)

client := NewTestClient(t, server, hub)
defer client.CloseWithBye()
Expand Down Expand Up @@ -987,8 +975,7 @@ func TestBackendServer_ParticipantsUpdateEmptyPermissions(t *testing.T) {
}

func TestBackendServer_ParticipantsUpdateTimeout(t *testing.T) {
_, _, _, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, hub, _, server := CreateBackendServerForTest(t)

client1 := NewTestClient(t, server, hub)
defer client1.CloseWithBye()
Expand Down Expand Up @@ -1201,8 +1188,7 @@ func TestBackendServer_ParticipantsUpdateTimeout(t *testing.T) {
}

func TestBackendServer_RoomMessage(t *testing.T) {
_, _, _, hub, _, server, shutdown := CreateBackendServerForTest(t)
defer shutdown()
_, _, _, hub, _, server := CreateBackendServerForTest(t)

client := NewTestClient(t, server, hub)
defer client.CloseWithBye()
Expand Down Expand Up @@ -1267,8 +1253,7 @@ func TestBackendServer_RoomMessage(t *testing.T) {
}

func TestBackendServer_TurnCredentials(t *testing.T) {
_, _, _, _, _, server, shutdown := CreateBackendServerForTestWithTurn(t)
defer shutdown()
_, _, _, _, _, server := CreateBackendServerForTestWithTurn(t)

q := make(url.Values)
q.Set("service", "turn")
Expand Down
6 changes: 2 additions & 4 deletions clientsession_test.go
Expand Up @@ -126,8 +126,7 @@ func Test_permissionsEqual(t *testing.T) {
}

func TestBandwidth_Client(t *testing.T) {
hub, _, _, server, shutdown := CreateHubForTest(t)
defer shutdown()
hub, _, _, server := CreateHubForTest(t)

mcu, err := NewTestMCU()
if err != nil {
Expand Down Expand Up @@ -199,8 +198,7 @@ func TestBandwidth_Client(t *testing.T) {
}

func TestBandwidth_Backend(t *testing.T) {
hub, _, _, server, shutdown := CreateHubWithMultipleBackendsForTest(t)
defer shutdown()
hub, _, _, server := CreateHubWithMultipleBackendsForTest(t)

u, err := url.Parse(server.URL + "/one")
if err != nil {
Expand Down

0 comments on commit 2a491ec

Please sign in to comment.