Skip to content

Commit

Permalink
Added docs to the NameServer and added NameServer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Wallar committed Sep 17, 2015
1 parent 9910b47 commit 19890a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/jammi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

__all__ = ["Heart"]
__all__ = ["Heart", "NameServer"]

from heart import Heart
from nameserver import NameServer
8 changes: 4 additions & 4 deletions src/jammi/nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions tests/heart_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions tests/ns_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 19890a9

Please sign in to comment.