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