MSP-8299 test move

This commit is contained in:
ziminis committed 2026-11-27 19:08:14 +07:00
1 parent cf57714d14
commit a70a9477b2
7 files changed
+103 -103

No files matched your search

@@ -1,4 +1,4 @@
package event_test
package tests_test
import (
"testing"
@@ -9,30 +9,30 @@ import (
"github.com/stretchr/testify/require"
)
const testModuleId = "test-module-id"
const eventTestModuleId = "test-module-id"
func TestCompactCollapsesModuleLifecycle(t *testing.T) {
t.Parallel()
require := require.New(t)
ready := entity.NewEvent(entity.EventPayload{
ModuleReady: &entity.PayloadModuleReady{ModuleId: testModuleId},
ModuleReady: &entity.PayloadModuleReady{ModuleId: eventTestModuleId},
})
ready.Id = 1
disconnected := entity.NewEvent(entity.EventPayload{
ModuleDisconnected: &entity.PayloadModuleDisconnected{ModuleId: testModuleId},
ModuleDisconnected: &entity.PayloadModuleDisconnected{ModuleId: eventTestModuleId},
})
disconnected.Id = 2
configUpdated := entity.NewEvent(entity.EventPayload{
ConfigUpdated: &entity.PayloadConfigUpdated{ModuleId: testModuleId},
ConfigUpdated: &entity.PayloadConfigUpdated{ModuleId: eventTestModuleId},
})
configUpdated.Id = 3
require.Equal("module_lifecycle_"+testModuleId, ready.Key())
require.Equal("module_lifecycle_"+testModuleId, disconnected.Key())
require.Equal("config_updated_"+testModuleId, configUpdated.Key())
require.Equal("module_lifecycle_"+eventTestModuleId, ready.Key())
require.Equal("module_lifecycle_"+eventTestModuleId, disconnected.Key())
require.Equal("config_updated_"+eventTestModuleId, configUpdated.Key())
compacted := event.NewCompactor().Compact([]entity.Event{ready, disconnected, configUpdated})
@@ -1,4 +1,4 @@
package event_test
package tests_test
import (
"context"
@@ -31,23 +31,23 @@ func (s *subscriptionSpy) NotifyRoutingChanged(ctx context.Context) error {
return nil
}
type loggerSpy struct {
type eventLoggerSpy struct {
errors []string
}
func (l *loggerSpy) Error(ctx context.Context, message any, fields ...log.Field) {
func (l *eventLoggerSpy) Error(ctx context.Context, message any, fields ...log.Field) {
l.errors = append(l.errors, fmt.Sprint(message))
}
func (l *loggerSpy) Warn(ctx context.Context, message any, fields ...log.Field) {}
func (l *eventLoggerSpy) Warn(ctx context.Context, message any, fields ...log.Field) {}
func (l *loggerSpy) Info(ctx context.Context, message any, fields ...log.Field) {}
func (l *eventLoggerSpy) Info(ctx context.Context, message any, fields ...log.Field) {}
func (l *loggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
func (l *eventLoggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
func moduleReadyEvent(id int) entity.Event {
event := entity.NewEvent(entity.EventPayload{
ModuleReady: &entity.PayloadModuleReady{ModuleId: testModuleId},
ModuleReady: &entity.PayloadModuleReady{ModuleId: eventTestModuleId},
})
event.Id = id
return event
@@ -55,7 +55,7 @@ func moduleReadyEvent(id int) entity.Event {
func moduleDisconnectedEvent(id int) entity.Event {
event := entity.NewEvent(entity.EventPayload{
ModuleDisconnected: &entity.PayloadModuleDisconnected{ModuleId: testModuleId},
ModuleDisconnected: &entity.PayloadModuleDisconnected{ModuleId: eventTestModuleId},
})
event.Id = id
return event
@@ -66,7 +66,7 @@ func TestHandleLifecycleBatchFansRoutingOnce(t *testing.T) {
require := require.New(t)
subscription := &subscriptionSpy{}
logger := &loggerSpy{}
logger := &eventLoggerSpy{}
handler := event.NewHandler(subscription, logger)
batch := event.NewCompactor().Compact([]entity.Event{
@@ -75,7 +75,7 @@ func TestHandleLifecycleBatchFansRoutingOnce(t *testing.T) {
})
handler.Handle(t.Context(), batch)
require.Equal([]string{testModuleId}, subscription.backendsChanged,
require.Equal([]string{eventTestModuleId}, subscription.backendsChanged,
"ready and disconnected collapse into the newest lifecycle event")
require.Equal(1, subscription.routingCalls, "the routing map is broadcast once per batch")
require.Empty(logger.errors)
@@ -86,7 +86,7 @@ func TestHandleUnknownEventDoesNotAbortBatch(t *testing.T) {
require := require.New(t)
subscription := &subscriptionSpy{}
logger := &loggerSpy{}
logger := &eventLoggerSpy{}
handler := event.NewHandler(subscription, logger)
unknown := entity.NewEvent(entity.EventPayload{})
@@ -95,7 +95,7 @@ func TestHandleUnknownEventDoesNotAbortBatch(t *testing.T) {
require.Len(logger.errors, 1)
require.Contains(logger.errors[0], "unknown event")
require.Equal([]string{testModuleId}, subscription.backendsChanged,
require.Equal([]string{eventTestModuleId}, subscription.backendsChanged,
"an unknown payload kind must not stop the batch")
require.Equal(1, subscription.routingCalls)
}
@@ -1,4 +1,4 @@
package backend_test
package tests_test
import (
"context"
@@ -14,28 +14,28 @@ import (
)
const (
testModuleName = "test-module"
testModuleId = "test-module-id"
testConnId = "test-conn-id"
testNodeId = "test-node-id"
backendTestModuleName = "test-module"
backendTestModuleId = "test-module-id"
testConnId = "test-conn-id"
testNodeId = "test-node-id"
leaderConnId = "conn-1"
followerConnId = "conn-2"
leaderAddress = "10.0.0.1:9000"
followerAddress = "10.0.0.2:9000"
backendLeaderConnId = "conn-1"
backendFollowerConnId = "conn-2"
backendLeaderAddress = "10.0.0.1:9000"
followerAddress = "10.0.0.2:9000"
)
type storedLog struct {
type backendStoredLog struct {
message any
fields map[string]string
}
type loggerSpy struct {
warns []storedLog
type backendLoggerSpy struct {
warns []backendStoredLog
}
func (l *loggerSpy) Warn(ctx context.Context, message any, fields ...log.Field) {
entry := storedLog{
func (l *backendLoggerSpy) Warn(ctx context.Context, message any, fields ...log.Field) {
entry := backendStoredLog{
message: message,
fields: make(map[string]string, len(fields)),
}
@@ -45,11 +45,11 @@ func (l *loggerSpy) Warn(ctx context.Context, message any, fields ...log.Field)
l.warns = append(l.warns, entry)
}
func (l *loggerSpy) Error(ctx context.Context, message any, fields ...log.Field) {}
func (l *backendLoggerSpy) Error(ctx context.Context, message any, fields ...log.Field) {}
func (l *loggerSpy) Info(ctx context.Context, message any, fields ...log.Field) {}
func (l *backendLoggerSpy) Info(ctx context.Context, message any, fields ...log.Field) {}
func (l *loggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
func (l *backendLoggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
type backendRepoSpy struct {
upserted []entity.Backend
@@ -155,18 +155,18 @@ func TestConnectNormalizesModuleLeaderMode(t *testing.T) {
t.Parallel()
require := require.New(t)
logger := &loggerSpy{}
logger := &backendLoggerSpy{}
backendRepo := &backendRepoSpy{}
service := backend.NewBackend(backendRepo, &eventRepoSpy{}, testNodeId, nil, logger)
declaration := cluster.BackendDeclaration{
ModuleName: testModuleName,
ModuleName: backendTestModuleName,
Version: "1.0.0",
LibVersion: "1.0.0",
Address: cluster.AddressConfiguration{IP: "10.0.0.1", Port: "9000"},
ModuleLeaderMode: tt.announcedMode,
}
connected, err := service.Connect(t.Context(), testConnId, testModuleId, declaration)
connected, err := service.Connect(t.Context(), testConnId, backendTestModuleId, declaration)
require.NoError(err)
require.Len(backendRepo.upserted, 1)
@@ -179,7 +179,7 @@ func TestConnectNormalizesModuleLeaderMode(t *testing.T) {
}
require.Len(logger.warns, 1)
require.Equal(testModuleName, logger.warns[0].fields["moduleName"])
require.Equal(backendTestModuleName, logger.warns[0].fields["moduleName"])
require.Equal(tt.announcedMode, logger.warns[0].fields["moduleLeaderMode"])
})
}
@@ -190,20 +190,20 @@ func TestConnectInsertsOnlyLifecycleEvent(t *testing.T) {
require := require.New(t)
events := &eventRepoSpy{}
backendRepo := newBackendRepoSpy(electedInstance(leaderConnId, leaderAddress))
service := backend.NewBackend(backendRepo, events, testNodeId, nil, &loggerSpy{})
backendRepo := newBackendRepoSpy(backendElectedInstance(backendLeaderConnId, backendLeaderAddress))
service := backend.NewBackend(backendRepo, events, testNodeId, nil, &backendLoggerSpy{})
declaration := cluster.BackendDeclaration{
ModuleName: testModuleName,
ModuleName: backendTestModuleName,
Address: cluster.AddressConfiguration{IP: "10.0.0.2", Port: "9000"},
ModuleLeaderMode: cluster.ModuleLeaderModeRequestElection,
}
_, err := service.Connect(t.Context(), followerConnId, testModuleId, declaration)
_, err := service.Connect(t.Context(), backendFollowerConnId, backendTestModuleId, declaration)
require.NoError(err)
require.Equal([]string{"upsert"}, backendRepo.calls,
"the connect path elects nobody: the leader is derived where the address list is emitted")
require.Equal([]string{"module_lifecycle_" + testModuleId}, eventKeys(events),
require.Equal([]string{"module_lifecycle_" + backendTestModuleId}, eventKeys(events),
"one lifecycle event per connect, no dedicated leader event")
}
@@ -213,21 +213,21 @@ func TestDisconnectInsertsOnlyLifecycleEvent(t *testing.T) {
events := &eventRepoSpy{}
backendRepo := newBackendRepoSpy(
electedInstance(leaderConnId, leaderAddress),
electedInstance(followerConnId, followerAddress),
backendElectedInstance(backendLeaderConnId, backendLeaderAddress),
backendElectedInstance(backendFollowerConnId, followerAddress),
)
service := backend.NewBackend(backendRepo, events, testNodeId, nil, &loggerSpy{})
service := backend.NewBackend(backendRepo, events, testNodeId, nil, &backendLoggerSpy{})
err := service.Disconnect(t.Context(), entity.Backend{
WsConnectionId: leaderConnId,
ModuleId: testModuleId,
ModuleName: testModuleName,
WsConnectionId: backendLeaderConnId,
ModuleId: backendTestModuleId,
ModuleName: backendTestModuleName,
})
require.NoError(err)
require.Equal([]string{"delete"}, backendRepo.calls,
"the disconnect path elects nobody")
require.Equal([]string{"module_lifecycle_" + testModuleId}, eventKeys(events),
require.Equal([]string{"module_lifecycle_" + backendTestModuleId}, eventKeys(events),
"one lifecycle event per disconnect, no dedicated leader event")
}
@@ -237,10 +237,10 @@ func TestClearPhantomBackendsInsertsOneLifecycleEventPerModule(t *testing.T) {
events := &eventRepoSpy{}
backendRepo := newBackendRepoSpy(
electedInstance(leaderConnId, leaderAddress),
electedInstance(followerConnId, followerAddress),
backendElectedInstance(backendLeaderConnId, backendLeaderAddress),
backendElectedInstance(backendFollowerConnId, followerAddress),
)
service := backend.NewBackend(backendRepo, events, testNodeId, &etp.Rooms{}, &loggerSpy{})
service := backend.NewBackend(backendRepo, events, testNodeId, &etp.Rooms{}, &backendLoggerSpy{})
deleted, err := service.ClearPhantomBackends(t.Context())
@@ -248,7 +248,7 @@ func TestClearPhantomBackendsInsertsOneLifecycleEventPerModule(t *testing.T) {
require.Equal(2, deleted)
require.Equal([]string{"own_node", "delete"}, backendRepo.calls,
"the cleanup deletes and announces, it elects nobody")
require.Equal([]string{"module_lifecycle_" + testModuleId}, eventKeys(events),
require.Equal([]string{"module_lifecycle_" + backendTestModuleId}, eventKeys(events),
"the instances of one module are announced once")
}
@@ -257,18 +257,18 @@ func TestDeleteOwnBackendsInsertsNoEvents(t *testing.T) {
require := require.New(t)
events := &eventRepoSpy{}
service := backend.NewBackend(newBackendRepoSpy(), events, testNodeId, nil, &loggerSpy{})
service := backend.NewBackend(newBackendRepoSpy(), events, testNodeId, nil, &backendLoggerSpy{})
service.DeleteOwnBackends(t.Context())
require.Empty(events.inserted, "graceful shutdown inserts no events at all")
}
func electedInstance(connId string, address string) entity.Backend {
func backendElectedInstance(connId string, address string) entity.Backend {
return entity.Backend{
WsConnectionId: connId,
ModuleId: testModuleId,
ModuleName: testModuleName,
ModuleId: backendTestModuleId,
ModuleName: backendTestModuleName,
Address: address,
ModuleLeaderMode: cluster.ModuleLeaderModeRequestElection,
}
@@ -1,4 +1,4 @@
package leader_test
package tests_test
import (
"slices"
@@ -1,4 +1,4 @@
package module_test
package tests_test
import (
"context"
@@ -1,4 +1,4 @@
package subscription_test
package tests_test
import (
"context"
@@ -14,28 +14,28 @@ import (
)
const (
testModuleId = "test-module-id"
testModuleName = "test-module"
subscriptionTestModuleId = "test-module-id"
subscriptionTestModuleName = "test-module"
leaderConnId = "conn-1"
followerConnId = "conn-2"
subscriptionLeaderConnId = "conn-1"
subscriptionFollowerConnId = "conn-2"
leaderAddress = "10.0.0.1:9000"
successorAddress = "10.0.0.2:9000"
plainAddress = "10.0.0.3:9000"
subscriptionLeaderAddress = "10.0.0.1:9000"
successorAddress = "10.0.0.2:9000"
plainAddress = "10.0.0.3:9000"
)
type storedLog struct {
type subscriptionStoredLog struct {
message any
fields map[string]string
}
type loggerSpy struct {
infos []storedLog
type subscriptionLoggerSpy struct {
infos []subscriptionStoredLog
}
func (l *loggerSpy) Info(ctx context.Context, message any, fields ...log.Field) {
entry := storedLog{
func (l *subscriptionLoggerSpy) Info(ctx context.Context, message any, fields ...log.Field) {
entry := subscriptionStoredLog{
message: message,
fields: make(map[string]string, len(fields)),
}
@@ -45,32 +45,32 @@ func (l *loggerSpy) Info(ctx context.Context, message any, fields ...log.Field)
l.infos = append(l.infos, entry)
}
func (l *loggerSpy) Error(ctx context.Context, message any, fields ...log.Field) {}
func (l *subscriptionLoggerSpy) Error(ctx context.Context, message any, fields ...log.Field) {}
func (l *loggerSpy) Warn(ctx context.Context, message any, fields ...log.Field) {}
func (l *subscriptionLoggerSpy) Warn(ctx context.Context, message any, fields ...log.Field) {}
func (l *loggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
func (l *subscriptionLoggerSpy) Debug(ctx context.Context, message any, fields ...log.Field) {}
type moduleRepoSpy struct{}
func (r *moduleRepoSpy) GetByNames(ctx context.Context, names []string) ([]entity.Module, error) {
return []entity.Module{{Id: testModuleId, Name: testModuleName}}, nil
return []entity.Module{{Id: subscriptionTestModuleId, Name: subscriptionTestModuleName}}, nil
}
func (r *moduleRepoSpy) GetById(ctx context.Context, id string) (*entity.Module, error) {
return &entity.Module{Id: testModuleId, Name: testModuleName}, nil
return &entity.Module{Id: subscriptionTestModuleId, Name: subscriptionTestModuleName}, nil
}
type backendRepoSpy struct {
type subscriptionBackendRepoSpy struct {
forLeaderElection []entity.Backend
all []entity.Backend
}
func (r *backendRepoSpy) GetForLeaderElection(ctx context.Context, moduleId string) ([]entity.Backend, error) {
func (r *subscriptionBackendRepoSpy) GetForLeaderElection(ctx context.Context, moduleId string) ([]entity.Backend, error) {
return r.forLeaderElection, nil
}
func (r *backendRepoSpy) All(ctx context.Context) ([]entity.Backend, error) {
func (r *subscriptionBackendRepoSpy) All(ctx context.Context) ([]entity.Backend, error) {
return r.all, nil
}
@@ -96,23 +96,23 @@ func TestNotifyBackendsChangedLogsTheLeaderItPublishes(t *testing.T) {
t.Parallel()
require := require.New(t)
logger := &loggerSpy{}
logger := &subscriptionLoggerSpy{}
rooms := etp.NewServer().Rooms()
rooms.Join(&etp.Conn{}, subscription.BackendsChangingRoom(testModuleName))
service := newService(logger, rooms, &backendRepoSpy{
rooms.Join(&etp.Conn{}, subscription.BackendsChangingRoom(subscriptionTestModuleName))
service := newService(logger, rooms, &subscriptionBackendRepoSpy{
forLeaderElection: []entity.Backend{
electedInstance(leaderConnId, leaderAddress),
electedInstance(followerConnId, successorAddress),
subscriptionElectedInstance(subscriptionLeaderConnId, subscriptionLeaderAddress),
subscriptionElectedInstance(subscriptionFollowerConnId, successorAddress),
},
})
err := service.NotifyBackendsChanged(t.Context(), testModuleId)
err := service.NotifyBackendsChanged(t.Context(), subscriptionTestModuleId)
require.NoError(err)
require.Len(logger.infos, 1)
require.Equal("module address list narrowed to the leader", logger.infos[0].message)
require.Equal(testModuleName, logger.infos[0].fields["moduleName"])
require.Equal(leaderAddress, logger.infos[0].fields["leaderAddress"],
require.Equal(subscriptionTestModuleName, logger.infos[0].fields["moduleName"])
require.Equal(subscriptionLeaderAddress, logger.infos[0].fields["leaderAddress"],
"the logged leader is the oldest elected instance, the one the subscriber received")
}
@@ -120,13 +120,13 @@ func TestNotifyRoutingChangedLogsPublishedLeaders(t *testing.T) {
t.Parallel()
require := require.New(t)
logger := &loggerSpy{}
logger := &subscriptionLoggerSpy{}
rooms := etp.NewServer().Rooms()
rooms.Join(&etp.Conn{}, subscription.RoutingChangingRoom())
service := newService(logger, rooms, &backendRepoSpy{
service := newService(logger, rooms, &subscriptionBackendRepoSpy{
all: []entity.Backend{
electedInstance(leaderConnId, leaderAddress),
electedInstance(followerConnId, successorAddress),
subscriptionElectedInstance(subscriptionLeaderConnId, subscriptionLeaderAddress),
subscriptionElectedInstance(subscriptionFollowerConnId, successorAddress),
plainInstance("conn-3", plainAddress),
},
})
@@ -136,7 +136,7 @@ func TestNotifyRoutingChangedLogsPublishedLeaders(t *testing.T) {
require.NoError(err)
require.Len(logger.infos, 1, "one summary line per routing rebuild")
require.Equal("routing map publishes module leaders", logger.infos[0].message)
require.Equal(testModuleName+"="+leaderAddress, logger.infos[0].fields["moduleLeaders"],
require.Equal(subscriptionTestModuleName+"="+subscriptionLeaderAddress, logger.infos[0].fields["moduleLeaders"],
"only the elected module and only its leader are published, the plain module is not listed")
}
@@ -144,10 +144,10 @@ func TestNotifyRoutingChangedWithoutElectionLogsNothing(t *testing.T) {
t.Parallel()
require := require.New(t)
logger := &loggerSpy{}
logger := &subscriptionLoggerSpy{}
rooms := etp.NewServer().Rooms()
rooms.Join(&etp.Conn{}, subscription.RoutingChangingRoom())
service := newService(logger, rooms, &backendRepoSpy{
service := newService(logger, rooms, &subscriptionBackendRepoSpy{
all: []entity.Backend{plainInstance("conn-3", plainAddress)},
})
@@ -157,11 +157,11 @@ func TestNotifyRoutingChangedWithoutElectionLogsNothing(t *testing.T) {
require.Empty(logger.infos, "modules without election keep the previous behaviour, including the log")
}
func electedInstance(connId string, address string) entity.Backend {
func subscriptionElectedInstance(connId string, address string) entity.Backend {
return entity.Backend{
WsConnectionId: connId,
ModuleId: testModuleId,
ModuleName: testModuleName,
ModuleId: subscriptionTestModuleId,
ModuleName: subscriptionTestModuleName,
Address: address,
ModuleLeaderMode: cluster.ModuleLeaderModeRequestElection,
}
@@ -1,4 +1,4 @@
package variable_test
package tests_test
import (
"encoding/json"