Move NATS testing code to separate module.

This commit is contained in:
Joachim Bauch 2026-01-12 13:37:31 +01:00
commit 3c6d3c0b7a
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
8 changed files with 131 additions and 17 deletions

View file

@ -32,6 +32,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/api"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
natstest "github.com/strukturag/nextcloud-spreed-signaling/nats/test"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
)
@ -159,7 +160,7 @@ func TestAsyncEvents_Loopback(t *testing.T) {
func TestAsyncEvents_NATS(t *testing.T) {
t.Parallel()
server, _ := nats.StartLocalServer(t)
server, _ := natstest.StartLocalServer(t)
logger := log.NewLoggerForTest(t)
ctx := log.NewLoggerContext(t.Context(), logger)
events, err := NewAsyncEvents(ctx, server.ClientURL())

View file

@ -33,6 +33,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/async/events"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
natstest "github.com/strukturag/nextcloud-spreed-signaling/nats/test"
)
var (
@ -62,7 +63,7 @@ func GetAsyncEventsForTest(t *testing.T) events.AsyncEvents {
func getRealAsyncEventsForTest(t *testing.T) events.AsyncEvents {
logger := log.NewLoggerForTest(t)
ctx := log.NewLoggerContext(t.Context(), logger)
server, _ := nats.StartLocalServer(t)
server, _ := natstest.StartLocalServer(t)
events, err := events.NewAsyncEvents(ctx, server.ClientURL())
if err != nil {
require.NoError(t, err)
@ -92,7 +93,7 @@ func getLoopbackAsyncEventsForTest(t *testing.T) events.AsyncEvents {
return
}
nats.WaitForSubscriptionsEmpty(ctx, t, e.GetNatsClient())
natstest.WaitForSubscriptionsEmpty(ctx, t, e.GetNatsClient())
})
return events
}

View file

@ -60,6 +60,13 @@ func NewLoopbackClient(logger log.Logger) (Client, error) {
return client, nil
}
func (c *LoopbackClient) SubscriptionCount() int {
c.mu.Lock()
defer c.mu.Unlock()
return len(c.subscriptions)
}
func (c *LoopbackClient) processMessages() {
defer close(c.closed)

View file

@ -31,12 +31,31 @@ import (
"github.com/stretchr/testify/require"
"github.com/nats-io/nats-server/v2/server"
natsservertest "github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/test"
)
func StartLocalServer(t *testing.T) (*server.Server, int) {
t.Helper()
return StartLocalServerPort(t, server.RANDOM_PORT)
}
func StartLocalServerPort(t *testing.T, port int) (*server.Server, int) {
t.Helper()
opts := natsservertest.DefaultTestOptions
opts.Port = port
opts.Cluster.Name = "testing"
srv := natsservertest.RunServer(&opts)
t.Cleanup(func() {
srv.Shutdown()
srv.WaitForShutdown()
})
return srv, opts.Port
}
func CreateLocalClientForTest(t *testing.T, options ...nats.Option) (*server.Server, int, Client) {
t.Helper()
server, port := StartLocalServer(t)

View file

@ -19,7 +19,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nats
package test
import (
"context"
@ -29,6 +29,8 @@ import (
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats-server/v2/test"
"github.com/stretchr/testify/assert"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
)
func StartLocalServer(t *testing.T) (*server.Server, int) {
@ -49,22 +51,18 @@ func StartLocalServerPort(t *testing.T, port int) (*server.Server, int) {
return srv, opts.Port
}
func WaitForSubscriptionsEmpty(ctx context.Context, t *testing.T, client Client) {
func WaitForSubscriptionsEmpty(ctx context.Context, t *testing.T, client nats.Client) {
t.Helper()
if c, ok := client.(*LoopbackClient); assert.True(t, ok, "expected LoopbackNatsClient, got %T", client) {
if c, ok := client.(*nats.LoopbackClient); assert.True(t, ok, "expected LoopbackNatsClient, got %T", client) {
for {
c.mu.Lock()
count := len(c.subscriptions)
c.mu.Unlock()
if count == 0 {
remaining := c.SubscriptionCount()
if remaining == 0 {
break
}
select {
case <-ctx.Done():
c.mu.Lock()
assert.NoError(t, ctx.Err(), "Error waiting for subscriptions %+v to terminate", c.subscriptions)
c.mu.Unlock()
assert.NoError(t, ctx.Err(), "Error waiting for %d subscriptions to terminate", remaining)
return
default:
time.Sleep(time.Millisecond)

87
nats/test/server_test.go Normal file
View file

@ -0,0 +1,87 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2026 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
)
func TestLocalServer(t *testing.T) {
t.Parallel()
require := require.New(t)
assert := assert.New(t)
server, port := StartLocalServer(t)
assert.NotEqual(0, port)
ctx := log.NewLoggerContext(t.Context(), log.NewLoggerForTest(t))
client, err := nats.NewClient(ctx, server.ClientURL())
require.NoError(err)
assert.NoError(client.Close(context.Background()))
}
func TestWaitForSubscriptionsEmpty(t *testing.T) {
t.Parallel()
require := require.New(t)
assert := assert.New(t)
ctx := log.NewLoggerContext(t.Context(), log.NewLoggerForTest(t))
client, err := nats.NewClient(ctx, nats.LoopbackUrl)
require.NoError(err)
defer func() {
assert.NoError(client.Close(context.Background()))
}()
ch := make(chan *nats.Msg)
sub, err := client.Subscribe("foo", ch)
require.NoError(err)
ready := make(chan struct{})
done := make(chan struct{})
go func() {
defer close(done)
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
defer cancel()
close(ready)
WaitForSubscriptionsEmpty(ctx, t, client)
}()
<-ready
require.NoError(sub.Unsubscribe())
<-done
}

View file

@ -53,6 +53,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/internal"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
natstest "github.com/strukturag/nextcloud-spreed-signaling/nats/test"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
)
@ -149,7 +150,7 @@ func CreateBackendServerWithClusteringForTestFromConfig(t *testing.T, config1 *g
server2.Close()
})
nats, _ := nats.StartLocalServer(t)
nats, _ := natstest.StartLocalServer(t)
grpcServer1, addr1 := grpctest.NewServerForTest(t)
grpcServer2, addr2 := grpctest.NewServerForTest(t)

View file

@ -62,7 +62,7 @@ import (
"github.com/strukturag/nextcloud-spreed-signaling/internal"
"github.com/strukturag/nextcloud-spreed-signaling/log"
"github.com/strukturag/nextcloud-spreed-signaling/mock"
"github.com/strukturag/nextcloud-spreed-signaling/nats"
natstest "github.com/strukturag/nextcloud-spreed-signaling/nats/test"
"github.com/strukturag/nextcloud-spreed-signaling/session"
sfutest "github.com/strukturag/nextcloud-spreed-signaling/sfu/test"
"github.com/strukturag/nextcloud-spreed-signaling/talk"
@ -235,10 +235,10 @@ func CreateClusteredHubsForTestWithConfig(t *testing.T, getConfigFunc func(*http
server2.Close()
})
nats1, _ := nats.StartLocalServer(t)
nats1, _ := natstest.StartLocalServer(t)
var nats2 *server.Server
if strings.Contains(t.Name(), "Federation") {
nats2, _ = nats.StartLocalServer(t)
nats2, _ = natstest.StartLocalServer(t)
} else {
nats2 = nats1
}