|
| 1 | +package rabbitmq |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const enableDockerIntegrationTestsFlag = `ENABLE_DOCKER_INTEGRATION_TESTS` |
| 14 | + |
| 15 | +func prepareDockerTest(t *testing.T) (connStr string) { |
| 16 | + if v, ok := os.LookupEnv(enableDockerIntegrationTestsFlag); !ok || strings.ToUpper(v) != "TRUE" { |
| 17 | + t.Skipf("integration tests are only run if '%s' is TRUE", enableDockerIntegrationTestsFlag) |
| 18 | + return |
| 19 | + } |
| 20 | + ctx, cancel := context.WithCancel(context.Background()) |
| 21 | + defer cancel() |
| 22 | + |
| 23 | + out, err := exec.CommandContext(ctx, "docker", "run", "--rm", "--detach", "--publish=5672:5672", "--quiet", "--", "rabbitmq:3-alpine").Output() |
| 24 | + if err != nil { |
| 25 | + t.Log("container id", string(out)) |
| 26 | + t.Fatalf("error launching rabbitmq in docker: %v", err) |
| 27 | + } |
| 28 | + t.Cleanup(func() { |
| 29 | + containerId := strings.TrimSpace(string(out)) |
| 30 | + t.Logf("attempting to shutdown container '%s'", containerId) |
| 31 | + if err := exec.Command("docker", "rm", "--force", containerId).Run(); err != nil { |
| 32 | + t.Logf("failed to stop: %v", err) |
| 33 | + } |
| 34 | + }) |
| 35 | + return "amqp://guest:guest@localhost:5672/" |
| 36 | +} |
| 37 | + |
| 38 | +func waitForHealthyAmqp(t *testing.T, connStr string) *Conn { |
| 39 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 40 | + defer cancel() |
| 41 | + tkr := time.NewTicker(time.Second) |
| 42 | + |
| 43 | + // only log connection-level logs when connection has succeeded |
| 44 | + muted := true |
| 45 | + connLogger := simpleLogF(func(s string, i ...interface{}) { |
| 46 | + if !muted { |
| 47 | + t.Logf(s, i...) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + var lastErr error |
| 52 | + for { |
| 53 | + select { |
| 54 | + case <-ctx.Done(): |
| 55 | + t.Fatal("timed out waiting for healthy amqp", lastErr) |
| 56 | + return nil |
| 57 | + case <-tkr.C: |
| 58 | + t.Log("attempting connection") |
| 59 | + conn, err := NewConn(connStr, WithConnectionOptionsLogger(connLogger)) |
| 60 | + if err != nil { |
| 61 | + lastErr = err |
| 62 | + t.Log("connection attempt failed - retrying") |
| 63 | + } else { |
| 64 | + if err := func() error { |
| 65 | + pub, err := NewPublisher(conn, WithPublisherOptionsLogger(simpleLogF(t.Logf))) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("failed to setup publisher: %v", err) |
| 68 | + } |
| 69 | + t.Log("attempting publish") |
| 70 | + return pub.PublishWithContext(ctx, []byte{}, []string{"ping"}, WithPublishOptionsExchange("")) |
| 71 | + }(); err != nil { |
| 72 | + _ = conn.Close() |
| 73 | + t.Log("publish ping failed", err.Error()) |
| 74 | + } else { |
| 75 | + t.Log("ping successful") |
| 76 | + muted = true |
| 77 | + return conn |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestSimplePubSub is an integration testing function that validates whether we can reliably connect to a docker-based |
| 85 | +// rabbitmq and consumer a message that we publish. This uses the default direct exchange with lots of error checking |
| 86 | +// to ensure the result is as expected. |
| 87 | +func TestSimplePubSub(t *testing.T) { |
| 88 | + connStr := prepareDockerTest(t) |
| 89 | + conn := waitForHealthyAmqp(t, connStr) |
| 90 | + defer conn.Close() |
| 91 | + |
| 92 | + t.Logf("new consumer") |
| 93 | + consumerQueue := "my_queue" |
| 94 | + consumer, err := NewConsumer(conn, consumerQueue, WithConsumerOptionsLogger(simpleLogF(t.Logf))) |
| 95 | + if err != nil { |
| 96 | + t.Fatal("error creating consumer", err) |
| 97 | + } |
| 98 | + defer consumer.CloseWithContext(context.Background()) |
| 99 | + |
| 100 | + // Setup a consumer which pushes each of its consumed messages over the channel. If the channel is closed or full |
| 101 | + // it does not block. |
| 102 | + consumed := make(chan Delivery) |
| 103 | + defer close(consumed) |
| 104 | + |
| 105 | + go func() { |
| 106 | + err = consumer.Run(func(d Delivery) Action { |
| 107 | + t.Log("consumed") |
| 108 | + select { |
| 109 | + case consumed <- d: |
| 110 | + default: |
| 111 | + } |
| 112 | + return Ack |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + t.Log("consumer run failed", err) |
| 116 | + } |
| 117 | + }() |
| 118 | + |
| 119 | + // Setup a publisher with notifications enabled |
| 120 | + t.Logf("new publisher") |
| 121 | + publisher, err := NewPublisher(conn, WithPublisherOptionsLogger(simpleLogF(t.Logf))) |
| 122 | + if err != nil { |
| 123 | + t.Fatal("error creating publisher", err) |
| 124 | + } |
| 125 | + publisher.NotifyPublish(func(p Confirmation) { |
| 126 | + }) |
| 127 | + defer publisher.Close() |
| 128 | + |
| 129 | + // For test stability we cannot rely on the fact that the consumer go routines are up and running before the |
| 130 | + // publisher starts it's first publish attempt. For this reason we run the publisher in a loop every second and |
| 131 | + // pass after we see the first message come through. |
| 132 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 133 | + defer cancel() |
| 134 | + tkr := time.NewTicker(time.Second) |
| 135 | + for { |
| 136 | + select { |
| 137 | + case <-ctx.Done(): |
| 138 | + t.Fatal("timed out waiting for pub sub", ctx.Err()) |
| 139 | + case <-tkr.C: |
| 140 | + t.Logf("new publish") |
| 141 | + confirms, err := publisher.PublishWithDeferredConfirmWithContext(ctx, []byte("example"), []string{consumerQueue}) |
| 142 | + if err != nil { |
| 143 | + // publish should always succeed since we've verified the ping previously |
| 144 | + t.Fatal("failed to publish", err) |
| 145 | + } |
| 146 | + for _, confirm := range confirms { |
| 147 | + if _, err := confirm.WaitContext(ctx); err != nil { |
| 148 | + t.Fatal("failed to wait for publish", err) |
| 149 | + } |
| 150 | + } |
| 151 | + case d := <-consumed: |
| 152 | + t.Logf("successfully saw message round trip: '%s'", string(d.Body)) |
| 153 | + return |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments