| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package rabbitpubsub |
| 16 |
|
| 17 |
// To run these tests against a real RabbitMQ server, first run localrabbit.sh. |
| 18 |
// Then wait a few seconds for the server to be ready. |
| 19 |
// If no server is running, the tests will use a fake (see fake_test.go). |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"os" |
| 27 |
"strings" |
| 28 |
"sync" |
| 29 |
"sync/atomic" |
| 30 |
"testing" |
| 31 |
"time" |
| 32 |
|
| 33 |
amqp "github.com/rabbitmq/amqp091-go" |
| 34 |
"gocloud.dev/gcerrors" |
| 35 |
"gocloud.dev/internal/testing/setup" |
| 36 |
"gocloud.dev/pubsub" |
| 37 |
"gocloud.dev/pubsub/driver" |
| 38 |
"gocloud.dev/pubsub/drivertest" |
| 39 |
) |
| 40 |
|
| 41 |
const rabbitURL = "amqp://guest:guest@localhost:5672/" |
| 42 |
|
| 43 |
var logOnce sync.Once |
| 44 |
|
| 45 |
func mustDialRabbit(t testing.TB) amqpConnection { |
| 46 |
t.Helper() |
| 47 |
|
| 48 |
if !setup.HasDockerTestEnvironment() { |
| 49 |
logOnce.Do(func() { |
| 50 |
t.Log("using the fake because the RabbitMQ server is not available") |
| 51 |
}) |
| 52 |
return newFakeConnection() |
| 53 |
} |
| 54 |
conn, err := amqp.Dial(rabbitURL) |
| 55 |
if err != nil { |
| 56 |
t.Fatal(err) |
| 57 |
} |
| 58 |
logOnce.Do(func() { |
| 59 |
t.Logf("using the RabbitMQ server at %s", rabbitURL) |
| 60 |
}) |
| 61 |
return &connection{conn} |
| 62 |
} |
| 63 |
|
| 64 |
func TestConformance(t *testing.T) { |
| 65 |
harnessMaker := func(_ context.Context, t *testing.T) (drivertest.Harness, error) { |
| 66 |
t.Helper() |
| 67 |
|
| 68 |
return &harness{conn: mustDialRabbit(t)}, nil |
| 69 |
} |
| 70 |
_, isFake := mustDialRabbit(t).(*fakeConnection) |
| 71 |
asTests := []drivertest.AsTest{rabbitAsTest{isFake}} |
| 72 |
drivertest.RunConformanceTests(t, harnessMaker, asTests) |
| 73 |
|
| 74 |
// Run the conformance tests with the fake if we haven't. |
| 75 |
if isFake { |
| 76 |
return |
| 77 |
} |
| 78 |
t.Logf("now running tests with the fake") |
| 79 |
harnessMaker = func(_ context.Context, t *testing.T) (drivertest.Harness, error) { |
| 80 |
t.Helper() |
| 81 |
|
| 82 |
return &harness{conn: newFakeConnection()}, nil |
| 83 |
} |
| 84 |
asTests = []drivertest.AsTest{rabbitAsTest{true}} |
| 85 |
drivertest.RunConformanceTests(t, harnessMaker, asTests) |
| 86 |
} |
| 87 |
|
| 88 |
func BenchmarkRabbit(b *testing.B) { |
| 89 |
ctx := context.Background() |
| 90 |
h := &harness{conn: mustDialRabbit(b)} |
| 91 |
dt, cleanup, err := h.CreateTopic(ctx, b.Name()) |
| 92 |
if err != nil { |
| 93 |
b.Fatal(err) |
| 94 |
} |
| 95 |
defer cleanup() |
| 96 |
ds, cleanup, err := h.CreateSubscription(ctx, dt, b.Name()) |
| 97 |
if err != nil { |
| 98 |
b.Fatal(err) |
| 99 |
} |
| 100 |
defer cleanup() |
| 101 |
|
| 102 |
topic := pubsub.NewTopic(dt, nil) |
| 103 |
defer topic.Shutdown(ctx) |
| 104 |
sub := pubsub.NewSubscription(ds, nil, nil) |
| 105 |
defer sub.Shutdown(ctx) |
| 106 |
|
| 107 |
drivertest.RunBenchmarks(b, topic, sub) |
| 108 |
} |
| 109 |
|
| 110 |
type harness struct { |
| 111 |
conn amqpConnection |
| 112 |
numTopics uint32 |
| 113 |
numSubs uint32 |
| 114 |
} |
| 115 |
|
| 116 |
func (h *harness) CreateTopic(_ context.Context, testName string) (dt driver.Topic, cleanup func(), err error) { |
| 117 |
exchange := fmt.Sprintf("%s-topic-%d", testName, atomic.AddUint32(&h.numTopics, 1)) |
| 118 |
if err := declareExchange(h.conn, exchange); err != nil { |
| 119 |
return nil, nil, err |
| 120 |
} |
| 121 |
cleanup = func() { |
| 122 |
ch, err := h.conn.Channel() |
| 123 |
if err != nil { |
| 124 |
panic(err) |
| 125 |
} |
| 126 |
ch.ExchangeDelete(exchange) |
| 127 |
} |
| 128 |
return newTopic(h.conn, exchange, nil), cleanup, nil |
| 129 |
} |
| 130 |
|
| 131 |
func (h *harness) MakeNonexistentTopic(context.Context) (driver.Topic, error) { |
| 132 |
return newTopic(h.conn, "nonexistent-topic", nil), nil |
| 133 |
} |
| 134 |
|
| 135 |
func (h *harness) CreateSubscription(_ context.Context, dt driver.Topic, testName string) (ds driver.Subscription, cleanup func(), err error) { |
| 136 |
queue := fmt.Sprintf("%s-subscription-%d", testName, atomic.AddUint32(&h.numSubs, 1)) |
| 137 |
if err := bindQueue(h.conn, queue, dt.(*topic).exchange); err != nil { |
| 138 |
return nil, nil, err |
| 139 |
} |
| 140 |
cleanup = func() { |
| 141 |
ch, err := h.conn.Channel() |
| 142 |
if err != nil { |
| 143 |
panic(err) |
| 144 |
} |
| 145 |
ch.QueueDelete(queue) |
| 146 |
} |
| 147 |
ds = newSubscription(h.conn, queue, nil) |
| 148 |
return ds, cleanup, nil |
| 149 |
} |
| 150 |
|
| 151 |
func (h *harness) MakeNonexistentSubscription(_ context.Context) (driver.Subscription, func(), error) { |
| 152 |
return newSubscription(h.conn, "nonexistent-subscription", nil), func() {}, nil |
| 153 |
} |
| 154 |
|
| 155 |
func (h *harness) Close() { |
| 156 |
h.conn.Close() |
| 157 |
} |
| 158 |
|
| 159 |
func (h *harness) MaxBatchSizes() (int, int) { return 0, 0 } |
| 160 |
|
| 161 |
func (*harness) SupportsMultipleSubscriptions() bool { return true } |
| 162 |
|
| 163 |
func TestUnroutable(t *testing.T) { |
| 164 |
// Expect that we get an error on publish if the exchange has no queue bound to it. |
| 165 |
// The error should be a MultiError containing one error per message. |
| 166 |
ctx := context.Background() |
| 167 |
conn := mustDialRabbit(t) |
| 168 |
defer conn.Close() |
| 169 |
|
| 170 |
if err := declareExchange(conn, "u"); err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
topic := newTopic(conn, "u", nil) |
| 174 |
msgs := []*driver.Message{ |
| 175 |
{Body: []byte("")}, |
| 176 |
{Body: []byte("")}, |
| 177 |
} |
| 178 |
err := topic.SendBatch(ctx, msgs) |
| 179 |
var merr MultiError |
| 180 |
if !topic.ErrorAs(err, &merr) { |
| 181 |
t.Fatalf("got error of type %T, want MultiError", err) |
| 182 |
} |
| 183 |
if got, want := len(merr), len(msgs); got != want { |
| 184 |
t.Fatalf("got %d errors, want %d", got, want) |
| 185 |
} |
| 186 |
// Test MultiError.Error. |
| 187 |
if got, want := strings.Count(merr.Error(), ";")+1, len(merr); got != want { |
| 188 |
t.Errorf("got %d semicolon-separated messages, want %d", got, want) |
| 189 |
} |
| 190 |
// Test each individual error. |
| 191 |
for i, err := range merr { |
| 192 |
if !strings.Contains(err.Error(), "NO_ROUTE") { |
| 193 |
t.Errorf("%d: got %v, want an error with 'NO_ROUTE'", i, err) |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
func TestErrorCode(t *testing.T) { |
| 199 |
for _, test := range []struct { |
| 200 |
in error |
| 201 |
want gcerrors.ErrorCode |
| 202 |
}{ |
| 203 |
{nil, gcerrors.Unknown}, |
| 204 |
{&os.PathError{}, gcerrors.Unknown}, |
| 205 |
{&amqp.Error{Code: amqp.SyntaxError}, gcerrors.Internal}, |
| 206 |
{&amqp.Error{Code: amqp.NotImplemented}, gcerrors.Unimplemented}, |
| 207 |
{&amqp.Error{Code: amqp.ContentTooLarge}, gcerrors.Unknown}, |
| 208 |
} { |
| 209 |
if got := errorCode(test.in); got != test.want { |
| 210 |
t.Errorf("%v: got %s, want %s", test.in, got, test.want) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestOpens(t *testing.T) { |
| 216 |
ctx := context.Background() |
| 217 |
if got := OpenTopic(nil, "t", nil); got == nil { |
| 218 |
t.Error("got nil, want non-nil") |
| 219 |
} else { |
| 220 |
got.Shutdown(ctx) |
| 221 |
} |
| 222 |
if got := OpenSubscription(nil, "s", nil); got == nil { |
| 223 |
t.Error("got nil, want non-nil") |
| 224 |
} else { |
| 225 |
got.Shutdown(ctx) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func TestIsRetryable(t *testing.T) { |
| 230 |
for _, test := range []struct { |
| 231 |
err error |
| 232 |
want bool |
| 233 |
}{ |
| 234 |
{errors.New("xyz"), false}, |
| 235 |
{io.ErrUnexpectedEOF, false}, |
| 236 |
{&amqp.Error{Code: amqp.AccessRefused}, false}, |
| 237 |
{&amqp.Error{Code: amqp.ContentTooLarge}, true}, |
| 238 |
{&amqp.Error{Code: amqp.ConnectionForced}, true}, |
| 239 |
} { |
| 240 |
got := isRetryable(test.err) |
| 241 |
if got != test.want { |
| 242 |
t.Errorf("%+v: got %t, want %t", test.err, got, test.want) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestRunWithContext(t *testing.T) { |
| 248 |
// runWithContext will run its argument to completion if the context isn't done. |
| 249 |
e := errors.New("") |
| 250 |
// f sleeps for a bit just to give the scheduler a chance to run. |
| 251 |
f := func() error { time.Sleep(100 * time.Millisecond); return e } |
| 252 |
got := runWithContext(context.Background(), f) |
| 253 |
if want := e; got != want { |
| 254 |
t.Errorf("got %v, want %v", got, want) |
| 255 |
} |
| 256 |
|
| 257 |
// runWithContext will return ctx.Err if context is done. |
| 258 |
ctx, cancel := context.WithCancel(context.Background()) |
| 259 |
cancel() |
| 260 |
got = runWithContext(ctx, f) |
| 261 |
if want := context.Canceled; got != want { |
| 262 |
t.Errorf("got %v, want %v", got, want) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func declareExchange(conn amqpConnection, name string) error { |
| 267 |
ch, err := conn.Channel() |
| 268 |
if err != nil { |
| 269 |
panic(err) |
| 270 |
} |
| 271 |
defer ch.Close() |
| 272 |
return ch.ExchangeDeclare(name) |
| 273 |
} |
| 274 |
|
| 275 |
func bindQueue(conn amqpConnection, queueName, exchangeName string) error { |
| 276 |
ch, err := conn.Channel() |
| 277 |
if err != nil { |
| 278 |
return err |
| 279 |
} |
| 280 |
defer ch.Close() |
| 281 |
return ch.QueueDeclareAndBind(queueName, exchangeName) |
| 282 |
} |
| 283 |
|
| 284 |
type rabbitAsTest struct { |
| 285 |
usingFake bool |
| 286 |
} |
| 287 |
|
| 288 |
func (rabbitAsTest) Name() string { |
| 289 |
return "rabbit test" |
| 290 |
} |
| 291 |
|
| 292 |
func (r rabbitAsTest) TopicCheck(topic *pubsub.Topic) error { |
| 293 |
var conn2 amqp.Connection |
| 294 |
if topic.As(&conn2) { |
| 295 |
return fmt.Errorf("cast succeeded for %T, want failure", &conn2) |
| 296 |
} |
| 297 |
if !r.usingFake { |
| 298 |
var conn3 *amqp.Connection |
| 299 |
if !topic.As(&conn3) { |
| 300 |
return fmt.Errorf("cast failed for %T", &conn3) |
| 301 |
} |
| 302 |
} |
| 303 |
return nil |
| 304 |
} |
| 305 |
|
| 306 |
func (r rabbitAsTest) SubscriptionCheck(sub *pubsub.Subscription) error { |
| 307 |
var conn2 amqp.Connection |
| 308 |
if sub.As(&conn2) { |
| 309 |
return fmt.Errorf("cast succeeded for %T, want failure", &conn2) |
| 310 |
} |
| 311 |
if !r.usingFake { |
| 312 |
var conn3 *amqp.Connection |
| 313 |
if !sub.As(&conn3) { |
| 314 |
return fmt.Errorf("cast failed for %T", &conn3) |
| 315 |
} |
| 316 |
} |
| 317 |
return nil |
| 318 |
} |
| 319 |
|
| 320 |
func (rabbitAsTest) TopicErrorCheck(t *pubsub.Topic, err error) error { |
| 321 |
var aerr *amqp.Error |
| 322 |
if !t.ErrorAs(err, &aerr) { |
| 323 |
return fmt.Errorf("failed to convert %v (%T) to an amqp.Error", err, err) |
| 324 |
} |
| 325 |
if aerr.Code != amqp.NotFound { |
| 326 |
return fmt.Errorf("got code %v, want NotFound", aerr.Code) |
| 327 |
} |
| 328 |
|
| 329 |
err = MultiError{err} |
| 330 |
var merr MultiError |
| 331 |
if !t.ErrorAs(err, &merr) { |
| 332 |
return fmt.Errorf("failed to convert %v (%T) to a MultiError", err, err) |
| 333 |
} |
| 334 |
var perr *os.PathError |
| 335 |
if t.ErrorAs(err, &perr) { |
| 336 |
return errors.New("got true for PathError, want false") |
| 337 |
} |
| 338 |
return nil |
| 339 |
} |
| 340 |
|
| 341 |
func (rabbitAsTest) SubscriptionErrorCheck(s *pubsub.Subscription, err error) error { |
| 342 |
var aerr *amqp.Error |
| 343 |
if !s.ErrorAs(err, &aerr) { |
| 344 |
return fmt.Errorf("failed to convert %v (%T) to an amqp.Error", err, err) |
| 345 |
} |
| 346 |
if aerr.Code != amqp.NotFound { |
| 347 |
return fmt.Errorf("got code %v, want NotFound", aerr.Code) |
| 348 |
} |
| 349 |
|
| 350 |
err = MultiError{err} |
| 351 |
var merr MultiError |
| 352 |
if !s.ErrorAs(err, &merr) { |
| 353 |
return fmt.Errorf("failed to convert %v (%T) to a MultiError", err, err) |
| 354 |
} |
| 355 |
var perr *os.PathError |
| 356 |
if s.ErrorAs(err, &perr) { |
| 357 |
return errors.New("got true for PathError, want false") |
| 358 |
} |
| 359 |
return nil |
| 360 |
} |
| 361 |
|
| 362 |
func (r rabbitAsTest) MessageCheck(m *pubsub.Message) error { |
| 363 |
var pd *amqp.Delivery |
| 364 |
if m.As(&pd) { |
| 365 |
return fmt.Errorf("cast succeeded for %T, want failure", &pd) |
| 366 |
} |
| 367 |
if !r.usingFake { |
| 368 |
var d amqp.Delivery |
| 369 |
if !m.As(&d) { |
| 370 |
return fmt.Errorf("cast failed for %T", &d) |
| 371 |
} |
| 372 |
} |
| 373 |
return nil |
| 374 |
} |
| 375 |
|
| 376 |
func (rabbitAsTest) BeforeSend(as func(any) bool) error { |
| 377 |
var pub *amqp.Publishing |
| 378 |
if !as(&pub) { |
| 379 |
return fmt.Errorf("cast failed for %T", &pub) |
| 380 |
} |
| 381 |
return nil |
| 382 |
} |
| 383 |
|
| 384 |
func (rabbitAsTest) AfterSend(as func(any) bool) error { |
| 385 |
return nil |
| 386 |
} |
| 387 |
|
| 388 |
func TestOpenTopicFromURL(t *testing.T) { |
| 389 |
t.Setenv("RABBIT_SERVER_URL", rabbitURL) |
| 390 |
|
| 391 |
tests := []struct { |
| 392 |
label string |
| 393 |
URLTemplate string |
| 394 |
WantErr bool |
| 395 |
}{ |
| 396 |
{"valid url", "rabbit://%s", false}, |
| 397 |
{"valid url with key name parameter", "rabbit://%s?key_name=foo", false}, |
| 398 |
{"invalid url with parameters", "rabbit://%s?param=value", true}, |
| 399 |
{"invalid url with key name parameter", "rabbit://%s?key_name=", true}, |
| 400 |
} |
| 401 |
|
| 402 |
for _, test := range tests { |
| 403 |
t.Run(test.label, func(t *testing.T) { |
| 404 |
conn := mustDialRabbit(t) |
| 405 |
_, isFake := conn.(*fakeConnection) |
| 406 |
if isFake { |
| 407 |
t.Skip("test requires real rabbitmq") |
| 408 |
} |
| 409 |
|
| 410 |
h := &harness{conn: conn} |
| 411 |
|
| 412 |
ctx := context.Background() |
| 413 |
|
| 414 |
dt, cleanupTopic, err := h.CreateTopic(ctx, t.Name()) |
| 415 |
if err != nil { |
| 416 |
t.Fatalf("unable to create topic: %v", err) |
| 417 |
} |
| 418 |
|
| 419 |
t.Cleanup(cleanupTopic) |
| 420 |
|
| 421 |
exchange := dt.(*topic).exchange |
| 422 |
url := fmt.Sprintf(test.URLTemplate, exchange) |
| 423 |
|
| 424 |
topic, err := pubsub.OpenTopic(ctx, url) |
| 425 |
if (err != nil) != test.WantErr { |
| 426 |
t.Errorf("%s: got error %v, want error %v", test.URLTemplate, err, test.WantErr) |
| 427 |
} |
| 428 |
if topic != nil { |
| 429 |
topic.Shutdown(ctx) |
| 430 |
} |
| 431 |
}) |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
func TestOpenSubscriptionFromURL(t *testing.T) { |
| 436 |
t.Setenv("RABBIT_SERVER_URL", rabbitURL) |
| 437 |
|
| 438 |
tests := []struct { |
| 439 |
label string |
| 440 |
URLTemplate string |
| 441 |
WantErr bool |
| 442 |
}{ |
| 443 |
{"url with no QoS prefetch count", "rabbit://%s", false}, |
| 444 |
{"invalid parameters", "rabbit://%s?param=value", true}, |
| 445 |
{"valid url with QoS prefetch count", "rabbit://%s?prefetch_count=1024", false}, |
| 446 |
{"invalid url with QoS prefetch count", "rabbit://%s?prefetch_count=value", true}, |
| 447 |
} |
| 448 |
|
| 449 |
for _, test := range tests { |
| 450 |
t.Run(test.label, func(t *testing.T) { |
| 451 |
conn := mustDialRabbit(t) |
| 452 |
_, isFake := conn.(*fakeConnection) |
| 453 |
if isFake { |
| 454 |
t.Skip("test requires real rabbitmq") |
| 455 |
} |
| 456 |
|
| 457 |
h := &harness{conn: conn} |
| 458 |
|
| 459 |
ctx := context.Background() |
| 460 |
|
| 461 |
dt, cleanupTopic, err := h.CreateTopic(ctx, t.Name()) |
| 462 |
if err != nil { |
| 463 |
t.Fatalf("unable to create topic: %v", err) |
| 464 |
} |
| 465 |
|
| 466 |
t.Cleanup(cleanupTopic) |
| 467 |
|
| 468 |
ds, cleanupSubscription, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 469 |
if err != nil { |
| 470 |
t.Fatalf("unable to create subscription: %v", err) |
| 471 |
} |
| 472 |
|
| 473 |
t.Cleanup(cleanupSubscription) |
| 474 |
|
| 475 |
queue := ds.(*subscription).queue |
| 476 |
url := fmt.Sprintf(test.URLTemplate, queue) |
| 477 |
|
| 478 |
sub, err := pubsub.OpenSubscription(ctx, url) |
| 479 |
if (err != nil) != test.WantErr { |
| 480 |
t.Errorf("%s: got error %v, want error %v", test.URLTemplate, err, test.WantErr) |
| 481 |
} |
| 482 |
|
| 483 |
if sub != nil { |
| 484 |
sub.Shutdown(ctx) |
| 485 |
} |
| 486 |
}) |
| 487 |
} |
| 488 |
} |