|
1 | 1 | package testcontainers |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
@@ -529,20 +530,55 @@ func TestReaper_ReuseRunning(t *testing.T) { |
529 | 530 | } |
530 | 531 | } |
531 | 532 |
|
532 | | -// reaperConnect copies the logic from Reaper.connect() but with better error handling. |
533 | | -// Reaper.connect() neither returns the error from the handshake, nor the connection, |
534 | | -// making the testing of the handshake flow impossible. |
| 533 | +func TestReaperConnectReturnsHandshakeError(t *testing.T) { |
| 534 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
| 535 | + defer cancel() |
| 536 | + |
| 537 | + listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 538 | + require.NoError(t, err) |
| 539 | + t.Cleanup(func() { |
| 540 | + require.NoError(t, listener.Close()) |
| 541 | + }) |
| 542 | + |
| 543 | + done := make(chan struct{}) |
| 544 | + go func() { |
| 545 | + defer close(done) |
| 546 | + |
| 547 | + conn, err := listener.Accept() |
| 548 | + if err != nil { |
| 549 | + return |
| 550 | + } |
| 551 | + defer conn.Close() |
| 552 | + |
| 553 | + _, _ = bufio.NewReader(conn).ReadString('\n') |
| 554 | + _, _ = conn.Write([]byte("NOPE")) |
| 555 | + }() |
| 556 | + |
| 557 | + reaper := &Reaper{ |
| 558 | + SessionID: testSessionID, |
| 559 | + Endpoint: listener.Addr().String(), |
| 560 | + } |
| 561 | + |
| 562 | + termSignal, err := reaper.connect(ctx) |
| 563 | + require.Nil(t, termSignal) |
| 564 | + require.ErrorContains(t, err, "handshake reaper") |
| 565 | + require.ErrorContains(t, err, "unexpected reaper response: NOPE") |
| 566 | + |
| 567 | + select { |
| 568 | + case <-done: |
| 569 | + case <-ctx.Done(): |
| 570 | + require.FailNow(t, "test reaper server did not finish") |
| 571 | + } |
| 572 | +} |
| 573 | + |
535 | 574 | func reaperConnect(t *testing.T, reaper *Reaper) { |
536 | 575 | t.Helper() |
537 | 576 |
|
538 | | - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
539 | | - defer cancel() |
540 | | - var d net.Dialer |
541 | | - conn, err := d.DialContext(ctx, "tcp", reaper.Endpoint) |
542 | | - require.NoError(t, err, "dial reaper %s: %v", reaper.Endpoint, err) |
543 | | - defer conn.Close() |
544 | | - err = reaper.handshake(conn) |
| 577 | + termSignal, err := reaper.Connect() |
545 | 578 | require.NoError(t, err, "Reaper handshake should be successful") |
| 579 | + if termSignal != nil { |
| 580 | + termSignal <- true |
| 581 | + } |
546 | 582 | } |
547 | 583 |
|
548 | 584 | func TestSpawnerBackoff(t *testing.T) { |
|
0 commit comments