43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/store"
|
|
)
|
|
|
|
func TestOutboxFailureDoesNotStrandUnsentBatch(t *testing.T) {
|
|
st, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer st.Close()
|
|
for i := 0; i < 3; i++ {
|
|
if _, err := st.DB().Exec(`INSERT INTO outbox(event_id,tenant_key,exchange,routing_key,body,status,created_at) VALUES(?,'tenant-a','exchange','key','{}','pending','2026-09-21T00:00:00Z')`, fmt.Sprintf("event-%d", i)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
publisher := &fakePublisher{err: errors.New("confirm unavailable")}
|
|
d, err := New(st, publisher, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := d.FlushOutbox(context.Background(), 3); err == nil {
|
|
t.Fatal("publish failure hidden")
|
|
}
|
|
var stranded int
|
|
if err := st.DB().QueryRow(`SELECT COUNT(*) FROM outbox WHERE status='dispatching'`).Scan(&stranded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stranded != 0 {
|
|
t.Fatalf("%d unsent messages require unnecessary restart", stranded)
|
|
}
|
|
publisher.err = nil
|
|
if count, err := d.FlushOutbox(context.Background(), 3); err != nil || count != 3 {
|
|
t.Fatalf("recovery count=%d err=%v", count, err)
|
|
}
|
|
}
|