Skip to content

Commit

Permalink
Some unit tests in connection were showing a race condition (see rabb…
Browse files Browse the repository at this point in the history
…itmq#31):

These ones were the ones testing Open scenarios. The issue is that Open and Close, rwc.Open and rwc.Close can at the same time write on:

c.allocator = newAllocator(1, c.Config.ChannelMax)
connection.go line 444 and
connection.go line 849

while shutdown is protected by the structure mutex m, OpenComplete() is not causing the race.

Not sure if the library should be protected in this case adding Lock also in OpenComplete().
In any case the tests were just testing Open, so the rwc.Close() can be moved in the main function at the end avoiding the race and not affected the test functionality
  • Loading branch information
Daniele Palaia committed Mar 25, 2022
1 parent bf1ab06 commit e09eb3a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestDefaultClientProperties(t *testing.T) {

go func() {
srv.connectionOpen()
rwc.Close()

}()

if c, err := Open(rwc, defaultConfig()); err != nil {
Expand All @@ -236,6 +236,7 @@ func TestDefaultClientProperties(t *testing.T) {
if want, got := defaultLocale, srv.start.Locale; want != got {
t.Errorf("expected locale %s got: %s", want, got)
}
rwc.Close()
}

func TestCustomClientProperties(t *testing.T) {
Expand All @@ -249,7 +250,7 @@ func TestCustomClientProperties(t *testing.T) {

go func() {
srv.connectionOpen()
rwc.Close()

}()

if c, err := Open(rwc, config); err != nil {
Expand All @@ -263,18 +264,21 @@ func TestCustomClientProperties(t *testing.T) {
if want, got := config.Properties["version"], srv.start.ClientProperties["version"]; want != got {
t.Errorf("expected version %s got: %s", want, got)
}

rwc.Close()
}

func TestOpen(t *testing.T) {
rwc, srv := newSession(t)
go func() {
srv.connectionOpen()
rwc.Close()

}()

if c, err := Open(rwc, defaultConfig()); err != nil {
t.Fatalf("could not create connection: %v (%s)", c, err)
}
rwc.Close()
}

func TestChannelOpen(t *testing.T) {
Expand Down Expand Up @@ -326,7 +330,7 @@ func TestOpenAMQPlainAuth(t *testing.T) {

srv.recv(0, &connectionOpen{})
srv.send(0, &connectionOpenOk{})
rwc.Close()

auth <- table
}()

Expand All @@ -340,6 +344,7 @@ func TestOpenAMQPlainAuth(t *testing.T) {
if table["PASSWORD"] != defaultPassword {
t.Fatalf("unexpected password: want: %s, got: %s", defaultPassword, table["PASSWORD"])
}
rwc.Close()
}

func TestOpenFailedCredentials(t *testing.T) {
Expand Down

0 comments on commit e09eb3a

Please sign in to comment.