SPB Git

Repository Public

events_test.go 3.8 KB 113 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/events/events_test.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package events_test4344import (45	"context"46	"errors"47	"testing"48	"time"4950	"clustr.spboucher.ai/mcc/internal/events"51	"clustr.spboucher.ai/mcc/internal/testdb"52)5354func TestPublishSinceResumeAndResync(t *testing.T) {55	pool, _ := testdb.New(t)56	ctx, cancel := context.WithCancel(context.Background())57	defer cancel()58	hub := events.NewHub(pool)59	go hub.Run(ctx)60	wake, unsub := hub.Subscribe()61	defer unsub()6263	// An event published in a rolled-back transaction must never be visible.64	tx, _ := pool.Begin(ctx)65	if err := events.Publish(ctx, tx, "node.state", map[string]string{"node": "ghost"}); err != nil {66		t.Fatal(err)67	}68	tx.Rollback(ctx)6970	for i := 0; i < 5; i++ {71		if err := events.Publish(ctx, pool, "node.state", map[string]int{"i": i}); err != nil {72			t.Fatal(err)73		}74	}75	select {76	case <-wake:77	case <-time.After(3 * time.Second):78		t.Fatal("subscriber was not woken")79	}80	all, err := hub.Since(ctx, 0, 100)81	if err != nil || len(all) != 5 {82		t.Fatalf("since 0: %d events, err=%v", len(all), err)83	}84	for i := 1; i < len(all); i++ {85		if all[i].Seq <= all[i-1].Seq {86			t.Fatal("sequences must increase")87		}88	}89	// Resume after the third event: exactly the last two, no duplicates.90	rest, _ := hub.Since(ctx, all[2].Seq, 100)91	if len(rest) != 2 || rest[0].Seq != all[3].Seq {92		t.Fatalf("resume returned %d events", len(rest))93	}94	// Age everything out, prune, and a lagging client is told to resync.95	pool.Exec(ctx, `update events_outbox set ts = now() - interval '2 hours'`)96	if err := events.Prune(ctx, pool); err != nil {97		t.Fatal(err)98	}99	events.Publish(ctx, pool, "node.state", map[string]int{"i": 99})100	if _, err := hub.Since(ctx, all[1].Seq, 100); !errors.Is(err, events.ErrResync) {101		t.Fatalf("expected resync, got %v", err)102	}103	// A client that had seen everything before the prune continues normally.104	got, err := hub.Since(ctx, all[4].Seq, 100)105	if err != nil || len(got) != 1 {106		t.Fatalf("up-to-date client: %d events, err=%v", len(got), err)107	}108	// A position beyond the head (other database generation) also resyncs.109	if _, err := hub.Since(ctx, 1_000_000, 10); !errors.Is(err, events.ErrResync) {110		t.Fatalf("expected resync for future position, got %v", err)111	}112}113
Files main
Menu

Theme