scala - What does CallingThreadDispatcher do? -
i came across code , wonder callingthreaddispatcher does. can comment out
val dispatcherid = callingthreaddispatcher.id val props = props[greeter].withdispatcher(dispatcherid)
and test still work
class greetertest extends testkit(testsystem) wordspeclike mustmatchers stopsystemafterall { "the greeter" must { "say hello world! when greeting(\"world\") sent it" in { val dispatcherid = callingthreaddispatcher.id val props = props[greeter].withdispatcher(dispatcherid) val greeter = system.actorof(props) eventfilter.info(message = "hello world!", occurrences = 1).intercept { greeter ! greeting("world") } } } } object greetertest { val testsystem = { val config = configfactory.parsestring("""akka.loggers = [akka.testkit.testeventlistener]""") actorsystem("testsystem", config) } }
so callingthreaddispatcher do? , why need it?
the documentation callingthreaddispatcher
pretty good:
* dispatcher runs invocations on current thread only. * dispatcher not create new threads, can used * different threads concurrently same actor. dispatch strategy * run on current thread unless target actor either suspendswitch or * running on current thread (if running on different * thread, thread block until other invocation * finished); if invocation not run, queued in thread-local * queue executed once active invocation further call stack * finishes. leads deterministic execution order if 1 * thread used.
it used in unit tests because convenient actors have deterministic order of execution when you're testing particular unit of code. see, isn't mandatory test you're running , works without it.
Comments
Post a Comment