mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-10 16:04:04 +00:00
1dd05c75f1
ApplyInitialConnectPlan wrote a payload of {match_id,state,action},
omitting event, revision, resource_id, occurred_at and player_ids --
every field deliverStateOutboxEvent requires. Delivery rejected the row,
dispatch returned on the first error so it was never acknowledged, and
because reads are ordered oldest-first it was retried ahead of every
later state_changed event on every 100ms poll. One initial-connect
transition therefore blocked lifecycle delivery for all matches, not
just its own.
Two independent fixes, since either alone leaves the system fragile:
Build envelopes through one validating helper (MarshalOutboxEnvelope)
and convert all five writers to it. A writer that omits a required
field now fails its own transaction instead of committing a row that
can only ever poison the queue. The helper takes revision as int64 so
the -1 "nothing matched" sentinel some CTEs return surfaces as an error
rather than wrapping to a huge uint64.
Make dispatch resilient regardless: a delivery failure is now counted
against that row and the batch continues, with the row dead-lettered
after MaxOutboxDeliveryAttempts so a poison event degrades to one lost
notification instead of a stalled queue. Ordering within an aggregate
is still honoured -- later events of a failed match are deferred, so no
client observes that match's newer state before its older state. An ack
failure still stops the batch, being a database rather than a payload
problem.
Initial-connect events now address every participant, not just the
connected ones: a no-show needs to learn their ticket was failed and a
penalty applied.
158 lines
6.6 KiB
Go
158 lines
6.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cosmic-clash/cosmic-clash/server/store"
|
|
)
|
|
|
|
func TestDeliverProposalOutboxEventPublishesEveryTarget(t *testing.T) {
|
|
service := &Service{}
|
|
first := service.getEventHub().subscribe("player-a")
|
|
second := service.getEventHub().subscribe("player-b")
|
|
defer service.getEventHub().unsubscribe(first)
|
|
defer service.getEventHub().unsubscribe(second)
|
|
|
|
payload, err := json.Marshal(map[string]any{
|
|
"event": "proposal_changed", "revision": uint64(0), "resource_id": "proposal_1234567890",
|
|
"occurred_at": time.Unix(1000, 0).UTC(), "state": "OPEN", "player_ids": []string{"player-a", "player-b"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := deliverProposalOutboxEvent(context.Background(), store.OutboxEvent{EventID: "event-1", Payload: payload}, service); err != nil {
|
|
t.Fatalf("deliver proposal event: %v", err)
|
|
}
|
|
for name, subscriber := range map[string]*eventSubscriber{"player-a": first, "player-b": second} {
|
|
select {
|
|
case <-subscriber.queue:
|
|
case <-time.After(time.Second):
|
|
t.Fatalf("%s did not receive targeted proposal event", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeliverProposalOutboxEventRejectsMalformedOrUntargetedRows(t *testing.T) {
|
|
service := &Service{}
|
|
for name, event := range map[string]store.OutboxEvent{
|
|
"malformed": {Payload: []byte("{")},
|
|
"wrong event": {Payload: []byte(`{"event":"match_completed","resource_id":"match-1","player_ids":["player-a"]}`)},
|
|
"missing target": {Payload: []byte(`{"event":"proposal_changed","resource_id":"proposal-1","player_ids":[]}`)},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
if err := deliverProposalOutboxEvent(context.Background(), event, service); err == nil {
|
|
t.Fatal("malformed or untargeted event accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeliverResultOutboxEventRejectsMalformedRows(t *testing.T) {
|
|
for _, event := range []store.OutboxEvent{
|
|
{EventType: "proposal_changed", AggregateID: "match-1", Revision: 1, Payload: []byte(`{}`)},
|
|
{EventType: "match_completed", AggregateID: "", Revision: 1, Payload: []byte(`{}`)},
|
|
{EventType: "match_completed", AggregateID: "match-1", Revision: 1, Payload: []byte(`not-json`)},
|
|
} {
|
|
if err := deliverResultOutboxEvent(nil, nil, event, &Service{}); err == nil {
|
|
t.Fatalf("invalid result event accepted: %+v", event)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeliverStateOutboxEventValidatesRevisionAndTargets(t *testing.T) {
|
|
service := &Service{}
|
|
first := service.getEventHub().subscribe("player-a")
|
|
defer service.getEventHub().unsubscribe(first)
|
|
payload := []byte(`{"event":"state_changed","revision":4,"resource_id":"match_1234567890","occurred_at":"1970-01-01T00:16:40Z","state":"ASSIGNMENT_READY","match_id":"match-1","player_ids":["player-a"]}`)
|
|
if err := deliverStateOutboxEvent(context.Background(), store.OutboxEvent{EventType: "state_changed", AggregateID: "match_1234567890", Revision: 4, Payload: payload}, service); err != nil {
|
|
t.Fatalf("valid state event rejected: %v", err)
|
|
}
|
|
select {
|
|
case <-first.queue:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("participant did not receive state event")
|
|
}
|
|
bad := []byte(`{"event":"state_changed","revision":3,"resource_id":"match_1234567890","state":"LIVE","player_ids":["player-a"]}`)
|
|
if err := deliverStateOutboxEvent(context.Background(), store.OutboxEvent{EventType: "state_changed", AggregateID: "match_1234567890", Revision: 4, Payload: bad}, service); err == nil {
|
|
t.Fatal("revision-mismatched state event accepted")
|
|
}
|
|
}
|
|
|
|
func TestDeliverStateOutboxEventRoutesLiveAbandonmentLifecycle(t *testing.T) {
|
|
service := &Service{}
|
|
first := service.getEventHub().subscribe("player-a")
|
|
second := service.getEventHub().subscribe("player-b")
|
|
defer service.getEventHub().unsubscribe(first)
|
|
defer service.getEventHub().unsubscribe(second)
|
|
payload := []byte(`{"event":"state_changed","revision":9,"resource_id":"match_1234567890","occurred_at":"1970-01-01T00:16:40Z","state":"LIVE","match_id":"match_1234567890","player_ids":["player-a","player-b"],"abandoned_player_ids":["player-a"]}`)
|
|
event := store.OutboxEvent{EventType: "state_changed", AggregateID: "match_1234567890", Revision: 9, Payload: payload}
|
|
if err := deliverStateOutboxEvent(context.Background(), event, service); err != nil {
|
|
t.Fatalf("live abandonment event rejected: %v", err)
|
|
}
|
|
for playerID, subscriber := range map[string]*eventSubscriber{"player-a": first, "player-b": second} {
|
|
select {
|
|
case packet := <-subscriber.queue:
|
|
if !json.Valid(packet) {
|
|
t.Fatalf("%s received malformed lifecycle packet %q", playerID, packet)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatalf("%s did not receive live-abandonment lifecycle event", playerID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// One malformed row used to abort the whole batch. Because reads are
|
|
// oldest-first and the row was never acknowledged, it was re-read ahead of
|
|
// every later event of its type on every 100ms poll -- blocking lifecycle
|
|
// delivery for all matches indefinitely, not just its own.
|
|
func TestDispatchOutboxEventsIsNotBlockedByOnePoisonRow(t *testing.T) {
|
|
delivered := []string{}
|
|
acked := []string{}
|
|
dispatcher := &store.OutboxDispatcher{
|
|
Read: func(context.Context, int) ([]store.OutboxEvent, error) { return nil, nil },
|
|
Deliver: func(_ context.Context, event store.OutboxEvent) error {
|
|
delivered = append(delivered, event.EventID)
|
|
if event.AggregateID == "match-poison" {
|
|
return errors.New("invalid state outbox payload")
|
|
}
|
|
return nil
|
|
},
|
|
Ack: func(_ context.Context, eventID string, _ time.Time) error {
|
|
acked = append(acked, eventID)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
events := []store.OutboxEvent{
|
|
{EventID: "poison-1", AggregateID: "match-poison"},
|
|
{EventID: "healthy-1", AggregateID: "match-healthy"},
|
|
{EventID: "poison-2", AggregateID: "match-poison"},
|
|
{EventID: "healthy-2", AggregateID: "match-other"},
|
|
}
|
|
// nil db: the failure counter is exercised against a real PostgreSQL in
|
|
// the store integration tests; here we assert only batch progress.
|
|
err := dispatchOutboxEvents(context.Background(), nil, dispatcher, events)
|
|
if err == nil {
|
|
t.Fatal("expected the delivery failure to be reported to the caller")
|
|
}
|
|
|
|
for _, eventID := range []string{"healthy-1", "healthy-2"} {
|
|
if !slices.Contains(acked, eventID) {
|
|
t.Fatalf("%s was not acknowledged; a poison row still blocks the batch (acked=%v)", eventID, acked)
|
|
}
|
|
}
|
|
if slices.Contains(acked, "poison-1") {
|
|
t.Fatal("a failed delivery must not be acknowledged")
|
|
}
|
|
// Ordering within the failing aggregate is preserved: poison-2 must wait
|
|
// so no client sees that match's newer state before its older state.
|
|
if slices.Contains(delivered, "poison-2") {
|
|
t.Fatalf("later event of a failed aggregate was delivered out of order: %v", delivered)
|
|
}
|
|
}
|