Files
go-sip/internal/dispatcher/sip_only_test.go
T

68 lines
2.1 KiB
Go

package dispatcher
import (
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"git.ipao.vip/rogee/go-sip/internal/configread"
"git.ipao.vip/rogee/go-sip/internal/store"
)
func TestSIPOnlyNotificationCheckpointsNativeLoadWithoutOpeningBusiness(t *testing.T) {
const id = "c046b893-8628-4589-ae50-619d049248a6"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/internal/v1/dispatcher/sip" {
t.Errorf("SIP-only fetched business config %s", r.URL.Path)
w.WriteHeader(404)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(configExample(t, "config-read-sip"))
}))
defer server.Close()
client, err := configread.NewClient(server.URL, id, "test-secret", server.Client())
if err != nil {
t.Fatal(err)
}
db, err := store.Open(filepath.Join(t.TempDir(), "sip-only.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
applied := false
syncer := SIPOnly{DispatcherID: id, Store: db, Client: client,
ApplySIP: func(_ context.Context, sip configread.SIP) error {
applied = true
if sip.Revision != 8 {
t.Fatal("unexpected SIP revision")
}
return nil
},
VerifySIP: func(_ context.Context, sip configread.SIP) error {
if !applied {
t.Fatal("verified before native apply")
}
return nil
},
}
body := configExample(t, "mq-sip-change")
if err := syncer.HandleNotification(context.Background(), "d."+id+".control.in", body); err != nil {
t.Fatal(err)
}
if appliedRev, pending, err := db.SIPState(id); err != nil || appliedRev != 0 || pending != 8 {
t.Fatalf("notification was not durably fenced: %d %d %v", appliedRev, pending, err)
}
if err := syncer.Sync(context.Background()); err != nil {
t.Fatal(err)
}
if appliedRev, pending, err := db.SIPState(id); err != nil || appliedRev != 8 || pending != 0 {
t.Fatalf("verified readback was not recorded: %d %d %v", appliedRev, pending, err)
}
if err := syncer.HandleNotification(context.Background(), "d."+id+".control.in", configExample(t, "mq-control")); err == nil {
t.Fatal("SIP-only consumed a business control event")
}
}