Repository: opensistemas-hub/osbrain
Pull request #131 (2017-06-02 11:14:02)

Handler validation raise instead of assert
This PR should be looked only after looking at #130, since the commit comes from that one. Extract the handler validation into a function that throws an exception instead of an assert, to guarantee it is always checked.

codecov[bot]: # [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/131?src=pr&el=h1) Report
> Merging [#131](https://codecov.io/gh/opensistemas-hub/osbrain/pull/131?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/131/graphs/tree.svg?token=LfvMoNOrOc&src=pr&width=650&height=150)](https://codecov.io/gh/opensistemas-hub/osbrain/pull/131?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #131      +/-   ##
==========================================
+ Coverage   97.66%   97.67%   +<.01%     
==========================================
  Files           8        8              
  Lines        1197     1202       +5     
  Branches      180      181       +1     
==========================================
+ Hits         1169     1174       +5     
  Misses         20       20              
  Partials        8        8
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/131?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/131?src=pr&el=footer). Last update [276f461...6d4737f](https://codecov.io/gh/opensistemas-hub/osbrain/pull/131?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Flood1993: Oops, I probably left the "tests failing" in the commit message after rebasing. Fixed already.
Flood1993: You are right! Rewording and pushing into master!

Patch:
diff --git a/osbrain/agent.py b/osbrain/agent.py
index 7e9d543..75b4b85 100644
--- a/osbrain/agent.py
+++ b/osbrain/agent.py
@@ -25,6 +25,7 @@ from . import config
 from .common import format_exception
 from .common import format_method_exception
 from .common import unbound_method
+from .common import validate_handler
 from .common import LogLevel
 from .common import repeat
 from .common import after
@@ -633,8 +634,7 @@ class Agent():
         AgentAddress
             The address where the agent binded to.
         """
-        assert not kind.requires_handler() or handler is not None, \
-            'This socket requires a handler!'
+        validate_handler(handler, required=kind.requires_handler())
         socket = self.context.socket(kind.zmq())
         addr = self._bind_socket(socket, addr=addr, transport=transport)
         server_address = AgentAddress(transport, addr, kind, 'server',
@@ -670,7 +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!'
+            validate_handler(handler, required=True)
             socket = self.context.socket(zmq.PULL)
             addr = self._bind_socket(socket, addr=addr, transport=transport)
             server_address = AgentAddress(transport, addr, 'PULL', 'server',
@@ -769,8 +769,8 @@ class Agent():
         assert server_address.role == 'server', \
             'Incorrect address! A server address must be provided!'
         client_address = server_address.twin()
-        assert not client_address.kind.requires_handler() or \
-            handler is not None, 'This socket requires a handler!'
+        validate_handler(handler,
+                         required=client_address.kind.requires_handler())
         if self.registered(client_address):
             self._connect_old(client_address, alias, handler)
         else:
diff --git a/osbrain/common.py b/osbrain/common.py
index b7579e1..14de969 100644
--- a/osbrain/common.py
+++ b/osbrain/common.py
@@ -47,6 +47,14 @@ def format_method_exception(error, method, args, kwargs):
     return type(error)(message)
 
 
+def validate_handler(handler, required):
+    '''
+    Raises a ValueError exception when a required is handler but not present.
+    '''
+    if required and handler is None:
+        raise ValueError('This socket requires a handler!')
+
+
 class LogLevel(str):
     """
     Identifies the log level: ERROR, WARNING, INFO, DEBUG.


Test patch:
diff --git a/osbrain/tests/test_agent_async_requests_handlers.py b/osbrain/tests/test_agent_async_requests_handlers.py
index 444a1a7..c6eaa63 100644
--- a/osbrain/tests/test_agent_async_requests_handlers.py
+++ b/osbrain/tests/test_agent_async_requests_handlers.py
@@ -42,7 +42,7 @@ def test_async_rep_handler_exists(nsproxy):
     '''
     server = run_agent('server', base=Agent)
 
-    with pytest.raises(AssertionError) as error:
+    with pytest.raises(ValueError) as error:
         server.bind('ASYNC_REP', alias='should_crash')
     assert 'This socket requires a handler!' in str(error.value)
 
diff --git a/osbrain/tests/test_agent_sync_publications_handlers.py b/osbrain/tests/test_agent_sync_publications_handlers.py
index 9c0fdf4..e1f7b05 100644
--- a/osbrain/tests/test_agent_sync_publications_handlers.py
+++ b/osbrain/tests/test_agent_sync_publications_handlers.py
@@ -45,7 +45,7 @@ def test_sync_pub_handler_exists(nsproxy):
     '''
     server = run_agent('server', base=Agent)
 
-    with pytest.raises(AssertionError) as error:
+    with pytest.raises(ValueError) as error:
         server.bind('SYNC_PUB', alias='should_crash')
     assert 'This socket requires a handler!' in str(error.value)