Skip to content

Commit

Permalink
FABGW-8: Fix race condition in Finder unit test
Browse files Browse the repository at this point in the history
Keep sending messages on the send channel to ensure there is still a message available to read once the listener is established.

Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday authored and Jason Yellick committed Mar 23, 2021
1 parent 62e327e commit 7ac0b60
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/pkg/gateway/commit/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package commit
import (
"context"
"testing"
"time"

"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/core/ledger"
Expand All @@ -23,6 +24,21 @@ type queryProvider interface { // Mimic QueryProvider to avoid circular import w
}

func TestFinder(t *testing.T) {
sendUntilDone := func(commitSend chan<- *ledger.CommitNotification, msg *ledger.CommitNotification) chan struct{} {
done := make(chan struct{})

go func() {
for ; ; time.Sleep(10 * time.Millisecond) {
select {
case commitSend <- msg:
case <-done:
}
}
}()

return done
}

t.Run("passes channel name to query provider", func(t *testing.T) {
provider := &mock.QueryProvider{}
provider.TransactionStatusReturns(peer.TxValidationCode_MVCC_READ_CONFLICT, nil)
Expand Down Expand Up @@ -70,13 +86,14 @@ func TestFinder(t *testing.T) {
t.Run("returns notified transaction status when no previous commit", func(t *testing.T) {
provider := &mock.QueryProvider{}
provider.TransactionStatusReturns(0, errors.New("NOT_FOUND"))
commitSend := make(chan *ledger.CommitNotification, 1)
commitSend <- &ledger.CommitNotification{
commitSend := make(chan *ledger.CommitNotification)
msg := &ledger.CommitNotification{
BlockNumber: 1,
TxIDValidationCodes: map[string]peer.TxValidationCode{
"TX_ID": peer.TxValidationCode_MVCC_READ_CONFLICT,
},
}
defer close(sendUntilDone(commitSend, msg))
finder := &Finder{
Query: provider,
Notifier: NewNotifier(newNotificationSupplier(commitSend)),
Expand Down

0 comments on commit 7ac0b60

Please sign in to comment.