Skip to content

Commit

Permalink
tests: add testcase to ensure reader routine terminates
Browse files Browse the repository at this point in the history
Add a testcase for the bug that the reader go-routine tries to send a
message to the buffered rpc channel but call() terminated because it
read an error from the errors chan or the errors chan was closed.
It cause that reader routine gets stuck forever and does not terminate
when the connection is closed.
More information: rabbitmq#69.

This testcase does not reproduce the issue reliably, but it is triggered
in ~80% of executions.
  • Loading branch information
fho committed Apr 13, 2022
1 parent 6cac2fa commit bfa7b47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"sync"
"testing"
"time"

"go.uber.org/goleak"
)

func TestRequiredServerLocale(t *testing.T) {
Expand Down Expand Up @@ -210,3 +212,32 @@ func TestChannelIsClosed(t *testing.T) {
t.Fatal("channel expected to be marked as closed")
}
}

// TestReaderGoRoutineTerminatesWhenMsgIsProcessedDuringClose tests the issue
// described in https://github.com/rabbitmq/amqp091-go/issues/69.
func TestReaderGoRoutineTerminatesWhenMsgIsProcessedDuringClose(t *testing.T) {
const routines = 10
defer goleak.VerifyNone(t)
c := integrationConnection(t, t.Name())

var wg sync.WaitGroup
startSigCh := make(chan interface{})

for i := 0; i < routines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()

<-startSigCh

err := c.Close()
if err != nil {
t.Logf("close failed in routine %d: %s", id, err.Error())
}
}(i)
}
close(startSigCh)

t.Log("waiting for go-routines to terminate")
wg.Wait()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/rabbitmq/amqp091-go

go 1.16

require go.uber.org/goleak v1.1.12

0 comments on commit bfa7b47

Please sign in to comment.