|
9 | 9 | package amqp091 |
10 | 10 |
|
11 | 11 | import ( |
| 12 | + "context" |
12 | 13 | "crypto/tls" |
13 | 14 | "net" |
| 15 | + "os" |
| 16 | + "os/exec" |
14 | 17 | "regexp" |
| 18 | + "strings" |
15 | 19 | "sync" |
16 | 20 | "testing" |
17 | 21 | "time" |
18 | 22 | ) |
19 | 23 |
|
| 24 | +const rabbitmqctlEnvKey = "RABBITMQ_RABBITMQCTL_PATH" |
| 25 | + |
20 | 26 | func TestRequiredServerLocale(t *testing.T) { |
21 | 27 | conn := integrationConnection(t, "AMQP 0-9-1 required server locale") |
22 | 28 | t.Cleanup(func() { conn.Close() }) |
@@ -332,3 +338,56 @@ func TestNewConnectionProperties_HasDefaultProperties(t *testing.T) { |
332 | 338 | t.Fatalf("Version in NewConnectionProperties is not a valid semver value: %s", version) |
333 | 339 | } |
334 | 340 | } |
| 341 | + |
| 342 | +// Connection and channels should be closeable when a memory alarm is active. |
| 343 | +// https://github.com/rabbitmq/amqp091-go/issues/178 |
| 344 | +func TestConnection_Close_WhenMemoryAlarmIsActive(t *testing.T) { |
| 345 | + err := rabbitmqctl(t, "set_vm_memory_high_watermark", "0.0001") |
| 346 | + if err != nil { |
| 347 | + t.Fatal(err) |
| 348 | + } |
| 349 | + t.Cleanup(func() { |
| 350 | + _ = rabbitmqctl(t, "set_vm_memory_high_watermark", "0.4") |
| 351 | + conn, ch := integrationQueue(t, t.Name()) |
| 352 | + integrationQueueDelete(t, ch, t.Name()) |
| 353 | + _ = ch.Close() |
| 354 | + _ = conn.Close() |
| 355 | + }) |
| 356 | + |
| 357 | + conn, ch := integrationQueue(t, t.Name()) |
| 358 | + |
| 359 | + go func() { |
| 360 | + // simulate a producer |
| 361 | + // required to block the connection |
| 362 | + _ = ch.PublishWithContext(context.Background(), "", t.Name(), false, false, Publishing{ |
| 363 | + Body: []byte("this is a test"), |
| 364 | + }) |
| 365 | + }() |
| 366 | + <-time.After(time.Second * 1) |
| 367 | + |
| 368 | + err = conn.CloseDeadline(time.Now().Add(time.Second * 2)) |
| 369 | + if err == nil { |
| 370 | + t.Fatal("expected error, got nil") |
| 371 | + } |
| 372 | + if !conn.IsClosed() { |
| 373 | + t.Fatal("expected connection to be closed") |
| 374 | + } |
| 375 | +} |
| 376 | + |
| 377 | +func rabbitmqctl(t *testing.T, args ...string) error { |
| 378 | + rabbitmqctlPath, found := os.LookupEnv(rabbitmqctlEnvKey) |
| 379 | + if !found { |
| 380 | + t.Skipf("variable for %s for rabbitmqctl not found, skipping", rabbitmqctlEnvKey) |
| 381 | + } |
| 382 | + |
| 383 | + var cmd *exec.Cmd |
| 384 | + if strings.HasPrefix(rabbitmqctlPath, "DOCKER:") { |
| 385 | + containerName := strings.Split(rabbitmqctlPath, ":")[1] |
| 386 | + cmd = exec.Command("docker", "exec", containerName, "rabbitmqctl") |
| 387 | + cmd.Args = append(cmd.Args, args...) |
| 388 | + } else { |
| 389 | + cmd = exec.Command(rabbitmqctlPath, args...) |
| 390 | + } |
| 391 | + |
| 392 | + return cmd.Run() |
| 393 | +} |
0 commit comments