@ -19,7 +19,7 @@ func simpleHandler(event Event) (Events, error) {
case eventA :
return Events { eventB { } } , nil
case eventB :
return Events { routineFinished { } } , done
return Events { } , done
}
return Events { } , nil
}
@ -29,53 +29,54 @@ func TestRoutine(t *testing.T) {
assert . False ( t , routine . isRunning ( ) ,
"expected an initialized routine to not be running" )
go routine . run ( )
go routine . start ( )
go routine . feedback ( )
for {
if routine . isRunning ( ) {
break
}
time . Sleep ( 10 * time . Millisecond )
}
<- routine . ready ( )
routine . send ( eventA { } )
assert . True ( t , routine . trySend ( eventA { } ) ,
"expected sending to a ready routine to succeed" )
routine . stop ( )
assert . Equal ( t , done , <- routine . final ( ) ,
"expected the final event to be done" )
}
func TesRoutineSend ( t * testing . T ) {
routine := newRoutine ( "simpleRoutine" , simpleHandler )
assert . False ( t , routine . s end( eventA { } ) ,
assert . False ( t , routine . tryS end( eventA { } ) ,
"expected sending to an unstarted routine to fail" )
go routine . run ( )
go routine . start ( )
go routine . feedback ( )
for {
if routine . isRunning ( ) {
break
}
time . Sleep ( 10 * time . Millisecond )
}
<- routine . ready ( )
assert . True ( t , routine . s end( eventA { } ) ,
assert . True ( t , routine . trySend ( eventA { } ) ,
"expected sending to a running routine to succeed" )
routine . stop ( )
assert . False ( t , routine . s end( eventA { } ) ,
assert . False ( t , routine . tryS end( eventA { } ) ,
"expected sending to a stopped routine to fail" )
}
type finalCount struct {
count int
}
func ( f finalCount ) Error ( ) string {
return "end"
}
func genStatefulHandler ( maxCount int ) handleFunc {
counter := 0
return func ( event Event ) ( Events , error ) {
// golint fixme
switch event . ( type ) {
case eventA :
counter += 1
if counter >= maxCount {
return Events { } , done
return Events { } , finalCount { counter }
}
return Events { eventA { } } , nil
@ -85,23 +86,27 @@ func genStatefulHandler(maxCount int) handleFunc {
}
func TestStatefulRoutine ( t * testing . T ) {
handler := genStatefulHandler ( 10 )
count := 10
handler := genStatefulHandler ( count )
routine := newRoutine ( "statefulRoutine" , handler )
routine . setLogger ( log . TestingLogger ( ) )
go routine . run ( )
go routine . start ( )
go routine . feedback ( )
for {
if routine . isRunning ( ) {
break
}
time . Sleep ( 10 * time . Millisecond )
}
<- routine . ready ( )
routine . send ( eventA { } )
assert . True ( t , routine . trySend ( eventA { } ) ,
"expected sending to a started routine to succeed" )
routine . stop ( )
final := <- routine . final ( )
fnl , ok := final . ( finalCount )
if ok {
assert . Equal ( t , count , fnl . count ,
"expected the routine to count to 10" )
} else {
t . Fail ( )
}
}
func handleWithErrors ( event Event ) ( Events , error ) {
@ -116,22 +121,17 @@ func handleWithErrors(event Event) (Events, error) {
func TestErrorSaturation ( t * testing . T ) {
routine := newRoutine ( "errorRoutine" , handleWithErrors )
go routine . run ( )
go routine . start ( )
<- routine . ready ( )
go func ( ) {
for {
routine . s end( eventA { } )
routine . tryS end( eventA { } )
time . Sleep ( 10 * time . Millisecond )
}
} ( )
for {
if routine . isRunning ( ) {
break
}
time . Sleep ( 10 * time . Millisecond )
}
assert . True ( t , routine . send ( errEvent { } ) ,
assert . True ( t , routine . trySend ( errEvent { } ) ,
"expected send to succeed even when saturated" )
routine . wait ( )
assert . Equal ( t , done , <- routine . final ( ) )
}