def test_get_pool(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() with pool.next() as client: client.foo.bar() assert call().start().foo.bar() in FakeClusterRpcProxy.mock_calls destroy_pool()
def test_pool_call_method_notdefined(self): with tools.assert_raises(MethodNotFound): pool = get_pool() with pool.next() as client: client.echo.unknown_method() destroy_pool()
def test_pool_call_no_rabbitmq_server(self): with tools.assert_raises(socket.error): pool = get_pool() with pool.next() as client: client.echo.echo(42) destroy_pool()
def test_pool_call_bad_rabbitmq_cred(self): with tools.assert_raises(AccessRefused): pool = get_pool() with pool.next() as client: client.echo.echo(42) destroy_pool()
def test_pool_call_unknown_service(self): with tools.assert_raises(UnknownService): pool = get_pool() with pool.next() as client: client.unknown_service.echo(42) destroy_pool()
def test_pool_call_no_service(self): pool = get_pool() with pool.next() as client: tools.assert_raises(UnknownService, client.unknownservice.echo) tools.assert_raises(MethodNotFound, client.echo.unknown_method) destroy_pool()
def test_error_clear_context(self): pool = get_pool() with tools.assert_raises(Exception): with pool.next() as client: client.service.method() raise Exception("oops") destroy_pool()
def test_context_data(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() assert pool.context_data.get("data") == 123 # ctx = pool.queue.get_nowait() # context_data = ctx.proxy._worker_ctx.data # assert context_data.get("data") == 123 # ctx.stop() destroy_pool()
def test_pool_destroy_and_recreate(self): pool = get_pool() with pool.next() as client: assert client.echo.echo(42) == [42] destroy_pool() pool = get_pool() with pool.next() as client: assert client.echo.echo(42) == [42] destroy_pool()
def test_pool_call_existing_service(self): pool = get_pool() with pool.next() as client: assert client.echo.echo(42) == [42] # try to call RPC out of with statement tools.assert_raises(AttributeError, lambda: client.echo.echo(42)) # try again inside with statement with pool.next() as client: assert client.echo.echo(42) == [42] destroy_pool()
def test_multi_pool(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool_default = get_pool('default') pool_0 = get_pool() pool1 = get_pool('pool1') pool2 = get_pool('pool2') assert pool1 != pool2 assert pool1.is_started and pool2.is_started assert pool_0.is_started and pool_default == pool_0 assert pool1.config == pool_0.config assert pool1.queue.qsize() == 8 assert pool2.config != pool_0.config tools.assert_raises(ImproperlyConfigured, lambda: get_pool('pool3')) destroy_pool()
def test_multi_pool_context_data(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool1 = get_pool('pool1') pool2 = get_pool('pool2') assert pool1.context_data.get("common") == "multi" assert pool2.context_data.get("common") == "multi" assert pool1.context_data.get("name") == "pool1" assert pool2.context_data.get("name") == "pool2" assert pool1.context_data.get("data") == 123 assert pool2.context_data.get("data") == 321 pool3 = get_pool('pool3') assert pool3.context_data.get("common") == "multi" assert pool3.context_data.get("name") is None assert pool3.queue.qsize() == 8 assert pool1.is_started and pool2.is_started and pool3.is_started tools.assert_raises(ImproperlyConfigured, lambda: get_pool('pool4')) destroy_pool()
def test_connection_error(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() assert pool.queue.qsize() == 4, pool.queue.qsize() client = pool.next() assert pool.queue.qsize() == 3, pool.queue.qsize() with tools.assert_raises(ConnectionError): with pool.next(): assert pool.queue.qsize() == 2, pool.queue.qsize raise ConnectionError("connection closed") assert pool.queue.qsize() == 3, pool.queue.qsize # this has cleared all 4 proxy since runtimeerror is expected to broke them all with client: assert pool.queue.qsize() == 3, pool.queue.qsize() client.foo.bar() assert call().start().foo.bar() in FakeClusterRpcProxy.mock_calls assert pool.queue.qsize() == 4, pool.queue.qsize() destroy_pool()
def test_runtime_error(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() assert pool.queue.qsize() == 4, pool.queue.qsize() client = pool.next() assert pool.queue.qsize() == 3, pool.queue.qsize() with tools.assert_raises(RuntimeError): with pool.next(): assert pool.queue.qsize() == 2, pool.queue.qsize() raise RuntimeError( "This consumer has been stopped, and can no longer be used" ) # this has cleared all 4 proxy since runtimeerror is expected to broke them all assert pool.queue.qsize() == 4, pool.queue.qsize with client: assert pool.queue.qsize() == 4, pool.queue.qsize() client.foo.bar() assert call().start().foo.bar() in FakeClusterRpcProxy.mock_calls assert pool.queue.qsize() == 5, pool.queue.qsize() destroy_pool()
def test_pool_call_rpc_out_of_with_statement(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() with pool.next() as client: client.foo.bar() assert call().start().foo.bar() in FakeClusterRpcProxy.mock_calls # try to call RPC out of with statement tools.assert_raises(AttributeError, lambda: client.foo.bar()) try: client.bar.foo() except AttributeError: pass else: # pragma: nocover raise AssertionError( "AttributeError is expected when call rpc out of with statement" ) # try again inside with statement with pool.next() as client: client.bar.foo() assert call().start().bar.foo() in FakeClusterRpcProxy.mock_calls destroy_pool()
def test_runtime_error(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() assert pool.queue.qsize() == 4, pool.queue.qsize() client = pool.next() # getting client out of pool without using context will make 1 connection go missing assert pool.queue.qsize() == 3, pool.queue.qsize() with client: client.foo.bar() assert call().start().foo.bar() in FakeClusterRpcProxy.mock_calls client._proxy = None # this line will make this client become unusable as it is stopped assert pool.queue.qsize() == 4, pool.queue.qsize() with tools.assert_raises(RuntimeError): # try to loop through all connection in pool, the last one will failed due to RuntimeError for i in range(4): with pool.next(): assert pool.queue.qsize() == 3, pool.queue.qsize() # expect the pool to recover the stopped client assert pool.queue.qsize() == 4, pool.queue.qsize() destroy_pool()
def test_pool_call_no_rabbitmq_server(self): with tools.assert_raises(socket.error): _ = get_pool() destroy_pool()
def test_missing_default_settings(): tools.assert_raises(ImproperlyConfigured, get_pool) destroy_pool()
def test_abd_settings(): tools.assert_raises(ImproperlyConfigured, get_pool) destroy_pool()
def test_custom_pool_size(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: pool = get_pool() assert pool.queue.qsize() == 5 destroy_pool()
def test_pool_call_bad_rabbitmq_cred(self): with tools.assert_raises(AccessRefused): _ = get_pool() destroy_pool()
def test_multi_pool_no_config(): with patch('django_nameko.rpc.ClusterRpcProxy') as FakeClusterRpcProxy: tools.assert_raises(ImproperlyConfigured, lambda: get_pool('pool1')) destroy_pool()