Repository: opensistemas-hub/osbrain
Pull request #76 (2017-03-14 11:48:02)

`run_agent()` must wait for agent to start running
This should prevent this happening again:

https://travis-ci.org/Peque/osbrain/jobs/210906487

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


```diff
@@            Coverage Diff             @@
##           master      #76      +/-   ##
==========================================
+ Coverage   96.95%   96.96%   +<.01%     
==========================================
  Files           7        7              
  Lines         985      988       +3     
  Branches      137      138       +1     
==========================================
+ Hits          955      958       +3     
  Misses         21       21              
  Partials        9        9
```


| [Impacted Files](https://codecov.io/gh/opensistemas-hub/osbrain/pull/76?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [osbrain/agent.py](https://codecov.io/gh/opensistemas-hub/osbrain/compare/257d7f85d4f4ef09c0a7a18abc3a43ecae4107d9...69e4c66a596deb47a7889ad1e6c2019943d5c708?src=pr&el=tree#diff-b3NicmFpbi9hZ2VudC5weQ==) | `94.67% <100%> (+0.02%)` | :white_check_mark: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/76?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/76?src=pr&el=footer). Last update [257d7f8...69e4c66](https://codecov.io/gh/opensistemas-hub/osbrain/compare/257d7f85d4f4ef09c0a7a18abc3a43ecae4107d9...69e4c66a596deb47a7889ad1e6c2019943d5c708?el=footer&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 e32e589..310d4c9 100644
--- a/osbrain/agent.py
+++ b/osbrain/agent.py
@@ -10,6 +10,7 @@ import pickle
 import json
 import signal
 import sys
+import time
 import types
 from uuid import uuid4
 
@@ -1342,4 +1343,6 @@ def run_agent(name, nsaddr=None, addr=None, base=Agent, serializer=None):
                  serializer=serializer).start()
     proxy = Proxy(name, nsaddr)
     proxy.run()
+    while not proxy.get_attr('running'):
+        time.sleep(0.01)
     return proxy


Test patch:
diff --git a/osbrain/tests/test_agent.py b/osbrain/tests/test_agent.py
index 998b832..5787c6f 100644
--- a/osbrain/tests/test_agent.py
+++ b/osbrain/tests/test_agent.py
@@ -8,6 +8,7 @@ from uuid import uuid4
 from threading import Timer
 
 import pytest
+import Pyro4
 
 from osbrain import run_logger
 from osbrain import run_agent
@@ -71,6 +72,23 @@ def test_ping(nsaddr):
     assert a0.ping() == 'pong'
 
 
+def test_late_runner(nsaddr):
+    """
+    The `run_agent` function should always make sure the agent is actually
+    running before returning its proxy.
+    """
+    class LateRunner(Agent):
+        @Pyro4.oneway
+        def run(self):
+            time.sleep(1)
+            super().run()
+
+    t0 = time.time()
+    a0 = run_agent('a0', base=LateRunner)
+    assert time.time() - t0 > 1
+    assert a0.ping() == 'pong'
+
+
 def test_agent_shutdown(nsaddr):
     """
     An agent must unregister itself before shutting down.