From 19890a94e8ca08c33c9d364e55e5012cbf9b13c8 Mon Sep 17 00:00:00 2001 From: Alex Wallar Date: Thu, 17 Sep 2015 12:28:44 -0400 Subject: [PATCH] Added docs to the NameServer and added NameServer test --- src/jammi/__init__.py | 3 ++- src/jammi/nameserver.py | 8 ++++---- tests/heart_test.py | 5 +---- tests/ns_test.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 tests/ns_test.py diff --git a/src/jammi/__init__.py b/src/jammi/__init__.py index 3456c0b..108e783 100644 --- a/src/jammi/__init__.py +++ b/src/jammi/__init__.py @@ -1,4 +1,5 @@ -__all__ = ["Heart"] +__all__ = ["Heart", "NameServer"] from heart import Heart +from nameserver import NameServer diff --git a/src/jammi/nameserver.py b/src/jammi/nameserver.py index 4ae6567..4ecf1ca 100644 --- a/src/jammi/nameserver.py +++ b/src/jammi/nameserver.py @@ -59,8 +59,8 @@ def get_alive(self): alive = list() ctime = time.time() - for name, t in times.iteritems(): - if ctime - t < interval: + for name, t in self.times.iteritems(): + if ctime - t < self.interval: alive.append(name) return alive @@ -114,10 +114,10 @@ def start(self): else: def __thread(): while self.running: - data_zip, _ = sock.recvfrom(1024) + data_zip, _ = self.sock.recvfrom(1024) data_str = zlib.decompress(data_zip) data = json.loads(data_str) - self.master[data["name"]] = data + self.masters[data["name"]] = data self.times[data["name"]] = time.time() self.running = True self.thread = threading.Thread(target=__thread) diff --git a/tests/heart_test.py b/tests/heart_test.py index f8d8e8a..3777621 100644 --- a/tests/heart_test.py +++ b/tests/heart_test.py @@ -22,12 +22,9 @@ def test_start(): heart = jammi.Heart(host, port, rate) heart.data["name"] = "test" heart.start() - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((host, port)) - i = 0 - while i < 5: - i += 1 + for i in xrange(5): data_zip, addr = sock.recvfrom(1024) data = zlib.decompress(data_zip) assert(json.loads(data) == heart.data) diff --git a/tests/ns_test.py b/tests/ns_test.py new file mode 100644 index 0000000..cb5bb65 --- /dev/null +++ b/tests/ns_test.py @@ -0,0 +1,35 @@ + +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), "../src")) + +import jammi +import rospy + + +def test_start(): + """ + Tests that the NameServer is able to store "alive" masters + """ + + rospy.init_node("jammi_test", anonymous=False) + host, port = "localhost", 5005 + rate = 20 + ns = jammi.NameServer(host, port, 3) + ns.start() + heart = jammi.Heart(host, port, rate) + heart.data["name"] = "test" + heart.start() + r = rospy.Rate(10) + alive_set = set() + for i in xrange(5): + alive_set.update(set(ns.get_alive())) + r.sleep() + assert(list(alive_set)[0] == heart.data["name"]) + heart.kill() + ns.kill() + + +if __name__ == "__main__": + test_start()