def test_SingleSyncChunk_SingleThen(self): expected = [lambda r: self.assertEqual(1, r.get())] check = checker(expected, self) f = Chain([lambda: 1], ioLoop=self.io_loop) f.then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_SingleSyncChunk_SingleThen(self): expected = [ lambda r: self.assertEqual(1, r.get()), ] check = checker(expected, self) f = Chain([lambda: 1], ioLoop=self.io_loop) f.then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_SingleErrorChunk_SingleThen(self): expected = [lambda r: self.assertRaises(Exception, r.get)] check = checker(expected, self) def firstStep(): raise Exception("Actual") f = Chain([firstStep], ioLoop=self.io_loop) f.then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_AppRestartActionAppIsNotRunningProfileIsNotSpecified(self): node = mock() action = app.Restart(node, **{'name': 'AppName', 'host': '', 'port': ''}) when(common.NodeInfo).execute().thenReturn(Chain([lambda: { 'apps': {} }])) when(app.Stop).execute().thenReturn(Chain([lambda: {'AppName': 'NotRunning'}])) when(app.Start).execute().thenReturn(Chain([lambda: {'AppName': 'Started'}])) self.assertRaises(ToolsError, action.execute().get) verify(common.NodeInfo).execute()
def test_Chain_YieldEmptyServiceInside(self): expected = [lambda r: self.assertEqual("Ok", r.get())] check = checker(expected, self) def firstStep(): r1 = yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.002).execute() yield "Ok" f = Chain([firstStep], ioLoop=self.io_loop) f.then(check) self.wait() self.assertTrue(len(expected) == 0)
def test_SingleErrorChunk_SingleThen(self): expected = [ lambda r: self.assertRaises(Exception, r.get), ] check = checker(expected, self) def firstStep(): raise Exception('Actual') f = Chain([firstStep], ioLoop=self.io_loop) f.then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_SingleSyncChunk_MultipleThen(self): expected = [lambda r: self.assertEqual(6, r.get())] check = checker(expected, self) f = Chain([lambda: 2], ioLoop=self.io_loop) def firstStep(futureResult): r = futureResult.get() r *= 3 return r f.then(firstStep).then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_SingleErrorChunk_MultipleThen(self): expected = [lambda r: self.assertRaises(Exception, r.get)] check = checker(expected, self) def firstStep(): raise Exception("Actual") def secondStep(futureResult): futureResult.get() self.fail("This one should never be seen by anyone") f = Chain([firstStep], ioLoop=self.io_loop) f.then(secondStep).then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_SingleSyncChunk_MultipleThen(self): expected = [ lambda r: self.assertEqual(6, r.get()), ] check = checker(expected, self) f = Chain([lambda: 2], ioLoop=self.io_loop) def firstStep(futureResult): r = futureResult.get() r *= 3 return r f.then(firstStep).then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def firstStep(): r1 = yield Chain([lambda: 1], ioLoop=self.io_loop) yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.002).execute() yield r1
def test_YieldChainAndEmptyServiceInside_MultipleSteps(self): expected = [lambda r: self.assertEqual("Really Ok", r.get())] check = checker(expected, self) def firstStep(): yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.004).execute() yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.002).execute() yield "Ok" def secondStep(result): yield "Really " + result.get() f = Chain([firstStep], ioLoop=self.io_loop).then(secondStep) f.then(check) self.wait() self.assertTrue(len(expected) == 0)
def test_ProfileListAction(self): storage = mock() action = profile.List(storage) when(storage).find(any(str), any(tuple)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get(timeout=0.1) verify(storage).find('profiles', PROFILES_TAGS)
def test_CrashlogListAction(self): storage = mock() action = crashlog.List(storage, **{'name': 'CrashlogName'}) when(storage).find(any(str), any(list)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).find('crashlogs', ['CrashlogName'])
def test_RunlistRemoveAction(self): storage = mock() action = runlist.Remove(storage, **{'name': 'RunlistName'}) when(storage).remove(any(str), any(str)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).remove('runlists', 'RunlistName')
def test_RunlistUploadActionRawRunlistProvided(self): storage = mock() action = runlist.Upload(storage, **{'name': 'RunlistName', 'runlist': '{}'}) when(storage).write(any(str), any(str), any(str), any(tuple)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).write('runlists', 'RunlistName', msgpack.dumps({}), RUNLISTS_TAGS)
def test_RunlistListAction(self): storage = mock() action = runlist.List(storage) when(storage).find(any(str), any(tuple)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).find('runlists', RUNLISTS_TAGS)
def test_ProfileRemoveAction(self): storage = mock() action = profile.Remove(storage, **{'name': 'ProfileName'}) when(storage).remove(any(str), any(str)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).remove('profiles', 'ProfileName')
def test_SingleErrorChunk_MultipleThen(self): expected = [ lambda r: self.assertRaises(Exception, r.get), ] check = checker(expected, self) def firstStep(): raise Exception('Actual') def secondStep(futureResult): futureResult.get() self.fail('This one should never be seen by anyone') f = Chain([firstStep], ioLoop=self.io_loop) f.then(secondStep).then(check) self.wait(timeout=0.5) self.assertTrue(len(expected) == 0)
def test_Chain_YieldEmptyServiceInside(self): expected = [ lambda r: self.assertEqual('Ok', r.get()), ] check = checker(expected, self) def firstStep(): r1 = yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.002).execute() yield 'Ok' f = Chain([firstStep], ioLoop=self.io_loop) f.then(check) self.wait() self.assertTrue(len(expected) == 0)
def test_RunlistUploadAction(self): storage = mock() jsonEncoder = mock() action = runlist.Upload(storage, **{'name': 'RunlistName', 'runlist': 'r.json'}) action.jsonEncoder = jsonEncoder when(jsonEncoder).encode('r.json').thenReturn('{-encodedJson-}') when(storage).write(any(str), any(str), any(str), any(tuple)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).write('runlists', 'RunlistName', '{-encodedJson-}', RUNLISTS_TAGS)
def test_ProfileUploadAction(self): storage = mock() jsonEncoder = mock() action = profile.Upload(storage, **{'name': 'ProfileName', 'profile': 'p.json'}) action.jsonEncoder = jsonEncoder when(jsonEncoder).encode('p.json').thenReturn('{-encodedJson-}') when(storage).write(any(str), any(str), any(str), any(tuple)).thenReturn(Chain([lambda: 'Ok'])) action.execute().get() verify(storage).write('profiles', 'ProfileName', '{-encodedJson-}', PROFILES_TAGS)
def test_AppRestartActionAppIsRunningProfileIsSpecified(self): node = mock() action = app.Restart(node, **{'name': 'AppName', 'profile': 'NewProfile', 'host': '', 'port': ''}) when(common.NodeInfo).execute().thenReturn(Chain([lambda: { 'apps': { 'AppName': { 'profile': 'ProfileName', 'state': 'running', } } }])) when(app.Stop).execute().thenReturn(Chain([lambda: {'AppName': 'Stopped'}])) when(app.Start).execute().thenReturn(Chain([lambda: {'AppName': 'Started'}])) action.execute().get() verify(common.NodeInfo).execute() verify(app.Stop).execute() verify(app.Start).execute()
def test_AppRestartActionAppIsRunningProfileIsNotSpecified(self): node = mock() locator = mock() action = app.Restart(node, locator, **{'name': 'AppName', 'profile': 'default'}) when(common.NodeInfo).execute().thenReturn(Chain([lambda: { 'apps': { 'AppName': { 'profile': 'ProfileName', 'state': 'running', } } }])) when(app.Stop).execute().thenReturn(Chain([lambda: {'AppName': 'Stopped'}])) when(app.Start).execute().thenReturn(Chain([lambda: {'AppName': 'Started'}])) action.execute().get(timeout=0.1) verify(common.NodeInfo).execute() verify(app.Stop).execute() verify(app.Start).execute()
def test_AppCheckActionReturnsStoppedOrMissingWhenApplicationIsNotFound(self): node = mock() storage = mock() locator = mock() action = app.Check(node, storage, locator, **{'name': 'AppName'}) mockInfo = { 'apps': { } } when(node).info().thenReturn(Chain([lambda: mockInfo])) actual = action.execute().get(timeout=0.1) verify(node).info() self.assertEqual({'AppName': 'stopped or missing'}, actual)
def test_YieldChainAndEmptyServiceInside_MultipleSteps(self): expected = [ lambda r: self.assertEqual('Really Ok', r.get()), ] check = checker(expected, self) def firstStep(): yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.004).execute() yield ServiceMock(chunks=[], T=self.T, ioLoop=self.io_loop, interval=0.002).execute() yield 'Ok' def secondStep(result): yield 'Really ' + result.get() f = Chain([firstStep], ioLoop=self.io_loop).then(secondStep) f.then(check) self.wait() self.assertTrue(len(expected) == 0)
def wrapper(*args, **kwargs): future = Deferred() timeout = kwargs.get('timeout', None) if timeout is not None: timeoutId = self._ioLoop.add_timeout( time() + timeout, lambda: future.error(TimeoutError(timeout))) def timeoutRemover(func): def wrapper(*args, **kwargs): self._ioLoop.remove_timeout(timeoutId) return func(*args, **kwargs) return wrapper future.close = timeoutRemover(future.close) self._session += 1 self._writableStream.write([methodId, self._session, args]) self._subscribers[self._session] = future return Chain([lambda: future], ioLoop=self._ioLoop)
def get_response(self, request): try: urlconf = settings.ROOT_URLCONF urlresolvers.set_urlconf(urlconf) resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) try: response = None for middleware_method in self._request_middleware: response = middleware_method(request) if response: break if response is None: if hasattr(request, 'urlconf'): urlconf = request.urlconf urlresolvers.set_urlconf(urlconf) resolver = urlresolvers.RegexURLResolver( r'^/', urlconf) resolver_match = resolver.resolve(request.path_info) callback, callback_args, callback_kwargs = resolver_match request.resolver_match = resolver_match for middleware_method in self._view_middleware: response = middleware_method( request, callback, callback_args, callback_kwargs) if response: break if response is None: try: response = yield Chain([ lambda: callback(request, *callback_args, ** callback_kwargs) ]) except Exception as e: for middleware_method in self._exception_middleware: response = middleware_method(request, e) if response: break if response is None: raise if response is None: if isinstance(callback, types.FunctionType): # FBV view_name = callback.__name__ else: # CBV view_name = callback.__class__.__name__ + '.__call__' raise ValueError( "The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name)) if hasattr(response, 'render') and callable( response.render): for middleware_method in self._template_response_middleware: response = middleware_method(request, response) response = response.render() except http.Http404 as e: log.warning('Not Found: %s', request.path, extra={ 'status_code': 404, 'request': request }) if settings.DEBUG: response = debug.technical_404_response(request, e) else: try: callback, param_dict = resolver.resolve404() response = callback(request, **param_dict) except: signals.got_request_exception.send( sender=self.__class__, request=request) response = self.handle_uncaught_exception( request, resolver, sys.exc_info()) except exceptions.PermissionDenied: log.warning('Forbidden (Permission denied): %s', request.path, extra={ 'status_code': 403, 'request': request }) try: callback, param_dict = resolver.resolve403() response = callback(request, **param_dict) except: signals.got_request_exception.send( sender=self.__class__, request=request) response = self.handle_uncaught_exception( request, resolver, sys.exc_info()) except SystemExit: raise except: signals.got_request_exception.send(sender=self.__class__, request=request) response = self.handle_uncaught_exception( request, resolver, sys.exc_info()) finally: urlresolvers.set_urlconf(None) try: for middleware_method in self._response_middleware: response = middleware_method(request, response) response = self.apply_response_fixes(request, response) except: signals.got_request_exception.send(sender=self.__class__, request=request) response = self.handle_uncaught_exception( request, resolver, sys.exc_info()) yield response
def invoke(self, request, stream): self._state = 1 self._response = stream # attach response stream Chain([lambda: self._obj(request, self._response), self.on_error])
from cocaine.services import Service __author__ = 'EvgenySafronov <*****@*****.**>' if __name__ == '__main__': if len(sys.argv) != 2: print('Usage: chunker.py NUMBER_OF_CHUNKS') exit(os.EX_USAGE) def fetchAll(): chunk = yield service.enqueue('chunkMe', str(sys.argv[1])) chunk = msgpack.loads(chunk) size = len(chunk) counter = 0 while True: ch = yield chunk = msgpack.loads(ch) size += len(chunk) counter += 1 print(counter, len(chunk), size) if chunk == 'Done': break service = Service('Chunker') c = Chain([fetchAll]) try: c.get() except ChokeEvent: print('Done')
'measureRPS': True } c = Counter(config['benchmark']['maxRequests'], config['benchmark']['tickLimit']) service = Service(config['service']['name']) if config['printObjectGraph']: print('Object graph.') objgraph.show_growth() print('') start = time() print('Total requests: {0}'.format(c.maxRequests)) loop = IOLoop.instance() p = PeriodicCallback(lambda: Chain([example_AsynchronousChaining]), config['benchmark']['interval'], io_loop=loop) p.start() loop.start() print('') if config['measureTime']: stop = time() print('Total elapsed: {0:.3f}s'.format(stop - start)) if config['measureRPS']: stop = time() print('RPS: {0:.3f}'.format(config['benchmark']['maxRequests'] / (stop - start)))
def wrapper(self, *args, **kwargs): Chain([ lambda: func(self, *args, **kwargs), lambda result: self.io_loop.stop() ]) self.io_loop.start()
for chunk in service.perform_sync('enqueue', 'doIt', 'SomeMessage'): print('example_SynchronousFetching: Response received - {0}'.format( msgpack.loads(chunk))) def example_Synchronous(): message = service.enqueue('doIt', 'SomeMessage').get() print('example_Synchronous: Response received - {0}'.format( msgpack.loads(message))) def example_AsynchronousYielding(): message = yield service.enqueue('doIt', 'SomeMessage') print('example_AsynchronousYielding: Response received - {0}'.format( msgpack.loads(message))) def example_AsynchronousChaining(): return service.enqueue('doIt', 'SomeMessage') if __name__ == '__main__': service = Service('Echo') example_SynchronousFetching() example_Synchronous() Chain([example_AsynchronousYielding]).wait() example_AsynchronousChaining()\ .then(lambda r: msgpack.loads(r.get()))\ .then(lambda r: sys.stdout.write('example_AsynchronousChaining: Response received - {0}\n'.format(r.get())))\ .wait() sys.stdout.flush()