152 lines
5.5 KiB
Go
152 lines
5.5 KiB
Go
package runtimemanager_test
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"galaxy/lobby/internal/adapters/runtimemanager"
|
|
"galaxy/lobby/internal/ports"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestPublisher(t *testing.T, clock func() time.Time) (*runtimemanager.Publisher, *miniredis.Miniredis, *redis.Client) {
|
|
t.Helper()
|
|
|
|
server := miniredis.RunT(t)
|
|
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
|
t.Cleanup(func() { _ = client.Close() })
|
|
|
|
publisher, err := runtimemanager.NewPublisher(runtimemanager.Config{
|
|
Client: client,
|
|
StartJobsStream: "runtime:start_jobs",
|
|
StopJobsStream: "runtime:stop_jobs",
|
|
Clock: clock,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
return publisher, server, client
|
|
}
|
|
|
|
func TestPublisherRejectsInvalidConfig(t *testing.T) {
|
|
_, err := runtimemanager.NewPublisher(runtimemanager.Config{
|
|
StartJobsStream: "runtime:start_jobs",
|
|
StopJobsStream: "runtime:stop_jobs",
|
|
})
|
|
require.Error(t, err)
|
|
|
|
server := miniredis.RunT(t)
|
|
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
|
t.Cleanup(func() { _ = client.Close() })
|
|
|
|
_, err = runtimemanager.NewPublisher(runtimemanager.Config{
|
|
Client: client,
|
|
StopJobsStream: "runtime:stop_jobs",
|
|
})
|
|
require.Error(t, err)
|
|
|
|
_, err = runtimemanager.NewPublisher(runtimemanager.Config{
|
|
Client: client,
|
|
StartJobsStream: "runtime:start_jobs",
|
|
})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestPublishStartJobAppendsToStartStream(t *testing.T) {
|
|
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
|
|
publisher, _, client := newTestPublisher(t, func() time.Time { return now })
|
|
|
|
require.NoError(t, publisher.PublishStartJob(context.Background(), "game-1", "galaxy/game:v1.0.0"))
|
|
|
|
entries, err := client.XRange(context.Background(), "runtime:start_jobs", "-", "+").Result()
|
|
require.NoError(t, err)
|
|
require.Len(t, entries, 1)
|
|
assert.Equal(t, "game-1", entries[0].Values["game_id"])
|
|
assert.Equal(t, "galaxy/game:v1.0.0", entries[0].Values["image_ref"])
|
|
assert.Equal(t, strconv.FormatInt(now.UnixMilli(), 10), entries[0].Values["requested_at_ms"])
|
|
|
|
stop, err := client.XLen(context.Background(), "runtime:stop_jobs").Result()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), stop, "stop stream must remain empty")
|
|
}
|
|
|
|
func TestPublisherStartJobIncludesImageRef(t *testing.T) {
|
|
publisher, _, client := newTestPublisher(t, nil)
|
|
|
|
require.NoError(t, publisher.PublishStartJob(context.Background(), "game-1", "registry.example.com/galaxy/game:v1.4.7"))
|
|
|
|
entries, err := client.XRange(context.Background(), "runtime:start_jobs", "-", "+").Result()
|
|
require.NoError(t, err)
|
|
require.Len(t, entries, 1)
|
|
assert.Equal(t, "registry.example.com/galaxy/game:v1.4.7", entries[0].Values["image_ref"],
|
|
"image_ref field must be present in the start envelope")
|
|
}
|
|
|
|
func TestPublishStopJobAppendsToStopStream(t *testing.T) {
|
|
now := time.Date(2026, 4, 25, 13, 0, 0, 0, time.UTC)
|
|
publisher, _, client := newTestPublisher(t, func() time.Time { return now })
|
|
|
|
require.NoError(t, publisher.PublishStopJob(context.Background(), "game-2", ports.StopReasonOrphanCleanup))
|
|
|
|
entries, err := client.XRange(context.Background(), "runtime:stop_jobs", "-", "+").Result()
|
|
require.NoError(t, err)
|
|
require.Len(t, entries, 1)
|
|
assert.Equal(t, "game-2", entries[0].Values["game_id"])
|
|
assert.Equal(t, "orphan_cleanup", entries[0].Values["reason"])
|
|
assert.Equal(t, strconv.FormatInt(now.UnixMilli(), 10), entries[0].Values["requested_at_ms"])
|
|
|
|
startLen, err := client.XLen(context.Background(), "runtime:start_jobs").Result()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), startLen, "start stream must remain empty")
|
|
}
|
|
|
|
func TestPublisherStopJobIncludesReason(t *testing.T) {
|
|
publisher, _, client := newTestPublisher(t, nil)
|
|
|
|
require.NoError(t, publisher.PublishStopJob(context.Background(), "game-2", ports.StopReasonCancelled))
|
|
|
|
entries, err := client.XRange(context.Background(), "runtime:stop_jobs", "-", "+").Result()
|
|
require.NoError(t, err)
|
|
require.Len(t, entries, 1)
|
|
assert.Equal(t, "cancelled", entries[0].Values["reason"],
|
|
"reason field must be present in the stop envelope")
|
|
}
|
|
|
|
func TestPublishRejectsEmptyGameID(t *testing.T) {
|
|
publisher, _, _ := newTestPublisher(t, nil)
|
|
|
|
require.Error(t, publisher.PublishStartJob(context.Background(), "", "galaxy/game:v1.0.0"))
|
|
require.Error(t, publisher.PublishStopJob(context.Background(), " ", ports.StopReasonCancelled))
|
|
}
|
|
|
|
func TestPublishStartJobRejectsEmptyImageRef(t *testing.T) {
|
|
publisher, _, _ := newTestPublisher(t, nil)
|
|
|
|
require.Error(t, publisher.PublishStartJob(context.Background(), "game-1", ""))
|
|
require.Error(t, publisher.PublishStartJob(context.Background(), "game-1", " "))
|
|
}
|
|
|
|
func TestPublishStopJobRejectsUnknownReason(t *testing.T) {
|
|
publisher, _, _ := newTestPublisher(t, nil)
|
|
|
|
require.Error(t, publisher.PublishStopJob(context.Background(), "game-1", ports.StopReason("")))
|
|
require.Error(t, publisher.PublishStopJob(context.Background(), "game-1", ports.StopReason("unknown_reason")))
|
|
}
|
|
|
|
func TestPublishRejectsNilContext(t *testing.T) {
|
|
publisher, _, _ := newTestPublisher(t, nil)
|
|
|
|
require.Error(t, publisher.PublishStartJob(nilContext(), "game-1", "galaxy/game:v1.0.0"))
|
|
require.Error(t, publisher.PublishStopJob(nilContext(), "game-1", ports.StopReasonCancelled))
|
|
}
|
|
|
|
// nilContext returns an explicit untyped nil to exercise the defensive
|
|
// nil-context guards on Publisher methods. The indirection silences the
|
|
// SA1012 hint where it is intentional.
|
|
func nilContext() context.Context { return nil }
|