Repository: opensistemas-hub/osbrain
Pull request #130 (2017-06-02 10:06:23)

ASYNC_REQ/SYNC_PUB server handler tests
This PR should be considered only if #129 ends up merged. These tests will focus on making sure `handler` managing is correct when binding the ASYNC_REQ/SYNC_PUB servers.

codecov[bot]: # [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=h1) Report
> Merging [#130](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=desc) into [master](https://codecov.io/gh/opensistemas-hub/osbrain/commit/276f4619db6040dc2e8a8c5f73aee067a85c4556?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130/graphs/tree.svg?width=650&height=150&src=pr&token=LfvMoNOrOc)](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #130      +/-   ##
==========================================
+ Coverage   97.66%   97.66%   +<.01%     
==========================================
  Files           8        8              
  Lines        1197     1198       +1     
  Branches      180      180              
==========================================
+ Hits         1169     1170       +1     
  Misses         20       20              
  Partials        8        8
```


| [Impacted Files](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [osbrain/agent.py](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=tree#diff-b3NicmFpbi9hZ2VudC5weQ==) | `95.87% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=footer). Last update [276f461...a02e220](https://codecov.io/gh/opensistemas-hub/osbrain/pull/130?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/osbrain/agent.py b/osbrain/agent.py
index b77e811..7e9d543 100644
--- a/osbrain/agent.py
+++ b/osbrain/agent.py
@@ -670,6 +670,7 @@ class Agent():
             The channel where the agent binded to.
         """
         if kind == 'ASYNC_REP':
+            assert handler is not None, 'This socket requires a handler!'
             socket = self.context.socket(zmq.PULL)
             addr = self._bind_socket(socket, addr=addr, transport=transport)
             server_address = AgentAddress(transport, addr, 'PULL', 'server',


Test patch:
diff --git a/osbrain/tests/test_agent_async_requests_handlers.py b/osbrain/tests/test_agent_async_requests_handlers.py
index 1491874..444a1a7 100644
--- a/osbrain/tests/test_agent_async_requests_handlers.py
+++ b/osbrain/tests/test_agent_async_requests_handlers.py
@@ -35,12 +35,39 @@ def receive_function(agent, response):
     agent.received.append(response)
 
 
+def test_async_rep_handler_exists(nsproxy):
+    '''
+    When binding an ASYNC_REP socket without a handler, an exception must be
+    thrown, letting the user know that a handler must be specified.
+    '''
+    server = run_agent('server', base=Agent)
+
+    with pytest.raises(AssertionError) as error:
+        server.bind('ASYNC_REP', alias='should_crash')
+    assert 'This socket requires a handler!' in str(error.value)
+
+
+@pytest.mark.parametrize(
+    'handler',
+    ['reply', receive_function, lambda a, x: a.received.append(x)]
+)
+def test_async_rep_handler_types(nsproxy, handler):
+    '''
+    When binding an ASYNC_REP socket, we must accept different types of
+    handlers: methods, functions, lambda expressions...
+    '''
+    server = run_agent('server', base=Server_ASYNC_REP)
+
+    assert server.bind('ASYNC_REP', alias='should_not_crash',
+                       handler=handler)
+
+
 @pytest.mark.parametrize(
     'handler, check_function',
     [('receive_method', False),
      (receive_function, True),
      (lambda a, x: a.received.append(x), False)])
-def test_connect_handler_types(nsproxy, handler, check_function):
+def test_async_rep_connect_handler_types(nsproxy, handler, check_function):
     '''
     We should be able to specify the handler in the `connect` call in
     different ways: method, functions, lambda expressions...
@@ -70,18 +97,18 @@ def test_connect_handler_types(nsproxy, handler, check_function):
     [('receive_method', False),
      (receive_function, True),
      (lambda a, x: a.received.append(x), False)])
-def test_send_handler_types(nsproxy, handler, check_function):
+def test_async_rep_send_handler_types(nsproxy, handler, check_function):
     '''
-    We should be able to override the handler in the `send` call in
-    different ways: method, functions...
+    We should be able to make requests even if we do not specify a handler
+    on the `connect` call, as long as we specify it on the `send` call.
     '''
     server = run_agent('server', base=Server_ASYNC_REP)
     client = run_agent('client', base=ClientWithHandler)
 
     addr = server.addr('publish')
 
-    # Default handler should not be called when specifying a handler on `send`
-    client.connect(addr, alias='sub', handler='crash_handler')
+    # Connect without a handler
+    client.connect(addr, alias='sub')
 
     client.send('sub', 'request!', handler=handler)
     assert wait_agent_attr(client, length=1)
diff --git a/osbrain/tests/test_agent_sync_publications_handlers.py b/osbrain/tests/test_agent_sync_publications_handlers.py
index af41be9..9c0fdf4 100644
--- a/osbrain/tests/test_agent_sync_publications_handlers.py
+++ b/osbrain/tests/test_agent_sync_publications_handlers.py
@@ -38,12 +38,39 @@ def receive_function(agent, response):
     agent.received.append(response)
 
 
+def test_sync_pub_handler_exists(nsproxy):
+    '''
+    When binding a SYNC_PUB socket without a handler, an exception must be
+    thrown, letting the user know that a handler must be specified.
+    '''
+    server = run_agent('server', base=Agent)
+
+    with pytest.raises(AssertionError) as error:
+        server.bind('SYNC_PUB', alias='should_crash')
+    assert 'This socket requires a handler!' in str(error.value)
+
+
+@pytest.mark.parametrize(
+    'handler',
+    ['reply', receive_function, lambda a, x: a.received.append(x)]
+)
+def test_sync_pub_handler_types(nsproxy, handler):
+    '''
+    When binding a SYNC_PUB socket, we must accept different types of
+    handlers: methods, functions, lambda expressions...
+    '''
+    server = run_agent('server', base=Server_SYNC_PUB)
+
+    assert server.bind('SYNC_PUB', alias='should_not_crash',
+                       handler=handler)
+
+
 @pytest.mark.parametrize(
     'handler, check_function',
     [('receive_method', False),
      (receive_function, True),
      (lambda a, x: a.received.append(x), False)])
-def test_connect_handler_types(nsproxy, handler, check_function):
+def test_sync_pub_connect_handler_types(nsproxy, handler, check_function):
     '''
     The handler for the normal PUB/SUB communication is specified in the
     `connect` call.