46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
|
|
package services
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
"go.uber.org/zap"
|
||
|
|
"go.uber.org/zap/zapcore"
|
||
|
|
"go.uber.org/zap/zaptest/observer"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestChatPubSubService_NilRedisLogsError(t *testing.T) {
|
||
|
|
core, observed := observer.New(zapcore.ErrorLevel)
|
||
|
|
logger := zap.New(core)
|
||
|
|
|
||
|
|
_ = NewChatPubSubService(nil, logger)
|
||
|
|
|
||
|
|
entries := observed.All()
|
||
|
|
assert.Len(t, entries, 1, "constructor should emit exactly one ERROR log when Redis is nil")
|
||
|
|
assert.Equal(t, zapcore.ErrorLevel, entries[0].Level)
|
||
|
|
assert.Contains(t, entries[0].Message, "cross-instance messages will be lost")
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestChatPubSubService_InMemoryFanout(t *testing.T) {
|
||
|
|
svc := NewChatPubSubService(nil, zap.NewNop())
|
||
|
|
|
||
|
|
ctx := context.Background()
|
||
|
|
roomID := uuid.New()
|
||
|
|
|
||
|
|
ch, cancel, err := svc.Subscribe(ctx, roomID)
|
||
|
|
assert.NoError(t, err)
|
||
|
|
defer cancel()
|
||
|
|
|
||
|
|
err = svc.Publish(ctx, roomID, []byte("hello"))
|
||
|
|
assert.NoError(t, err)
|
||
|
|
|
||
|
|
select {
|
||
|
|
case msg := <-ch:
|
||
|
|
assert.Equal(t, "hello", string(msg))
|
||
|
|
default:
|
||
|
|
t.Fatal("expected message on in-memory channel")
|
||
|
|
}
|
||
|
|
}
|