Fix dataraces in e2e/transactions tests

This commit is contained in:
Igor Mandrigin 2018-04-18 09:54:41 +02:00 committed by Igor Mandrigin
parent f3332cd7bb
commit 19556554a6

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"reflect" "reflect"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -447,7 +448,7 @@ func (s *TransactionsTestSuite) TestDoubleCompleteQueuedTransactions() {
completeQueuedTransaction := make(chan struct{}) completeQueuedTransaction := make(chan struct{})
// replace transaction notification handler // replace transaction notification handler
txFailedEventCalled := false var isTxFailedEventCalled int32 // using int32 as bool to avoid data race: 0 is `false`, 1 is `true`
signHash := gethcommon.Hash{} signHash := gethcommon.Hash{}
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope var envelope signal.Envelope
@ -488,7 +489,7 @@ func (s *TransactionsTestSuite) TestDoubleCompleteQueuedTransactions() {
receivedErrCode := event["error_code"].(string) receivedErrCode := event["error_code"].(string)
s.Equal("2", receivedErrCode) s.Equal("2", receivedErrCode)
txFailedEventCalled = true atomic.AddInt32(&isTxFailedEventCalled, 1)
} }
}) })
@ -509,7 +510,7 @@ func (s *TransactionsTestSuite) TestDoubleCompleteQueuedTransactions() {
s.Equal(sendTxHash, signHash, "transaction hash returned from SendTransaction is invalid") s.Equal(sendTxHash, signHash, "transaction hash returned from SendTransaction is invalid")
s.False(reflect.DeepEqual(sendTxHash, gethcommon.Hash{}), "transaction was never queued or completed") s.False(reflect.DeepEqual(sendTxHash, gethcommon.Hash{}), "transaction was never queued or completed")
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point") s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
s.True(txFailedEventCalled, "expected tx failure signal is not received") s.True(atomic.LoadInt32(&isTxFailedEventCalled) > 0, "expected tx failure signal is not received")
} }
func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() { func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
@ -524,7 +525,7 @@ func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
completeQueuedTransaction := make(chan struct{}) completeQueuedTransaction := make(chan struct{})
// replace transaction notification handler // replace transaction notification handler
txFailedEventCalled := false var isTxFailedEventCalled int32 // using int32 as bool to avoid data race: 0 = `false`, 1 = `true`
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
@ -563,7 +564,7 @@ func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
receivedErrCode := event["error_code"].(string) receivedErrCode := event["error_code"].(string)
s.Equal("4", receivedErrCode) s.Equal("4", receivedErrCode)
txFailedEventCalled = true atomic.AddInt32(&isTxFailedEventCalled, 1)
} }
}) })
@ -583,7 +584,7 @@ func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
s.True(reflect.DeepEqual(txHashCheck, gethcommon.Hash{}), "transaction returned hash, while it shouldn't") s.True(reflect.DeepEqual(txHashCheck, gethcommon.Hash{}), "transaction returned hash, while it shouldn't")
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point") s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
s.True(txFailedEventCalled, "expected tx failure signal is not received") s.True(atomic.LoadInt32(&isTxFailedEventCalled) > 0, "expected tx failure signal is not received")
} }
func (s *TransactionsTestSuite) TestCompleteMultipleQueuedTransactions() { func (s *TransactionsTestSuite) TestCompleteMultipleQueuedTransactions() {
@ -611,7 +612,7 @@ func (s *TransactionsTestSuite) TestDiscardMultipleQueuedTransactions() {
allTestTxDiscarded := make(chan struct{}) allTestTxDiscarded := make(chan struct{})
// replace transaction notification handler // replace transaction notification handler
txFailedEventCallCount := 0 var txFailedEventCallCount int32
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
@ -637,8 +638,8 @@ func (s *TransactionsTestSuite) TestDiscardMultipleQueuedTransactions() {
receivedErrCode := event["error_code"].(string) receivedErrCode := event["error_code"].(string)
s.Equal("4", receivedErrCode) s.Equal("4", receivedErrCode)
txFailedEventCallCount++ newCount := atomic.AddInt32(&txFailedEventCallCount, 1)
if txFailedEventCallCount == testTxCount { if newCount == int32(testTxCount) {
close(allTestTxDiscarded) close(allTestTxDiscarded)
} }
} }