mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2026-03-14 14:35:44 +01:00
Move NATS testing code to separate module.
This commit is contained in:
parent
c162b8bbeb
commit
3c6d3c0b7a
8 changed files with 131 additions and 17 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
87
nats/test/server_test.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue