def test_load_config(self): #Testing fail case with self.assertRaises(IOError) as err: HerdClient(config='/etc/etc/sdfsdfasdiff.conf') missing_exception = err.exception self.assertEqual(missing_exception.errno, 2) #Testing load works self.client = HerdClient(config=self.config_path) self.assertEqual(self.client.seed, "127.0.0.1:8338") #Testing passing arguments in self.client = HerdClient(seed="127.0.0.2:8888", config=self.config_path) self.assertEqual(self.client.seed, "127.0.0.2:8888") with self.assertRaises(AttributeError): HerdClient(config="%s/configs/failed.conf" % (self.real_path))
def test_request(self): from gevent.server import StreamServer def handle_req(sock, address): timeout = gevent.spawn_later(10, lambda: sock.shutdown(0)) fileobj = sock.makefile() while True: try: line = fileobj.readline() except IOError: line = None timeout.kill() if not line: break else: msg = json.loads(line) msg['foo'] = 'bar' if msg: fileobj.write(json.dumps(msg)) fileobj.flush() fileobj.close() sock.close() break self.stream = StreamServer(('127.0.0.1', 8338), handle_req) self.stream.start() self.client = HerdClient(seed="127.0.0.1:8338") msg = { 'testing': 'this' } response = self.client.request(msg) self.assertEqual(response['foo'], 'bar') self.assertEqual(response['testing'], 'this') self.stream.stop()
class ClientTest(TestCase): def setUp(self): self.real_path = os.path.dirname(os.path.realpath(__file__)) self.config_path = "%s/configs/herd_client.conf" % (self.real_path) def test_load_config(self): #Testing fail case with self.assertRaises(IOError) as err: HerdClient(config='/etc/etc/sdfsdfasdiff.conf') missing_exception = err.exception self.assertEqual(missing_exception.errno, 2) #Testing load works self.client = HerdClient(config=self.config_path) self.assertEqual(self.client.seed, "127.0.0.1:8338") #Testing passing arguments in self.client = HerdClient(seed="127.0.0.2:8888", config=self.config_path) self.assertEqual(self.client.seed, "127.0.0.2:8888") with self.assertRaises(AttributeError): HerdClient(config="%s/configs/failed.conf" % (self.real_path)) def test_request(self): from gevent.server import StreamServer def handle_req(sock, address): timeout = gevent.spawn_later(10, lambda: sock.shutdown(0)) fileobj = sock.makefile() while True: try: line = fileobj.readline() except IOError: line = None timeout.kill() if not line: break else: msg = json.loads(line) msg['foo'] = 'bar' if msg: fileobj.write(json.dumps(msg)) fileobj.flush() fileobj.close() sock.close() break self.stream = StreamServer(('127.0.0.1', 8338), handle_req) self.stream.start() self.client = HerdClient(seed="127.0.0.1:8338") msg = { 'testing': 'this' } response = self.client.request(msg) self.assertEqual(response['foo'], 'bar') self.assertEqual(response['testing'], 'this') self.stream.stop()
def manager2(): manager = HerdManager(address='127.0.0.1:8339', port=8339, ip='127.0.0.4', config=None, stream_ip='127.0.0.4', stream_port=8338) manager.start_listener() print "Starting Herd Managers" m1 = Greenlet(manager1) m1.start() m2 = Greenlet(manager2) m2.start() print "Ensure there are no existing groups in the cluster" gevent.sleep(8) msg = {'command': 'groups', 'data': None } manager_client = HerdClient(seed='127.0.0.1:8338') response = manager_client.request(msg) print response #Start two cluster nodes def node1(): groups=['phalliday'] managers = ['127.0.0.1:8338'] seeds = [] address = '127.0.0.2:14922' cluster = HerdCluster(seeds=seeds, address=address, groups=groups, ping_time=5000, managers=managers) cluster.start() def node2(): groups=['drizzt51','phalliday'] managers = ['127.0.0.1:8338']
import logging import random from herd.cluster.server import HerdCluster from herd.manager.client import HerdClient logger = logging.getLogger('') logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler()) groups=['phalliday'] managers = ['127.0.0.1:8338'] msg = { 'command': 'init', 'data': { 'groups': groups } } seeds = [] address = '127.0.0.2:14922' client = HerdClient(seed=random.choice(managers)) response = client.request(msg) if response and len(response['data']['groups']): for item in response['data']['groups']: for value in item.values(): for ip in value: if ip not in seeds and address != ip: seeds.append(ip) cluster = HerdCluster(seeds=seeds, address=address, groups=groups, ping_time=5000, managers=managers) cluster.start()