fix(go): replay raw before unified events

This commit is contained in:
lingniu
2026-07-02 12:18:37 +08:00
parent 7a9a6f441a
commit ba5a28e636
2 changed files with 71 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package eventbus
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
@@ -70,6 +71,30 @@ func TestDurableSinkReplayPublishesInFileOrderAndDeletesFiles(t *testing.T) {
}
}
func TestDurableSinkReplayPublishesRawBeforeUnifiedEvenWhenFilesAreOutOfOrder(t *testing.T) {
dir := t.TempDir()
env := durableTestEnvelope()
writeDurableRecord(t, filepath.Join(dir, "0001-unified.json"), durableRecord{Kind: "unified", Envelope: env})
writeDurableRecord(t, filepath.Join(dir, "0002-raw.json"), durableRecord{Kind: "raw", Envelope: env})
delegate := &scriptedSink{}
sink := NewDurableSink(delegate, DurableConfig{Directory: dir})
if err := sink.ReplayOnce(context.Background()); err != nil {
t.Fatalf("ReplayOnce() error = %v", err)
}
got := delegate.calls
want := []string{"raw", "unified"}
if len(got) != len(want) {
t.Fatalf("calls = %#v, want %#v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("calls[%d] = %q, want %q; all=%#v", i, got[i], want, got)
}
}
}
func durableTestEnvelope() envelope.FrameEnvelope {
return envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
@@ -83,6 +108,17 @@ func durableTestEnvelope() envelope.FrameEnvelope {
}
}
func writeDurableRecord(t *testing.T, path string, record durableRecord) {
t.Helper()
payload, err := json.Marshal(record)
if err != nil {
t.Fatalf("marshal durable record: %v", err)
}
if err := os.WriteFile(path, payload, 0o640); err != nil {
t.Fatalf("write durable record: %v", err)
}
}
func spoolFiles(t *testing.T, dir string) []string {
t.Helper()
matches, err := filepath.Glob(filepath.Join(dir, "*.json"))