Three defects shared one root cause -- messages had no identity of their own, so the store could not tell a redelivery from a new message: - SaveMessages destroyed history. Writing a batch replaced records that a concurrent write had already stored, and earlier messages vanished. - A redelivered channel message overwrote the stored one, contradicting the append-only sequence in docs/architecture/messaging-and-work-intake .md:112-114. - /compress stranded whatever exceeded one page: deleting 1200 reported 1000 deleted and left 200 behind. Messages now carry an application-generated MessageID, derived from (session, source id) so a redelivery lands on the same record, and stored records are immutable -- a repeat write is a no-op rather than an overwrite. SourceID keeps the channel-native id for correlation without being load-bearing. _ts is written as Unix milliseconds and clamped monotonic per session: Telegram dates are whole seconds, so replies arriving inside the same second would otherwise order arbitrarily. Deletes page with a cursor instead of assuming one page holds everything. Adds a SQLite session store behind the same interface, per docs/architecture/messaging-and-work-intake.md:107. A shared conformance suite runs all 19 groups against both implementations, so the two cannot drift while the NellDB path is still the one wired up.
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// TestSessionTrackerConcurrentResolve pins that the active-session map is
|
|
// safe under the concurrent dispatch the Telegram gateway actually uses:
|
|
// go-telegram/bot runs each update in its own goroutine. Run with -race.
|
|
func TestSessionTrackerConcurrentResolve(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
channels int
|
|
workers int
|
|
}{
|
|
{"single channel contended", 1, 32},
|
|
{"many channels", 16, 32},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := NewSessionStoreMemory("test-node")
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
tracker := newSessionTracker(store)
|
|
|
|
var wg sync.WaitGroup
|
|
for i := range tc.workers {
|
|
wg.Go(func() {
|
|
channel := fmt.Sprintf("chan-%d", i%tc.channels)
|
|
if _, err := tracker.resolve(ctx, "telegram", "bot", channel, ""); err != nil {
|
|
t.Errorf("resolve: %v", err)
|
|
}
|
|
tracker.setActive(channel, "", "override-"+channel)
|
|
_ = tracker.getActive(channel, "")
|
|
})
|
|
}
|
|
wg.Wait()
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestSessionTrackerResolveIsStable pins that resolve returns the same
|
|
// session for the same channel+thread across concurrent callers, so
|
|
// interleaved messages cannot land in different sessions.
|
|
func TestSessionTrackerResolveIsStable(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := NewSessionStoreMemory("test-node")
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
tracker := newSessionTracker(store)
|
|
|
|
const workers = 32
|
|
ids := make([]string, workers)
|
|
var wg sync.WaitGroup
|
|
for i := range workers {
|
|
wg.Go(func() {
|
|
id, err := tracker.resolve(ctx, "telegram", "bot", "chan", "thread")
|
|
if err != nil {
|
|
t.Errorf("resolve: %v", err)
|
|
return
|
|
}
|
|
ids[i] = id
|
|
})
|
|
}
|
|
wg.Wait()
|
|
|
|
for i, id := range ids {
|
|
if id != ids[0] {
|
|
t.Fatalf("worker %d resolved %q, worker 0 resolved %q", i, id, ids[0])
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCanonicalMessageIDIsSessionScoped pins that the same upstream message
|
|
// copied into another session gets its own identity, so branch lineage does
|
|
// not alias across conversations.
|
|
func TestCanonicalMessageIDIsSessionScoped(t *testing.T) {
|
|
a := newMessageID("session-a", "tg-1")
|
|
b := newMessageID("session-b", "tg-1")
|
|
if a == b {
|
|
t.Errorf("sessions a and b both derived %q for the same upstream ID", a)
|
|
}
|
|
if again := newMessageID("session-a", "tg-1"); again != a {
|
|
t.Errorf("derivation is not deterministic: %q then %q", a, again)
|
|
}
|
|
if first, second := newMessageID("session-a", ""), newMessageID("session-a", ""); first == second {
|
|
t.Error("messages with no upstream ID must get distinct identities")
|
|
}
|
|
}
|