@ -6,6 +6,8 @@ import (
"time"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
cmn "github.com/tendermint/tendermint/libs/common"
)
)
@ -14,10 +16,9 @@ import (
func TestAddListenerForEventFireOnce ( t * testing . T ) {
func TestAddListenerForEventFireOnce ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
messages := make ( chan EventData )
messages := make ( chan EventData )
evsw . AddListenerForEvent ( "listener" , "event" ,
evsw . AddListenerForEvent ( "listener" , "event" ,
func ( data EventData ) {
func ( data EventData ) {
@ -35,10 +36,9 @@ func TestAddListenerForEventFireOnce(t *testing.T) {
func TestAddListenerForEventFireMany ( t * testing . T ) {
func TestAddListenerForEventFireMany ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
doneSum := make ( chan uint64 )
doneSum := make ( chan uint64 )
doneSending := make ( chan uint64 )
doneSending := make ( chan uint64 )
numbers := make ( chan uint64 , 4 )
numbers := make ( chan uint64 , 4 )
@ -65,10 +65,9 @@ func TestAddListenerForEventFireMany(t *testing.T) {
func TestAddListenerForDifferentEvents ( t * testing . T ) {
func TestAddListenerForDifferentEvents ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
doneSum := make ( chan uint64 )
doneSum := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
doneSending2 := make ( chan uint64 )
doneSending2 := make ( chan uint64 )
@ -111,10 +110,9 @@ func TestAddListenerForDifferentEvents(t *testing.T) {
func TestAddDifferentListenerForDifferentEvents ( t * testing . T ) {
func TestAddDifferentListenerForDifferentEvents ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
doneSum1 := make ( chan uint64 )
doneSum1 := make ( chan uint64 )
doneSum2 := make ( chan uint64 )
doneSum2 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
@ -174,40 +172,38 @@ func TestAddAndRemoveListenerConcurrency(t *testing.T) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
done1 := make ( chan struct { } )
done1 := make ( chan struct { } )
done2 := make ( chan struct { } )
done2 := make ( chan struct { } )
// Must be executed concurrently to uncover the data race.
// 1. RemoveListener
go func ( ) {
go func ( ) {
for i := 0 ; i < roundCount ; i ++ {
for i := 0 ; i < roundCount ; i ++ {
evsw . RemoveListener ( "listener" )
evsw . RemoveListener ( "listener" )
}
}
done1 <- struct { } { }
close ( done1 )
} ( )
} ( )
// 2. AddListenerForEvent
go func ( ) {
go func ( ) {
for i := 0 ; i < roundCount ; i ++ {
for i := 0 ; i < roundCount ; i ++ {
index := i //it necessary for closure
index := i
evsw . AddListenerForEvent ( "listener" , fmt . Sprintf ( "event%d" , index ) ,
evsw . AddListenerForEvent ( "listener" , fmt . Sprintf ( "event%d" , index ) ,
func ( data EventData ) {
func ( data EventData ) {
t . Errorf ( "should not run callback for %d.\n" , index )
t . Errorf ( "should not run callback for %d.\n" , index )
stopInputEvent = true
stopInputEvent = true
} )
} )
}
}
done2 <- struct { } { }
close ( done2 )
} ( )
} ( )
<- done1
<- done1
<- done2
<- done2
close ( done1 )
close ( done2 )
evsw . RemoveListener ( "listener" ) // make sure remove last
evsw . RemoveListener ( "listener" ) // remove the last listener
for i := 0 ; i < roundCount && ! stopInputEvent ; i ++ {
for i := 0 ; i < roundCount && ! stopInputEvent ; i ++ {
evsw . FireEvent ( fmt . Sprintf ( "event%d" , i ) , uint64 ( 1001 ) )
evsw . FireEvent ( fmt . Sprintf ( "event%d" , i ) , uint64 ( 1001 ) )
@ -220,10 +216,9 @@ func TestAddAndRemoveListenerConcurrency(t *testing.T) {
func TestAddAndRemoveListener ( t * testing . T ) {
func TestAddAndRemoveListener ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
doneSum1 := make ( chan uint64 )
doneSum1 := make ( chan uint64 )
doneSum2 := make ( chan uint64 )
doneSum2 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
@ -266,10 +261,9 @@ func TestAddAndRemoveListener(t *testing.T) {
func TestRemoveListener ( t * testing . T ) {
func TestRemoveListener ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
count := 10
count := 10
sum1 , sum2 := 0 , 0
sum1 , sum2 := 0 , 0
// add some listeners and make sure they work
// add some listeners and make sure they work
@ -320,10 +314,9 @@ func TestRemoveListener(t *testing.T) {
func TestRemoveListenersAsync ( t * testing . T ) {
func TestRemoveListenersAsync ( t * testing . T ) {
evsw := NewEventSwitch ( )
evsw := NewEventSwitch ( )
err := evsw . Start ( )
err := evsw . Start ( )
if err != nil {
t . Errorf ( "Failed to start EventSwitch, error: %v" , err )
}
require . NoError ( t , err )
defer evsw . Stop ( )
defer evsw . Stop ( )
doneSum1 := make ( chan uint64 )
doneSum1 := make ( chan uint64 )
doneSum2 := make ( chan uint64 )
doneSum2 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
doneSending1 := make ( chan uint64 )
@ -406,7 +399,7 @@ func TestRemoveListenersAsync(t *testing.T) {
// until the receiving channel `numbers` is closed; it then sends the sum
// until the receiving channel `numbers` is closed; it then sends the sum
// on `doneSum` and closes that channel. Expected to be run in a go-routine.
// on `doneSum` and closes that channel. Expected to be run in a go-routine.
func sumReceivedNumbers ( numbers , doneSum chan uint64 ) {
func sumReceivedNumbers ( numbers , doneSum chan uint64 ) {
var sum uint64 = 0
var sum uint64
for {
for {
j , more := <- numbers
j , more := <- numbers
sum += j
sum += j
@ -425,7 +418,7 @@ func sumReceivedNumbers(numbers, doneSum chan uint64) {
// the test to assert all events have also been received.
// the test to assert all events have also been received.
func fireEvents ( evsw EventSwitch , event string , doneChan chan uint64 ,
func fireEvents ( evsw EventSwitch , event string , doneChan chan uint64 ,
offset uint64 ) {
offset uint64 ) {
var sentSum uint64 = 0
var sentSum uint64
for i := offset ; i <= offset + uint64 ( 999 ) ; i ++ {
for i := offset ; i <= offset + uint64 ( 999 ) ; i ++ {
sentSum += i
sentSum += i
evsw . FireEvent ( event , i )
evsw . FireEvent ( event , i )