Skip to content

Commit

Permalink
Tests: fix unit test not waiting for go routine to finish (#606)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cathcart <[email protected]>
  • Loading branch information
robsdedude authored Oct 4, 2024
1 parent 2341390 commit 5ebc91d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions neo4j/internal/bolt/message_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/neo4j/neo4j-go-driver/v5/neo4j/internal/testutil"
"net"
"reflect"
"sync"
"testing"
)

Expand Down Expand Up @@ -202,19 +203,22 @@ func TestMessageQueue(outer *testing.T) {
})

inner.Run("receives all messages, executes all handlers", func(t *testing.T) {
done := make(chan any)
waitGroup := sync.WaitGroup{}
called := make(chan bool)
queue.enqueueCallback(responseHandler{onSuccess: func(s *success) { called <- true }})
queue.enqueueCallback(responseHandler{onIgnored: func(*ignored) { called <- true }})
queue.enqueueCallback(responseHandler{onRecord: func(*db.Record) { called <- true }})

waitGroup.Add(1)
go func() {
defer waitGroup.Done()
AssertTrue(t, <-called)
AssertTrue(t, <-called)
AssertTrue(t, <-called)
done <- struct{}{}
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
AssertNoError(t, queue.receiveAll(ctx))
}()

Expand All @@ -224,7 +228,7 @@ func TestMessageQueue(outer *testing.T) {
writer.send(ctx, server)
writer.appendX(msgRecord, []any{})
writer.send(ctx, server)
<-done
waitGroup.Wait()
})

inner.Run("returns error when nil callback called for", func(inner *testing.T) {
Expand Down

0 comments on commit 5ebc91d

Please sign in to comment.