package broker import ( "context" "testing" "time" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) func TestConfirmAndRedeliverUnacked(t *testing.T) { ctx, cancel := context.WithTimeout(t.Context(), 2*time.Minute) defer cancel() c, err := testcontainers.Run(ctx, "rabbitmq:4.1.4-alpine", testcontainers.WithEnv(map[string]string{"RABBITMQ_DEFAULT_USER": "lab", "RABBITMQ_DEFAULT_PASS": "test-only"}), testcontainers.WithExposedPorts("5672/tcp"), testcontainers.WithWaitStrategy(wait.ForLog("Server startup complete"))) if err != nil { t.Fatal(err) } testcontainers.CleanupContainer(t, c) endpoint, err := c.PortEndpoint(ctx, "5672/tcp", "") if err != nil { t.Fatal(err) } url := "amqp://lab:test-only@" + endpoint + "/" pub, err := NewPublisher(ctx, url) if err != nil { t.Fatal(err) } defer pub.Close() if err = pub.Publish(ctx, []byte("work")); err != nil { t.Fatal(err) } // Broker redeliveries while PostgreSQL is unreachable do not consume // the application budget. Exercise more than the quorum queue default. for attempt := 0; attempt <= 22; attempt++ { consumer, err := NewConsumer(ctx, url) if err != nil { t.Fatal(err) } select { case msg := <-consumer.Deliveries: if string(msg.Body) != "work" || msg.Redelivered != (attempt > 0) { t.Fatalf("unexpected delivery on attempt %d", attempt) } if attempt == 22 { if err := msg.Ack(false); err != nil { t.Fatal(err) } } case <-time.After(5 * time.Second): consumer.Close() t.Fatalf("confirmed job missing on delivery %d", attempt+1) } consumer.Close() } }