Fix data races in pending_requests_test.go

This commit is contained in:
Igor Mandrigin 2018-04-17 16:13:32 +02:00 committed by Igor Mandrigin
parent f694a6d52b
commit f3332cd7bb
1 changed files with 11 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package sign
import ( import (
"context" "context"
"errors" "errors"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -138,16 +139,16 @@ func (s PendingRequestsSuite) TestConcurrentComplete() {
s.True(s.pendingRequests.Has(req.ID), "sign request should exist") s.True(s.pendingRequests.Has(req.ID), "sign request should exist")
approved := 0 var approved int32
tried := 0 var tried int32
for i := 10; i > 0; i-- { for i := 10; i > 0; i-- {
go func() { go func() {
result := s.pendingRequests.Approve(req.ID, correctPassword, testVerifyFunc) result := s.pendingRequests.Approve(req.ID, correctPassword, testVerifyFunc)
if result.Error == nil { if result.Error == nil {
approved++ atomic.AddInt32(&approved, 1)
} }
tried++ atomic.AddInt32(&tried, 1)
}() }()
} }
@ -155,8 +156,8 @@ func (s PendingRequestsSuite) TestConcurrentComplete() {
s.False(s.pendingRequests.Has(req.ID), "sign request should exist") s.False(s.pendingRequests.Has(req.ID), "sign request should exist")
s.Equal(approved, 1, "request should be approved only once") s.EqualValues(atomic.LoadInt32(&approved), 1, "request should be approved only once")
s.Equal(tried, 10, "request should be tried to approve 10 times") s.EqualValues(atomic.LoadInt32(&tried), 10, "request should be tried to approve 10 times")
} }
func (s PendingRequestsSuite) TestWaitSuccess() { func (s PendingRequestsSuite) TestWaitSuccess() {
@ -214,12 +215,10 @@ func (s PendingRequestsSuite) TestWaitTimeout() {
s.True(s.pendingRequests.Has(req.ID), "sign request should exist") s.True(s.pendingRequests.Has(req.ID), "sign request should exist")
go func() {
time.Sleep(10 * time.Microsecond)
result := s.pendingRequests.Approve(req.ID, correctPassword, testVerifyFunc)
s.NotNil(result.Error)
}()
result := s.pendingRequests.Wait(req.ID, 0*time.Second) result := s.pendingRequests.Wait(req.ID, 0*time.Second)
s.Equal(ErrSignReqTimedOut, result.Error) s.Equal(ErrSignReqTimedOut, result.Error)
// Try approving the timeouted request, it will fail
result = s.pendingRequests.Approve(req.ID, correctPassword, testVerifyFunc)
s.NotNil(result.Error)
} }