61 lines
2.4 KiB
Go
61 lines
2.4 KiB
Go
package mq
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/logging"
|
|
"git.ipao.vip/rogee/go-sip/internal/tenant"
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
func TestMergedRejectionKeepsReceiveLogsWireBytesAndDurableAck(t *testing.T) {
|
|
id := "c046b893-8628-4589-ae50-619d049248a6"
|
|
route, err := tenant.ControlRoute(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, debug := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "normal", true: "debug"}[debug], func(t *testing.T) {
|
|
previous := slog.Default()
|
|
defer slog.SetDefault(previous)
|
|
var output bytes.Buffer
|
|
logging.Configure("dispatcher", debug, &output)
|
|
body := []byte(`{"event_id":"merge-rejection","event_type":"task.control","dispatcher_id":"` + id + `","tenant_id":1001,"payload":{"task_id":"4","action":"pause"},"api_key":"private-merge-key"}`)
|
|
original := bytes.Clone(body)
|
|
ack := &logAck{}
|
|
broker := &Broker{dispatcherID: id, controlQueue: route.Queue}
|
|
handled := false
|
|
broker.SetInvalidMessageHandler(func(_ context.Context, queue, key string, raw []byte, cause error) error {
|
|
if queue != route.Queue || key != route.BindingKey || !bytes.Equal(raw, original) || cause == nil || ack.acks != 0 {
|
|
t.Fatal("merged receive path lost original bytes/cause or acknowledged before handling")
|
|
}
|
|
handled = true
|
|
return nil
|
|
})
|
|
deliveries := make(chan amqp.Delivery, 1)
|
|
deliveries <- amqp.Delivery{Body: body, RoutingKey: route.BindingKey, DeliveryTag: 1, Acknowledger: ack}
|
|
close(deliveries)
|
|
err := broker.consumeDeliveries(context.Background(), id, route.Queue, deliveries, func(context.Context, string, []byte) error {
|
|
t.Fatal("invalid message reached the business handler")
|
|
return nil
|
|
})
|
|
if err == nil || !handled || ack.acks != 1 || ack.rejects != 0 || ack.nacks != 0 || !bytes.Equal(body, original) {
|
|
t.Fatalf("merged durable handling failed: %v %+v", err, ack)
|
|
}
|
|
text := output.String()
|
|
for _, marker := range []string{"received", "rejected_handled", "ack_sent", "merge-rejection", "issued_at"} {
|
|
if !strings.Contains(text, marker) {
|
|
t.Fatalf("missing %s in merged workflow: %s", marker, text)
|
|
}
|
|
}
|
|
if strings.Contains(text, "private-merge-key") || strings.Contains(text, `\"action\":\"pause\"`) != debug {
|
|
t.Fatal("merged logging changed privacy/mode behavior", text)
|
|
}
|
|
})
|
|
}
|
|
}
|