From a08b198d889396d25182bb9716817311bcc3be47 Mon Sep 17 00:00:00 2001 From: drebs Date: Sun, 9 Jun 2013 14:16:33 -0300 Subject: Add possibility of unregistering in events mechanism. --- src/leap/common/tests/test_events.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/leap/common/tests/test_events.py') diff --git a/src/leap/common/tests/test_events.py b/src/leap/common/tests/test_events.py index 8c0bd36..7286bdc 100644 --- a/src/leap/common/tests/test_events.py +++ b/src/leap/common/tests/test_events.py @@ -198,3 +198,36 @@ class EventsTestCase(unittest.TestCase): response = events.signal(sig) self.assertTrue(response.status == response.OK, 'Received wrong response status when signaling.') + + def test_component_unregister_all(self): + """ + Test that the component can unregister all events for one signal. + """ + sig = CLIENT_UID + complist = server.registered_components + events.register(sig, lambda x: True) + events.register(sig, lambda x: True) + time.sleep(0.1) + events.unregister(sig) + time.sleep(0.1) + port = component.EventsComponentDaemon.get_instance().get_port() + self.assertFalse(bool(complist[sig])) + self.assertTrue(port not in complist[sig]) + + def test_component_unregister_by_uid(self): + """ + Test that the component can unregister an event by uid. + """ + sig = CLIENT_UID + complist = server.registered_components + events.register(sig, lambda x: True, uid='cbkuid') + events.register(sig, lambda x: True, uid='cbkuid2') + time.sleep(0.1) + events.unregister(sig, uid='cbkuid') + time.sleep(0.1) + port = component.EventsComponentDaemon.get_instance().get_port() + self.assertTrue(sig in complist) + self.assertTrue(len(complist[sig]) == 1) + self.assertTrue( + component.registered_callbacks[sig].pop()[0] == 'cbkuid2') + self.assertTrue(port in complist[sig]) -- cgit v1.2.3 From 1f1412f3c31dfba10135ceae4641313ee48318c8 Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 24 Jul 2013 14:01:30 -0300 Subject: Refactor events so components become clients. --- src/leap/common/tests/test_events.py | 66 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'src/leap/common/tests/test_events.py') diff --git a/src/leap/common/tests/test_events.py b/src/leap/common/tests/test_events.py index 7286bdc..687195f 100644 --- a/src/leap/common/tests/test_events.py +++ b/src/leap/common/tests/test_events.py @@ -22,7 +22,7 @@ from protobuf.socketrpc import RpcService from leap.common import events from leap.common.events import ( server, - component, + client, mac_auth, ) from leap.common.events.events_pb2 import ( @@ -51,7 +51,7 @@ class EventsTestCase(unittest.TestCase): @classmethod def setUpClass(cls): server.EventsServerDaemon.ensure(8090) - cls.callbacks = events.component.registered_callbacks + cls.callbacks = events.client.registered_callbacks @classmethod def tearDownClass(cls): @@ -62,8 +62,8 @@ class EventsTestCase(unittest.TestCase): super(EventsTestCase, self).setUp() def tearDown(self): - #events.component.registered_callbacks = {} - server.registered_components = {} + #events.client.registered_callbacks = {} + server.registered_clients = {} super(EventsTestCase, self).tearDown() def test_service_singleton(self): @@ -76,24 +76,24 @@ class EventsTestCase(unittest.TestCase): self.assertEqual(service1, service2, "Can't get singleton class for service.") - def test_component_register(self): + def test_client_register(self): """ - Ensure components can register callbacks. + Ensure clients can register callbacks. """ self.assertTrue(1 not in self.callbacks, 'There should should be no callback for this signal.') events.register(1, lambda x: True) self.assertTrue(1 in self.callbacks, - 'Could not register signal in local component.') + 'Could not register signal in local client.') events.register(2, lambda x: True) self.assertTrue(1 in self.callbacks, - 'Could not register signal in local component.') + 'Could not register signal in local client.') self.assertTrue(2 in self.callbacks, - 'Could not register signal in local component.') + 'Could not register signal in local client.') def test_register_signal_replace(self): """ - Make sure components can replace already registered callbacks. + Make sure clients can replace already registered callbacks. """ sig = 3 cbk = lambda x: True @@ -134,7 +134,7 @@ class EventsTestCase(unittest.TestCase): def test_events_server_service_register(self): """ - Ensure the server can register components to be signaled. + Ensure the server can register clients to be signaled. """ sig = 5 request = RegisterRequest() @@ -143,34 +143,34 @@ class EventsTestCase(unittest.TestCase): request.mac_method = mac_auth.MacMethod.MAC_NONE request.mac = "" service = RpcService(EventsServerService_Stub, port, 'localhost') - complist = server.registered_components + complist = server.registered_clients self.assertEqual({}, complist, 'There should be no registered_ports when ' 'server has just been created.') response = service.register(request, timeout=1000) self.assertTrue(sig in complist, "Signal not registered succesfully.") self.assertTrue(8091 in complist[sig], - 'Failed registering component port.') + 'Failed registering client port.') - def test_component_request_register(self): + def test_client_request_register(self): """ - Ensure components can register themselves with server. + Ensure clients can register themselves with server. """ sig = 6 - complist = server.registered_components + complist = server.registered_clients self.assertTrue(sig not in complist, - 'There should be no registered components for this ' + 'There should be no registered clients for this ' 'signal.') events.register(sig, lambda x: True) time.sleep(0.1) - port = component.EventsComponentDaemon.get_instance().get_port() - self.assertTrue(sig in complist, 'Failed registering component.') + port = client.EventsClientDaemon.get_instance().get_port() + self.assertTrue(sig in complist, 'Failed registering client.') self.assertTrue(port in complist[sig], - 'Failed registering component port.') + 'Failed registering client port.') - def test_component_receives_signal(self): + def test_client_receives_signal(self): """ - Ensure components can receive signals. + Ensure clients can receive signals. """ sig = 7 @@ -190,44 +190,44 @@ class EventsTestCase(unittest.TestCase): time.sleep(0.5) self.assertTrue(received, 'Did not receive signal back.') - def test_component_send_signal(self): + def test_client_send_signal(self): """ - Ensure components can send signals. + Ensure clients can send signals. """ sig = 8 response = events.signal(sig) self.assertTrue(response.status == response.OK, 'Received wrong response status when signaling.') - def test_component_unregister_all(self): + def test_client_unregister_all(self): """ - Test that the component can unregister all events for one signal. + Test that the client can unregister all events for one signal. """ sig = CLIENT_UID - complist = server.registered_components + complist = server.registered_clients events.register(sig, lambda x: True) events.register(sig, lambda x: True) time.sleep(0.1) events.unregister(sig) time.sleep(0.1) - port = component.EventsComponentDaemon.get_instance().get_port() + port = client.EventsClientDaemon.get_instance().get_port() self.assertFalse(bool(complist[sig])) self.assertTrue(port not in complist[sig]) - def test_component_unregister_by_uid(self): + def test_client_unregister_by_uid(self): """ - Test that the component can unregister an event by uid. + Test that the client can unregister an event by uid. """ sig = CLIENT_UID - complist = server.registered_components + complist = server.registered_clients events.register(sig, lambda x: True, uid='cbkuid') events.register(sig, lambda x: True, uid='cbkuid2') time.sleep(0.1) events.unregister(sig, uid='cbkuid') time.sleep(0.1) - port = component.EventsComponentDaemon.get_instance().get_port() + port = client.EventsClientDaemon.get_instance().get_port() self.assertTrue(sig in complist) self.assertTrue(len(complist[sig]) == 1) self.assertTrue( - component.registered_callbacks[sig].pop()[0] == 'cbkuid2') + client.registered_callbacks[sig].pop()[0] == 'cbkuid2') self.assertTrue(port in complist[sig]) -- cgit v1.2.3 From b7c74e4f293d0e611ea038e04022fbe700a8cb42 Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 24 Jul 2013 16:26:52 -0300 Subject: Trying to init events server raises when given port is not free. * Also fix and improve some tests. --- src/leap/common/tests/test_events.py | 146 ++++++++++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 20 deletions(-) (limited to 'src/leap/common/tests/test_events.py') diff --git a/src/leap/common/tests/test_events.py b/src/leap/common/tests/test_events.py index 687195f..90124b4 100644 --- a/src/leap/common/tests/test_events.py +++ b/src/leap/common/tests/test_events.py @@ -18,6 +18,12 @@ import unittest import sets import time +import socket +import threading +import random + + +from mock import Mock from protobuf.socketrpc import RpcService from leap.common import events from leap.common.events import ( @@ -28,9 +34,11 @@ from leap.common.events import ( from leap.common.events.events_pb2 import ( EventsServerService, EventsServerService_Stub, + EventsClientService_Stub, EventResponse, SignalRequest, RegisterRequest, + PingRequest, SOLEDAD_CREATING_KEYS, CLIENT_UID, ) @@ -39,11 +47,6 @@ from leap.common.events.events_pb2 import ( port = 8090 received = False -local_callback_executed = False - - -def callback(request, reponse): - return True class EventsTestCase(unittest.TestCase): @@ -120,17 +123,28 @@ class EventsTestCase(unittest.TestCase): response = service.signal(request, timeout=1000) self.assertEqual(EventResponse.OK, response.status, 'Wrong response status.') - # test asynch - def local_callback(request, response): - global local_callback_executed - local_callback_executed = True + def test_signal_executes_callback(self): + """ + Ensure callback is executed upon receiving signal. + """ + sig = CLIENT_UID + request = SignalRequest() + request.event = sig + request.content = 'my signal contents' + request.mac_method = mac_auth.MacMethod.MAC_NONE + request.mac = "" + service = RpcService(EventsServerService_Stub, port, 'localhost') - events.register(sig, local_callback) - service.signal(request, callback=local_callback) - time.sleep(0.1) - self.assertTrue(local_callback_executed, - 'Local callback did not execute.') + # register a callback + flag = Mock() + events.register(sig, lambda req: flag(req.event)) + # signal + response = service.signal(request) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + time.sleep(1) # wait for signal to arrive + flag.assert_called_once_with(sig) def test_events_server_service_register(self): """ @@ -173,12 +187,9 @@ class EventsTestCase(unittest.TestCase): Ensure clients can receive signals. """ sig = 7 + flag = Mock() - def getsig(param=None): - global received - received = True - - events.register(sig, getsig) + events.register(sig, lambda req: flag(req.event)) request = SignalRequest() request.event = sig request.content = "" @@ -188,7 +199,7 @@ class EventsTestCase(unittest.TestCase): response = service.signal(request, timeout=1000) self.assertTrue(response is not None, 'Did not receive response.') time.sleep(0.5) - self.assertTrue(received, 'Did not receive signal back.') + flag.assert_called_once_with(sig) def test_client_send_signal(self): """ @@ -231,3 +242,98 @@ class EventsTestCase(unittest.TestCase): self.assertTrue( client.registered_callbacks[sig].pop()[0] == 'cbkuid2') self.assertTrue(port in complist[sig]) + + def test_server_replies_ping(self): + """ + Ensure server replies to a ping. + """ + request = PingRequest() + service = RpcService(EventsServerService_Stub, port, 'localhost') + response = service.ping(request, timeout=1000) + self.assertIsNotNone(response) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + + def test_client_replies_ping(self): + """ + Ensure clients reply to a ping. + """ + daemon = client.ensure_client_daemon() + port = daemon.get_port() + request = PingRequest() + service = RpcService(EventsClientService_Stub, port, 'localhost') + response = service.ping(request, timeout=1000) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + + def test_server_ping(self): + """ + Ensure the function from server module pings correctly. + """ + response = server.ping() + self.assertIsNotNone(response) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + + def test_client_ping(self): + """ + Ensure the function from client module pings correctly. + """ + daemon = client.ensure_client_daemon() + response = client.ping(daemon.get_port()) + self.assertIsNotNone(response) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + + def test_module_ping_server(self): + """ + Ensure the function from main module pings server correctly. + """ + response = events.ping_server() + self.assertIsNotNone(response) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + + def test_module_ping_client(self): + """ + Ensure the function from main module pings clients correctly. + """ + daemon = client.ensure_client_daemon() + response = events.ping_client(daemon.get_port()) + self.assertIsNotNone(response) + self.assertEqual(EventResponse.OK, response.status, + 'Wrong response status.') + + def test_ensure_server_raises_if_port_taken(self): + """ + Verify that server raises an exception if port is already taken. + """ + # get a random free port + while True: + port = random.randint(1024, 65535) + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(('localhost', port)) + s.close() + except: + break + + class PortBlocker(threading.Thread): + + def run(self): + conns = 0 + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(('localhost', port)) + s.setblocking(1) + s.listen(1) + while conns < 2: # blocks until rece + conns += 1 + s.accept() + s.close() + + # block the port + taker = PortBlocker() + taker.start() + time.sleep(1) # wait for thread to start. + self.assertRaises( + server.PortAlreadyTaken, server.ensure_server, port) -- cgit v1.2.3 From e7fa419f13e0afbb3f2653a4b0d8330dd45bbfd0 Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 24 Jul 2013 17:51:03 -0300 Subject: Add doc and tests about events sync/async behaviour. * Also fix docstrings identation so sphynx doesn't complain. --- src/leap/common/tests/test_events.py | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'src/leap/common/tests/test_events.py') diff --git a/src/leap/common/tests/test_events.py b/src/leap/common/tests/test_events.py index 90124b4..bc04dd6 100644 --- a/src/leap/common/tests/test_events.py +++ b/src/leap/common/tests/test_events.py @@ -337,3 +337,92 @@ class EventsTestCase(unittest.TestCase): time.sleep(1) # wait for thread to start. self.assertRaises( server.PortAlreadyTaken, server.ensure_server, port) + + def test_async_register(self): + """ + Test asynchronous registering of callbacks. + """ + flag = Mock() + + # executed after async register, when response is received from server + def reqcbk(request, response): + flag(request.event) + + # callback registered by application + def callback(request): + pass + + # passing a callback as reqcbk param makes the call asynchronous + result = events.register(CLIENT_UID, callback, reqcbk=reqcbk) + self.assertIsNone(result) + events.signal(CLIENT_UID) + time.sleep(1) # wait for signal to arrive from server + flag.assert_called_once_with(CLIENT_UID) + + def test_async_signal(self): + """ + Test asynchronous registering of callbacks. + """ + flag = Mock() + + # executed after async signal, when response is received from server + def reqcbk(request, response): + flag(request.event) + + # passing a callback as reqcbk param makes the call asynchronous + result = events.signal(CLIENT_UID, reqcbk=reqcbk) + self.assertIsNone(result) + time.sleep(1) # wait for signal to arrive from server + flag.assert_called_once_with(CLIENT_UID) + + def test_async_unregister(self): + """ + Test asynchronous unregistering of callbacks. + """ + flag = Mock() + + # executed after async signal, when response is received from server + def reqcbk(request, response): + flag(request.event) + + # callback registered by application + def callback(request): + pass + + # passing a callback as reqcbk param makes the call asynchronous + events.register(CLIENT_UID, callback) + result = events.unregister(CLIENT_UID, reqcbk=reqcbk) + self.assertIsNone(result) + time.sleep(1) # wait for signal to arrive from server + flag.assert_called_once_with(CLIENT_UID) + + def test_async_ping_server(self): + """ + Test asynchronous pinging of server. + """ + flag = Mock() + + # executed after async signal, when response is received from server + def reqcbk(request, response): + flag() + + result = events.ping_server(reqcbk=reqcbk) + self.assertIsNone(result) + time.sleep(1) # wait for response to arrive from server. + flag.assert_called_once_with() + + def test_async_ping_client(self): + """ + Test asynchronous pinging of client. + """ + flag = Mock() + + # executed after async signal, when response is received from server + def reqcbk(request, response): + flag() + + daemon = client.ensure_client_daemon() + result = events.ping_client(daemon.get_port(), reqcbk=reqcbk) + self.assertIsNone(result) + time.sleep(1) # wait for response to arrive from server. + flag.assert_called_once_with() -- cgit v1.2.3 From dcb71ea078a7f56f76bf84b352656cdfa252d677 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 25 Jul 2013 15:20:33 -0300 Subject: Fix events doc about reqcbk signature. Closes #3261 --- src/leap/common/tests/test_events.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/leap/common/tests/test_events.py') diff --git a/src/leap/common/tests/test_events.py b/src/leap/common/tests/test_events.py index bc04dd6..0779b2e 100644 --- a/src/leap/common/tests/test_events.py +++ b/src/leap/common/tests/test_events.py @@ -346,7 +346,7 @@ class EventsTestCase(unittest.TestCase): # executed after async register, when response is received from server def reqcbk(request, response): - flag(request.event) + flag(request.event, response.status) # callback registered by application def callback(request): @@ -357,7 +357,7 @@ class EventsTestCase(unittest.TestCase): self.assertIsNone(result) events.signal(CLIENT_UID) time.sleep(1) # wait for signal to arrive from server - flag.assert_called_once_with(CLIENT_UID) + flag.assert_called_once_with(CLIENT_UID, EventResponse.OK) def test_async_signal(self): """ @@ -367,13 +367,13 @@ class EventsTestCase(unittest.TestCase): # executed after async signal, when response is received from server def reqcbk(request, response): - flag(request.event) + flag(request.event, response.status) # passing a callback as reqcbk param makes the call asynchronous result = events.signal(CLIENT_UID, reqcbk=reqcbk) self.assertIsNone(result) time.sleep(1) # wait for signal to arrive from server - flag.assert_called_once_with(CLIENT_UID) + flag.assert_called_once_with(CLIENT_UID, EventResponse.OK) def test_async_unregister(self): """ @@ -383,7 +383,7 @@ class EventsTestCase(unittest.TestCase): # executed after async signal, when response is received from server def reqcbk(request, response): - flag(request.event) + flag(request.event, response.status) # callback registered by application def callback(request): @@ -394,7 +394,7 @@ class EventsTestCase(unittest.TestCase): result = events.unregister(CLIENT_UID, reqcbk=reqcbk) self.assertIsNone(result) time.sleep(1) # wait for signal to arrive from server - flag.assert_called_once_with(CLIENT_UID) + flag.assert_called_once_with(CLIENT_UID, EventResponse.OK) def test_async_ping_server(self): """ @@ -404,12 +404,12 @@ class EventsTestCase(unittest.TestCase): # executed after async signal, when response is received from server def reqcbk(request, response): - flag() + flag(response.status) result = events.ping_server(reqcbk=reqcbk) self.assertIsNone(result) time.sleep(1) # wait for response to arrive from server. - flag.assert_called_once_with() + flag.assert_called_once_with(EventResponse.OK) def test_async_ping_client(self): """ @@ -419,10 +419,10 @@ class EventsTestCase(unittest.TestCase): # executed after async signal, when response is received from server def reqcbk(request, response): - flag() + flag(response.status) daemon = client.ensure_client_daemon() result = events.ping_client(daemon.get_port(), reqcbk=reqcbk) self.assertIsNone(result) time.sleep(1) # wait for response to arrive from server. - flag.assert_called_once_with() + flag.assert_called_once_with(EventResponse.OK) -- cgit v1.2.3