def test_rpc_protobuf(self): def echo(value): value.string = "received" return value def raw_received(session, event): msg = '\0' + base64.b64encode(event.data[1]).decode('ascii') res = session.call_rpc('/test/rpc', 'echo', msg, 'rst.generic.Value', 'rst.generic.Value') session.scopes['/test/out'].on_wamp_message(res) def protobuf_received(passed, lock, event): if hasattr(event.data, 'type') and event.data.string == 'received': passed() lock.release() rsb.converter.registerGlobalConverter( rsb.converter.ProtocolBufferConverter(messageClass=Value), True) self.session.register_scope('/test/scope', 'rst.generic.Value') self.session.register_scope('/test/out', 'rst.generic.Value') with rsb.createLocalServer('/test/rpc') as server: server.addMethod('echo', echo, Value, Value) with rsb.createListener('/test/scope', config=self.session.rsb_conf) as listener: listener.addHandler(partial(raw_received, self.session)) with rsb.createListener('/test/out') as out: out.addHandler(partial(protobuf_received, self.passed, self.lock)) with rsb.createInformer('/test/scope', dataType=Value) as informer: v = Value() v.type = Value.STRING v.string = "hello" informer.publishData(v) self.lock.acquire() self.assertTrue(self.passed.called)
def testParallelCallOfOneMethod(self): numParallelCalls = 3 runningCalls = [0] callLock = Condition() with rsb.createLocalServer('/takesometime', inProcessNoIntrospectionConfig) \ as localServer: def takeSomeTime(e): with callLock: runningCalls[0] = runningCalls[0] + 1 callLock.notifyAll() with callLock: while runningCalls[0] < numParallelCalls: callLock.wait() localServer.addMethod("takeSomeTime", takeSomeTime, str, allowParallelExecution=True) with rsb.createRemoteServer('/takesometime', inProcessNoIntrospectionConfig) \ as remoteServer: results = [ remoteServer.takeSomeTime. async ('call{}'.format(x)) for x in range(numParallelCalls) ] for r in results: r.get(10)
def testConstruction(self): # Test creating a server without methods with rsb.createLocalServer('/some/scope', inProcessNoIntrospectionConfig) as server: self.assertEqual(server.methods, []) with rsb.createLocalServer(rsb.Scope('/some/scope'), inProcessNoIntrospectionConfig) as server: self.assertEqual(server.methods, []) # Test creating a server with directly specified methods with rsb.createLocalServer(rsb.Scope('/some/scope'), methods=[('foo', lambda x: x, str, str)], config=inProcessNoIntrospectionConfig) \ as server: self.assertEqual([m.name for m in server.methods], ['foo']) # Test creating a server that exposes method of an existing # object class SomeClass(object): def bar(x): pass someObject = SomeClass() with rsb.createLocalServer(rsb.Scope('/some/scope'), object=someObject, expose=[('bar', str, None)], config=inProcessNoIntrospectionConfig) \ as server: self.assertEqual([m.name for m in server.methods], ['bar']) # Cannot supply expose without object self.assertRaises(ValueError, rsb.createLocalServer, '/some/scope', expose=[('bar', str, None)]) # Cannot supply these simultaneously self.assertRaises(ValueError, rsb.createLocalServer, '/some/scope', object=someObject, expose=[('bar', str, None)], methods=[('foo', lambda x: x, str, str)])
def testLocalServer(self): server = None method = None with rsb.createLocalServer('/') as participant: server = participant self.assertEqual(self.creationCalls, [(server, None)]) method = server.addMethod('echo', lambda x: x) self.assertTrue((method, server) in self.creationCalls) self.assertTrue(server in self.destructionCalls) self.assertTrue(method in self.destructionCalls)
def testNonIdentifierMethodName(self): serverScope = '/non-identifier-server' methodName = 'non-identifier-method' with rsb.createLocalServer(serverScope, inProcessNoIntrospectionConfig) \ as localServer: localServer.addMethod(methodName, lambda x: x, str, str) with rsb.createRemoteServer(serverScope, inProcessNoIntrospectionConfig) \ as remoteServer: self.assertEqual( remoteServer.getMethod(methodName)('foo'), 'foo')
def testVoidMethods(self): with rsb.createLocalServer('/void', inProcessNoIntrospectionConfig) \ as localServer: def nothing(e): pass localServer.addMethod("nothing", nothing, str) with rsb.createRemoteServer('/void', inProcessNoIntrospectionConfig) \ as remoteServer: future = remoteServer.nothing. async ("test") future.get(1)
def test_rpc(self): def echo(x): return x def squared(x): return x*x with rsb.createLocalServer('/test/rpc') as server: server.addMethod('echo', echo, str, str) server.addMethod('squared', squared, int, int) res = self.session.call_rpc('/test/rpc', 'echo', "foo", 'string', 'string') self.assertEqual(res, "foo") res = self.session.call_rpc('/test/rpc', 'squared', 5, 'string', 'integer') self.assertEqual(res, 25)
def __init__(self, owning_component_name, channel=None, participant_config=None): '''Create an OutputBuffer. Keyword arguments: owning_component_name -- name of the entity that own this buffer participant_config -- RSB configuration ''' super(OutputBuffer, self).__init__(owning_component_name, channel, participant_config) self._unique_name = '/ipaaca/component/' + str(owning_component_name) + 'ID' + self._uuid + '/OB' self._server = rsb.createLocalServer(rsb.Scope(self._unique_name)) self._server.addMethod('updateLinks', self._remote_update_links, ipaaca.converter.IULinkUpdate, int) self._server.addMethod('updatePayload', self._remote_update_payload, ipaaca.converter.IUPayloadUpdate, int) self._server.addMethod('commit', self._remote_commit, ipaaca.ipaaca_pb2.IUCommission, int) self._server.addMethod('resendRequest', self._remote_request_resend, ipaaca.ipaaca_pb2.IUResendRequest, int) self._informer_store = {} self._id_prefix = str(owning_component_name)+'-'+str(self._uuid)+'-IU-' self.__iu_id_counter_lock = threading.Lock()
def testRoundTrip(self): with rsb.createLocalServer('/roundtrip', methods=[('addone', lambda x: long(x + 1), long, long)], config=inProcessNoIntrospectionConfig): with rsb.createRemoteServer('/roundtrip', inProcessNoIntrospectionConfig) \ as remoteServer: # Call synchronously self.assertEqual(map(remoteServer.addone, range(100)), range(1, 101)) # Call synchronously with timeout self.assertEqual( [remoteServer.addone(x, timeout=10) for x in range(100)], range(1, 101)) # Call asynchronously self.assertEqual( map(lambda x: x.get(), map(remoteServer.addone. async, range(100))), range(1, 101))
# # ============================================================ # mark-start::body import time import logging import rsb if __name__ == '__main__': # Pacify logger. logging.basicConfig() # Create a LocalServer object that exposes its methods under the # scope /example/server. with rsb.createLocalServer('/example/server') as server: # Create a function which processes requests and returns a # result. Note that the name of the function does not determine # the name of the exposed method. See addMethod below. def echo(x): return x # Add the function to the server under the name "echo". server.addMethod('echo', echo, str, str) # Wait for method calls by clients. while True: time.sleep(1) # mark-end::body