Repository: opensistemas-hub/osbrain
Pull request #186 (2017-08-01 09:27:19)

Refactor `test_agent_pubsub_topics.py`
To prevent synchronization issues.

See https://travis-ci.org/opensistemas-hub/osbrain/jobs/259671624.

codecov[bot]: # [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/186?src=pr&el=h1) Report
> Merging [#186](https://codecov.io/gh/opensistemas-hub/osbrain/pull/186?src=pr&el=desc) into [master](https://codecov.io/gh/opensistemas-hub/osbrain/commit/69030291edf45218bcf673a48cc7c48c82068c54?src=pr&el=desc) will **decrease** coverage by `0.15%`.
> The diff coverage is `91.3%`.

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

```diff
@@            Coverage Diff            @@
##           master    #186      +/-   ##
=========================================
- Coverage   98.85%   98.7%   -0.16%     
=========================================
  Files          25      25              
  Lines        3154    3172      +18     
  Branches      234     240       +6     
=========================================
+ Hits         3118    3131      +13     
- Misses         24      28       +4     
- Partials       12      13       +1
```


| [Impacted Files](https://codecov.io/gh/opensistemas-hub/osbrain/pull/186?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [osbrain/tests/test\_agent\_pubsub\_topics.py](https://codecov.io/gh/opensistemas-hub/osbrain/pull/186?src=pr&el=tree#diff-b3NicmFpbi90ZXN0cy90ZXN0X2FnZW50X3B1YnN1Yl90b3BpY3MucHk=) | `98.24% <91.3%> (-1.76%)` | :arrow_down: |
| [osbrain/agent.py](https://codecov.io/gh/opensistemas-hub/osbrain/pull/186?src=pr&el=tree#diff-b3NicmFpbi9hZ2VudC5weQ==) | `96.17% <0%> (-0.45%)` | :arrow_down: |

------

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

Peque: Added a new commit to prevent https://travis-ci.org/opensistemas-hub/osbrain/jobs/259699063.
Peque: Fixed another problem... https://travis-ci.org/opensistemas-hub/osbrain/jobs/259719762

:sweat_smile: 
Flood1993: As a minor issue, can you correct the topic of this PR? :P

Patch:
diff --git a/osbrain/helper.py b/osbrain/helper.py
index 7827f3f..0fa7062 100644
--- a/osbrain/helper.py
+++ b/osbrain/helper.py
@@ -223,3 +223,32 @@ def wait_agent_condition(agent, condition, *args, timeout=3, **kwargs):
         if time.time() - t0 > timeout:
             return False
         time.sleep(0.01)
+
+
+def last_received_endswith(agent, tail):
+    """
+    Check if the agent's last received message ends with `tail`.
+
+    It is supposed to be executed as an agent method (in example: use it as
+    the condition in `wait_agent_condition` or execute it with the method
+    `execute_as_method`).
+
+    Parameters
+    ----------
+    agent : Proxy
+        A proxy to the agent.
+    tail : str or bytes
+        The expected termination of the last received message.
+
+    Returns
+    -------
+    bool
+        Whether the last message received ends with `tail`.
+    """
+    if not hasattr(agent, 'received'):
+        return False
+    if not agent.received:
+        return False
+    if not agent.received[-1][-len(tail):] == tail:
+        return False
+    return True


Test patch:
diff --git a/osbrain/tests/test_agent_pubsub_topics.py b/osbrain/tests/test_agent_pubsub_topics.py
index c122c5f..28e2730 100644
--- a/osbrain/tests/test_agent_pubsub_topics.py
+++ b/osbrain/tests/test_agent_pubsub_topics.py
@@ -1,9 +1,9 @@
-import time
-
 import pytest
 
 from osbrain import run_agent
 from osbrain.address import AgentAddressSerializer
+from osbrain.helper import wait_agent_condition
+from osbrain.helper import last_received_endswith
 
 from common import nsproxy  # pragma: no flakes
 from common import append_received
@@ -40,8 +40,12 @@ def test_pubsub_topics_separator(nsproxy, serializer):
     a4.connect(addr, handler={'bar': append_received})
     a5.connect(addr, handler={'fo': append_received})
 
-    # Give some time for all the agents to connect
-    time.sleep(0.1)
+    # Make sure all agents are connected
+    a0.each(0.1, 'send', 'pub', 'first', topic='foo')
+    a0.each(0.1, 'send', 'pub', 'first', topic='bar')
+    for agent in (a1, a2, a3, a4, a5):
+        assert wait_agent_condition(agent, last_received_endswith, 'first')
+    a0.stop_all_timers()
 
     # Send some messages
     message_01 = 'Hello'
@@ -56,8 +60,11 @@ def test_pubsub_topics_separator(nsproxy, serializer):
     message_04 = 'BAR'
     a0.send('pub', message_04, topic='fo')
 
-    # Give some time for all the agents to handle the message
-    time.sleep(0.1)
+    # Make sure all messages are processed
+    a0.send('pub', 'last', topic='foo')
+    a0.send('pub', 'last', topic='bar')
+    for agent in (a1, a2, a3, a4, a5):
+        assert wait_agent_condition(agent, last_received_endswith, 'last')
 
     # Check each agent received the corresponding messages
     assert message_01 in a1.get_attr('received')
@@ -120,8 +127,12 @@ def test_pubsub_topics_raw(nsproxy, serializer):
     a4.connect(addr, handler={'bar': append_received})
     a5.connect(addr, handler={'fo': append_received})
 
-    # Give some time for all the agents to connect
-    time.sleep(0.1)
+    # Make sure all agents are connected
+    a0.each(0.1, 'send', 'pub', b'first', topic='foo')
+    a0.each(0.1, 'send', 'pub', b'first', topic='bar')
+    for agent in (a1, a2, a3, a4, a5):
+        assert wait_agent_condition(agent, last_received_endswith, b'first')
+    a0.stop_all_timers()
 
     # Send some messages
     message_01 = b'Hello'
@@ -136,8 +147,11 @@ def test_pubsub_topics_raw(nsproxy, serializer):
     message_04 = b'BAR'
     a0.send('pub', message_04, topic='fo')
 
-    # Give some time for all the agents to handle the message
-    time.sleep(0.1)
+    # Make sure all agents are connected
+    a0.send('pub', b'last', topic='foo')
+    a0.send('pub', b'last', topic='bar')
+    for agent in (a1, a2, a3, a4, a5):
+        assert wait_agent_condition(agent, last_received_endswith, b'last')
 
     # Check each agent received the corresponding messages
     assert message_01 in a1.get_attr('received')
diff --git a/osbrain/tests/test_helper.py b/osbrain/tests/test_helper.py
index 53be718..339535e 100644
--- a/osbrain/tests/test_helper.py
+++ b/osbrain/tests/test_helper.py
@@ -10,6 +10,7 @@ from osbrain import Agent
 from osbrain.helper import logger_received
 from osbrain.helper import agent_dies
 from osbrain.helper import attribute_match_all
+from osbrain.helper import last_received_endswith
 from osbrain.helper import wait_agent_attr
 from osbrain.helper import wait_agent_condition
 
@@ -167,3 +168,19 @@ def test_wait_agent_condition(nsproxy):
     assert not wait_agent_condition(a0, log_length, size=4, timeout=0.2)
     assert wait_agent_condition(a0, log_length, size=10, timeout=1.)
     assert wait_agent_condition(a0, lambda agent: agent.count > 8, timeout=0.)
+
+
+def test_last_received_endswith(nsproxy):
+    """
+    Test `last_received_endswith` function.
+    """
+    agent = run_agent('a0')
+    assert not agent.execute_as_method(last_received_endswith, 'foo')
+    agent.set_attr(received=[])
+    assert not agent.execute_as_method(last_received_endswith, 'foo')
+    agent.set_attr(received=['bar'])
+    assert not agent.execute_as_method(last_received_endswith, 'foo')
+    agent.set_attr(received=['foo', 'bar'])
+    assert not agent.execute_as_method(last_received_endswith, 'foo')
+    agent.set_attr(received=['bar', 'foo'])
+    assert agent.execute_as_method(last_received_endswith, 'foo')
diff --git a/osbrain/tests/test_nameserver.py b/osbrain/tests/test_nameserver.py
index a4bd38c..c704af2 100644
--- a/osbrain/tests/test_nameserver.py
+++ b/osbrain/tests/test_nameserver.py
@@ -121,11 +121,11 @@ def test_nameserver_proxy_shutdown_lazy_agents(delay, timeout):
             ns.shutdown(timeout=delay-1)
         assert 'not all agents were shutdown' in str(error.value)
         # Agent should die soon anyway
-        assert agent_dies('a0', nsproxy=ns, timeout=1.5)
+        assert agent_dies('a0', nsproxy=ns, timeout=2.)
         assert agent_dies('a1', nsproxy=ns, timeout=0.)
     ns.shutdown()
     assert time.time() - t0 > delay
-    assert time.time() - t0 < delay + 1
+    assert time.time() - t0 < delay + 2
 
 
 def test_oneway_kill_non_running_agent_on_name_server_shutdown(nsproxy):