I wanna create an Actor which keeps executing a task, then it can be stopped by an external call to stop, but got quite blocked.
The actor should look the following
myActor = actor {
loop {
react { message ->
if(message instanceof String && "stop".equals(message))
{
stop()
}
}
}
while(true)
{
// do some work
}
}
After the creation, the actor can be started via in my test file, @BeforeMethod:
myActor.start()
And its handle can be kept by the main thread and stopped via below command in my test file, @AfterMethod:
myActor.send "stop"
However, I cannot get above approach working. First of all, I find I do not need to "myActor.start()", after the @BeforeMethod, it just runs automatically;
Secondly, although I tried to call "myActor.send "stop" in my test file's @AfterMethod, it looks like the loop{} of myActor is never visited and keeps working with the while{}.
I tried also to put the loop{} into while{}, but it is still not working and loop{} is still not called
Did I miss anything?
Thanks!
This code snippet should show you how to structure your code:
A few notes: