# Path: tests/mocks/client.py
# def use_mock_client(binder):
#     binder.gets  = {}
#     binder.posts = {}
#     binder.lastest_post = None
#     return binder
# 
# Path: tests/mocks/client.py
# def mock_client(binder):
#     class MockClient():
#         def __init__(self, *args):
#             self.binder = binder
# 
#         @property
#         def url(self):
#             return "http://test"
# 
#         @property
#         def port(self):
#             return "5000"
# 
#         def get(self, resource, default=None):
#             if resource in self.binder.gets:
#                 self.binder.gets[resource] += 1
#             else:
#                 self.binder.gets[resource] = 1
#             return default
# 
#         def post(self, resource, json, default=None):
#             if resource in self.binder.posts:
#                 self.binder.posts[resource] += 1
#             else:
#                 self.binder.posts[resource] = 1
#             self.binder.lastest_post = json
#             return default
# 
#     return MockClient
# 
# Path: dissemination/master.py
# class Master():
#     def __init__(self):
#         self.membership_list = []
# 
#         self.server = Server("master", MASTER_DEFAULT_PORT)
#         self.server.add_component_post("/register", MasterReceive(self))
# 
#     def register(self, registration, client_cls=Client):
#         """
#         Register slave in membership list.
#         """
#         logger.info("Received register {}:{}".format(
#             registration["ip"],
#             registration["port"]
#         ))
# 
#         # The servers are single threaded, one-connection at a time
#         client = client_cls("http://" + registration["ip"], registration["port"])
#         self.membership_list.append(client)
# 
#     def broadcast(self):
#         """
#         Create a JSON with information about each client, i.e. slave
#         in the membership list and send the broadcast data to all
#         other slaves.
#         """
#         logger.info(colored.yellow("Broadcasting membership...."))
#         broadcast = {
#             "members" : [{
#                 "ip" : client.url.split("/")[2],
#                 "port" : client.port,
#             } for client in self.membership_list]
#         }
# 
#         # Send the broadcast to each client
#         for client in self.membership_list:
#             client.get("/healty")
#             client.post("/membership", broadcast)


from unittest import TestCase
from tests.mocks.client import use_mock_client
from tests.mocks.client import mock_client
from dissemination.master import Master
from __future__ import absolute_import



class MasterTest(TestCase):
    def setUp(self):
        use_mock_client(self)

        self.master = Master()

        self.ips = [{
            "ip" : ip,
            "port" : "5000"
        } for ip in ["10.1.1.1", "10.1.1.2", "10.1.1.3", "10.1.1.4"]]

    def set_membership_list(self):
        """Add mock clients to the membership list of the master."""
        for ip in self.ips:
            self.master.register(ip, client_cls=mock_client(self))

    def test_register_updates_membership_list(self):
        """Correct registration of membership list slave."""
        list_len = 0
        self.assertEqual(len(self.master.membership_list), 0)
        for ip in self.ips:
            list_len += 1

# Path: tests/mocks/client.py
# def use_mock_client(binder):
#     binder.gets  = {}
#     binder.posts = {}
#     binder.lastest_post = None
#     return binder
# 
# Path: tests/mocks/client.py
# def mock_client(binder):
#     class MockClient():
#         def __init__(self, *args):
#             self.binder = binder
# 
#         @property
#         def url(self):
#             return "http://test"
# 
#         @property
#         def port(self):
#             return "5000"
# 
#         def get(self, resource, default=None):
#             if resource in self.binder.gets:
#                 self.binder.gets[resource] += 1
#             else:
#                 self.binder.gets[resource] = 1
#             return default
# 
#         def post(self, resource, json, default=None):
#             if resource in self.binder.posts:
#                 self.binder.posts[resource] += 1
#             else:
#                 self.binder.posts[resource] = 1
#             self.binder.lastest_post = json
#             return default
# 
#     return MockClient
# 
# Path: dissemination/master.py
# class Master():
#     def __init__(self):
#         self.membership_list = []
# 
#         self.server = Server("master", MASTER_DEFAULT_PORT)
#         self.server.add_component_post("/register", MasterReceive(self))
# 
#     def register(self, registration, client_cls=Client):
#         """
#         Register slave in membership list.
#         """
#         logger.info("Received register {}:{}".format(
#             registration["ip"],
#             registration["port"]
#         ))
# 
#         # The servers are single threaded, one-connection at a time
#         client = client_cls("http://" + registration["ip"], registration["port"])
#         self.membership_list.append(client)
# 
#     def broadcast(self):
#         """
#         Create a JSON with information about each client, i.e. slave
#         in the membership list and send the broadcast data to all
#         other slaves.
#         """
#         logger.info(colored.yellow("Broadcasting membership...."))
#         broadcast = {
#             "members" : [{
#                 "ip" : client.url.split("/")[2],
#                 "port" : client.port,
#             } for client in self.membership_list]
#         }
# 
#         # Send the broadcast to each client
#         for client in self.membership_list:
#             client.get("/healty")
#             client.post("/membership", broadcast)


# Path: tests/dissemination/test_master.py
from unittest import TestCase
from tests.mocks.client import use_mock_client
from tests.mocks.client import mock_client
from dissemination.master import Master

from __future__ import absolute_import



class MasterTest(TestCase):
    def setUp(self):
        use_mock_client(self)

        self.master = Master()

        self.ips = [{
            "ip" : ip,
            "port" : "5000"
        } for ip in ["10.1.1.1", "10.1.1.2", "10.1.1.3", "10.1.1.4"]]

    def set_membership_list(self):
        """Add mock clients to the membership list of the master."""
        for ip in self.ips:
            self.master.register(ip, client_cls=mock_client(self))

    def test_register_updates_membership_list(self):
        """Correct registration of membership list slave."""
        list_len = 0
        self.assertEqual(len(self.master.membership_list), 0)
        for ip in self.ips:
            list_len += 1

            self.master.register(ip, client_cls=mock_client(self))