-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pykka test support #25
Comments
I don't see much difference between actors and "regular" classes here. You
|
The flaw there is of course in 2., where an assumption has been made about the actor's behaviour wrt. to returning a message by blocking. What if I purposefully want to test a simple actor system like the one below:
Let's assume that The test then, is simply to expect that |
While this seems like a feature to ask of a testing library, it's at least Pykka's responsibility to clarify its position wrt. async/synchronous testing. Akka, for example shows you how you could test actor behaviour synchronously on one thread so that tests are well-behaved. |
The most straightforward way I have found of verifying message-passing behaviour is to create a dummy test Actor (I call this Set up your actor system, interact with the class NopActor(pykka.ThreadingActor):
"""
Does nothing. For testing purposes only.
"""
def __init__(self):
super(NopActor, self).__init__()
def test_should_be_able_to_assert_actor_message_passing_interactions():
nop_actor = NopActor.start()
sent_message = {'key': 'value'}
nop_actor.tell(sent_message)
inbox = copy.copy(nop_actor.actor_inbox)
received = inbox.get(block=False)
received.should.be.equal(sent_message) The |
If you like this style of testing (well, it works...!) please consider documenting this on the |
It seems to me that you're trying to test the wrong thing. Pykka (actors, message passing etc.) is already tested. If I wanted to test that your ProducerActor sends a message then I'd do something like this: from mock import Mock
from pykka import ThreadingActor
class ProducerActor(ThreadingActor):
def __init__(self, consumer):
self.consumer = consumer
# the code that interacts with consumer goes somewhere below
def test_that_producer_sends_something_after_its_started():
consumer = Mock()
producer = ProducerActor.start(consumer)
producer.stop()
consumer.tell.assert_called_once_with({'produced': 'apple'}) |
I'm now wondering how it's possible to run a 'wiring test'. A system of actors all need to be alive and running when the application is running in order for the application to work fully! |
I haven't done such a test myself, but I'd be interested in hearing about it you find a convenient way to test this. |
I've created a testing section in the docs, initially with the tips from this issue and some pytest examples: https://www.pykka.org/en/develop/testing/ |
One of the main advantages of using the Actor model is its ease of testing.
Every actor is by default isolated and so verifying the individual behaviours of Actors should be easy because we can provide each actor with fake / stub collaborators and verify that the right messages (of the appropriate formats) are sent and received.
However, it's not simple to capture this in the form of an assertion in a unit test.
Perhaps a couple of examples of how Pykka actors can be tested with popular test/mocking frameworks would help here.
The text was updated successfully, but these errors were encountered: