def test_raise_no_trap_exit(self): collectingActor1 = actor_of(CollectingActor) collectingActor1.trap_exit = False collectingActor1.start() collectingActor2 = actor_of(CollectingActor) collectingActor2.trap_exit = False collectingActor2.start() collectingActor3 = actor_of(CollectingActor) collectingActor3.trap_exit = True collectingActor3.start() collectingActor4 = actor_of(CollectingActor) collectingActor4.trap_exit = True collectingActor4.start() raisingActor = actor_of(RaisingActor) raisingActor.link(collectingActor1) collectingActor1.link(collectingActor2) collectingActor2.link(collectingActor3) collectingActor3.link(collectingActor4) raisingActor.start() raisingActor.notify("Msg") # wait for the messages to be sent # sorry, this is quite ugly time.sleep(1) # collectingActor2 should have closed automatically self.assertEqual(collectingActor2.is_alive, False) # collectingActor3 should still be alive self.assertEqual(collectingActor3.is_alive, True) # collectingActor3 should have received an Exit self.assertEqual(collectingActor1._actor.received_exit, None) self.assertEqual(collectingActor2._actor.received_exit, None) self.assertNotEqual(collectingActor3._actor.received_exit, None) self.assertEqual(collectingActor4._actor.received_exit, None) # TODO: who should be the sender of the exit notice? # self.assertEqual(collectingActor3.received_exit.sender, collectingActor2) collectingActor3.stop() collectingActor4.stop() # wait for the messages to be sent collectingActor3.join(3) self.assertEqual(collectingActor3.is_alive, False)
def test_connection(self): remote = RemoteConnection().start_listener("localhost", 0) remote_actor = actor_of(MultiplyingActor) remote.register("main-actor", remote_actor) remote.start_all() # port is dynamic port = remote.listener.socket.port client = RemoteConnection().actor_for("main-actor", "localhost", port) self.assertTrue(remote_actor.is_alive) self.assertTrue(client.is_connected()) self.assertTrue(remote_actor.is_alive) remote_actor.stop() remote_actor.join() # we are still connected self.assertTrue(client.is_connected()) remote.stop() # need to wait a little until the connection shuts down, sorry for i in range(50): still_connected = client.is_connected() if still_connected: time.sleep(0.1) else: return self.assertFalse(still_connected)
def test_simply_reply(self): actor_ref = actor_of(MultiplyingActor) actor_ref.start() res = actor_ref.query("mult", [1, 2, 3, 4]) self.assertEqual(res.get(timeout=3), 24) actor_ref.stop()
def test_remote(self): remote = RemoteConnection().start_listener("localhost", 0) remote.register("main-actor", actor_of(MultiplyingActor)) remote.start_all() # port is dynamic port = remote.listener.socket.port client1 = RemoteConnection().actor_for("main-actor", "localhost", port) res = client1.query("mult", [1, 2, 3, 4]) self.assertEqual(res.get(timeout=3), 24) # check, that I can use another client client2 = RemoteConnection().actor_for("main-actor", "localhost", port) res = client2.query("mult", [4, 4, 4]) self.assertEqual(res.get(timeout=3), 64) # check, that the first still works res = client1.query("mult", [2, 2, 4]) self.assertEqual(res.get(timeout=3), 16) # check a remote identifier which does not work # sorry, no error propagation at the moment, # need to use a timeout client2 = RemoteConnection().actor_for("unknown-actor", "localhost", port) res = client2.query("mult", [1, 4, 4]) self.assertRaises(Queue.Empty, res.get, timeout=1) remote.stop()
def test_unhandled(self): actor = actor_of(Dispatcher) actor.start() res = actor.query("unhandled") self.assertEqual(type(res.get()), str) actor.stop()
def test_docstring_request(self): actor = actor_of(Dispatcher) actor.start() res = actor.query("?get_docstring") self.assertEqual(res.get(), " This method has no content but a docstring. ") actor.stop()
def test_current_message(self): actor = actor_of(Dispatcher) actor.start() req = actor.query("return_message", {"arg1": 5, "arg3": 7}) self.assertEqual(req.get(), {'params': {'arg1': 5, 'arg3': 7}, 'method': 'return_message'}) actor.stop() actor.join()
def test_lifecycle(self): actor = actor_of(Dispatcher) self.assertRaises(ActorNotRunning, actor.notify, "dummy") actor.start() actor.notify("dummy") actor.stop() actor.join() self.assertRaises(ActorNotRunning, actor.notify, "dummy")
def test_running(self): actor = actor_of(Dispatcher) actor.start() self.assertEqual(actor.is_running, True) actor.stop() actor.join(3) self.assertEqual(actor.is_running, False)
def test_complicated_params(self): actor = actor_of(Dispatcher) actor.start() req = actor.query("complicated_params", {"arg1": 5, "arg3": 7}) # arg2 is default 2 self.assertEqual(req.get(), 725) req = actor.query("complicated_params", [1,2,3]) self.assertEqual(req.get(), 321) actor.stop() actor.join()
def test_renamed_dispatch(self): actor = actor_of(Dispatcher) actor.start() res = actor.query("renamed_method") self.assertEqual(res.get(), 12) res = actor.query("fake_name") self.assertTrue(res.get().startswith("Not found")) # TODO: proper error handling actor.stop()
def test_messages(self): actor = actor_of(Dispatcher) actor.start() actor.notify("set_param1", [12]) request = actor.query("get_param1") response = request.get() self.assertEqual(response, 12) actor.stop()
def test_invalid_dispatch(self): actor = actor_of(Dispatcher) actor.start() res = Request() actor.put("No dict", res) self.assertEqual(type(res.get()), str) # cant do better now res = actor.query(1) self.assertEqual(type(res.get()), str) # cant do better now actor.stop()
def test_not_running(self): remote = RemoteConnection().start_listener("localhost", 0) remote.register("main-actor", actor_of(MultiplyingActor)) # port is dynamic port = remote.listener.socket.port client = RemoteConnection().actor_for("main-actor", "localhost", port) req = client.query("mult", [1, 4, 4]) res = req.get(timeout=3) self.assertTrue("error" in res) remote.stop()
def _setup(self): """ Instantiates the ServerActor and initialises a new game. """ self.server = actor_of(ServerActor, "pelita-main") if self.port is not None: print "Starting remote connection on %s:%s" % (self.host, self.port) self.remote = RemoteConnection().start_listener(host=self.host, port=self.port) self.remote.register("pelita-main", self.server) self.remote.start_all() else: print "Starting actor '%s'" % "pelita-main" self.server.start() self.server.notify("initialize_game", [self.layout, self.players, self.rounds])
def test_bad_json(self): remote = RemoteConnection().start_listener("localhost", 0) remote.register("main-actor", actor_of(MultiplyingActor)) # port is dynamic port = remote.listener.socket.port client = RemoteConnection().actor_for("main-actor", "localhost", port) # unserialisable class class SomeClass(object): pass somobj = SomeClass() self.assertRaises(TypeError, client.query, "mult", somobj) remote.stop()
def test_bad_actors(self): remote = RemoteConnection().start_listener("localhost", 0) remote.register("main-actor", actor_of(MultiplyingActor)) remote.start_all() # port is dynamic port = remote.listener.socket.port # check a remote identifier which does not work # should reply with an error message client = RemoteConnection().actor_for("unknown-actor", "localhost", port) req = client.query("mult", [1, 4, 4]) res = req.get(timeout=3) self.assertTrue("error" in res) remote.stop()
def test_remote(self): remote = RemoteConnection().start_listener("localhost", 0) remote.register("main-actor", actor_of(MultiplyingActor)) remote.start_all() # port is dynamic port = remote.listener.socket.port client1 = RemoteConnection().actor_for("main-actor", "localhost", port) res = client1.query("mult", [1, 2, 3, 4]) self.assertEqual(res.get(timeout=3), 24) # check, that I can use another client client2 = RemoteConnection().actor_for("main-actor", "localhost", port) res = client2.query("mult", [4, 4, 4]) self.assertEqual(res.get(timeout=3), 64) # check, that the first still works res = client1.query("mult", [2, 2, 4]) self.assertEqual(res.get(timeout=3), 16) remote.stop()
Verbose demonstration of how to set up a server and run a remote game. For all practical needs, using the simplesetup module should be sufficient. """ from pelita.messaging import actor_of, RemoteConnection from pelita.actors import ServerActor import logging from pelita.ui.tk_viewer import TkViewer from pelita.utils.colorama_wrapper import colorama FORMAT = '[%(asctime)s,%(msecs)03d][%(name)s][%(levelname)s][%(funcName)s]' + colorama.Fore.MAGENTA + ' %(message)s' + colorama.Fore.RESET #logging.basicConfig(format=FORMAT, datefmt="%H:%M:%S", level=logging.WARNING) server = actor_of(ServerActor, "pelita-main") remote = RemoteConnection().start_listener(host="", port=50007) remote.register("pelita-main", server) remote.start_all() layout = ( """ ################## #0#. . 2# . 3 # # ##### ##### # # . # . .#1# ################## """) server.notify("initialize_game", [layout, 4, 200]) viewer = TkViewer()
self.ref.channel.notify("Pong", channel=self.ref) self.pong_count += 1 @expose def Stop(self, message): print "Pong: Stop." self.ref.put(StopProcessing) import logging #logging.basicConfig() remote = True if remote: remote = RemoteConnection().start_listener("localhost", 0) remote.register("ping", actor_of(Ping)) remote.start_all() port = remote.listener.socket.port ping = RemoteConnection().actor_for("ping", "localhost", port) else: ping = actor_of(Ping) ping.start() pong = actor_of(Pong) pong.start() ping.notify("connect", [pong.uuid]) ping.notify("Start")
@expose def calculate_pi_for(self, message, *params): res = calculate_pi_for(*params) self.ref.reply(res) @expose def slow_series(self, message, *params): res = slow_series(*params) self.ref.reply(res) @expose def random_int(self, message): import random self.ref.reply(random.randint(0, 10)) actor = actor_of(ClientActor) actor.start() port = 50007 remote_actor = RemoteConnection().actor_for("main-actor", "localhost", port) res = remote_actor.query("multiply", [1, 2, 3, 4]) print res.get() remote_actor.notify("hello", [str(actor.uuid)]) try: while actor.is_alive: actor.join(1)
def __init__(self, team_name): self.team_name = team_name self.actor_ref = actor_of(_ClientActor) self.actor_ref._actor.thread.daemon = True # TODO remove this line self.actor_ref.start()
reqs = [] for player in self.players: reqs.append( player.query("random_int", []) ) res = 0 for answer in reqs: res += answer.get() if res % 2 != 0: self.ref.reply("Player 1 wins") else: self.ref.reply("Player 2 wins") remote = RemoteConnection().start_listener("localhost", 50007) actor_ref = actor_of(ServerActor) remote.register("main-actor", actor_ref) remote.start_all() #incoming_bundler = IncomingConnectionsActor(incoming_connections, inbox) #incoming_bundler.start() def printcol(msg): """Using a helper function to get coloured output""" print colorama.Fore.BLUE + str(msg) + colorama.Fore.RESET class EndSession(Exception): pass import json try: