Example #1
0
    def test_init(self):
        unit_id = uuid.uuid4().hex
        destination = uuid.uuid4().hex
        neighbour = uuid.uuid4().hex

        # If core properties are missing a ValidationError should be raised
        self.assertRaises(ValidationError, lambda: Task(unit_id))

        # Create a new task with core properties (a task_id
        # is not needed as Task should create one if it is missing).
        # This is the minimum set of variables required to create a new task.
        task = Task(unit_id,
                    from_unit=unit_id,
                    to_unit=destination,
                    command={"announce": {
                        "fallback_ids": [neighbour]
                    }})

        self.assertTrue(task.task_id is not None)
        self.assertTrue(task.response == {})

        # Create an "existing" task with a task_id
        task_id = uuid.uuid4().hex
        task = Task(unit_id,
                    task_id=task_id,
                    from_unit=unit_id,
                    to_unit=destination,
                    command={"announce": {
                        "fallback_ids": [neighbour]
                    }})

        self.assertTrue(task.task_id == task_id)
    def test_listen_for_response(self):
        taskboard = Taskboard(uuid.uuid4().hex)
        
        command = {"memory":{}}
        id = uuid.uuid4().hex
        to = uuid.uuid4().hex
        
        # Create a task
        #task = taskboard.request(to, command)
        task = Task(id, from_unit = id, to_unit = to, command = command)
        #taskboard.add(task)
        
        print "posted task", task.json()
        

        # Should be no response.
        self.assertTrue(task.response == {})
        self.assertTrue(task.board == 'Backlog')
        
               
        response = {"test":"test"}
        task.add_response(response = response)
        
        #taskboard.respond(task)
        
        self.assertTrue(task.response <> {})
        self.assertTrue(task.board == 'Backlog')
Example #3
0
    def test_listen_for_response(self):
        taskboard = Taskboard(uuid.uuid4().hex)

        command = {"memory": {}}
        id = uuid.uuid4().hex
        to = uuid.uuid4().hex

        # Create a task
        #task = taskboard.request(to, command)
        task = Task(id, from_unit=id, to_unit=to, command=command)
        #taskboard.add(task)

        print "posted task", task.json()

        # Should be no response.
        self.assertTrue(task.response == {})
        self.assertTrue(task.board == 'Backlog')

        response = {"test": "test"}
        task.add_response(response=response)

        #taskboard.respond(task)

        self.assertTrue(task.response <> {})
        self.assertTrue(task.board == 'Backlog')
Example #4
0
    def test_chronicle(self):

        # Create a task that requests configuration from input unit
        id0 = uuid.uuid4().hex
        id1 = uuid.uuid4().hex
        id2 = uuid.uuid4().hex
        taskboard = Taskboard(id1)
        command = {"announce": {}}
        chronicle = [{'time_ms': 0.0, 'unit_id': id0, 'hop': 1}]

        task = Task(unit_id=id0,
                    chronicle=chronicle,
                    from_unit=id1,
                    to_unit=id2,
                    command=command)
        taskboard.add(task)
        self.assertTrue(len(task.chronicle) == 1)

        c = Chronicle(unit_id=id1, chronicle=task.chronicle)
        c.update()

        task.chronicle = c.json()
        self.assertTrue(len(task.chronicle) == 2)

        c = Chronicle(unit_id=id2, chronicle=task.chronicle)
        c.update()

        self.assertTrue(len(task.chronicle) == 3)

        print "task.chronicle", c.chronicle

        # Test last
        self.assertEquals(c.last(), id2)
        # Test next
        self.assertEquals(c.next(id1), id2)
        self.assertEquals(c.next(id0), id1)
        # Test previous
        self.assertEquals(c.previous(id1), id0)
        self.assertEquals(c.previous(id2), id1)

        # Test error condition

        self.assertRaises(LookupError, lambda: c.next(id2))

        self.assertRaises(LookupError, lambda: c.previous(id0))
 def test_json(self):
     
     unit_id = uuid.uuid4().hex
     destination = uuid.uuid4().hex
     neighbour = uuid.uuid4().hex
     task_id = uuid.uuid4().hex
     
     task = Task(unit_id, 
                 task_id = task_id,
                 from_unit = unit_id, 
                 to_unit = destination, 
                 command = {"announce":{"fallback_ids":[neighbour]}})
     
     json_result = task.json()
     
     self.assertTrue(json_result['from_unit'] == unit_id)
     self.assertTrue(json_result['to_unit'] == destination)
     self.assertTrue(json_result['command'] == {"announce":{"fallback_ids":[neighbour]}})
     self.assertTrue(json_result['response'] == {})
 def test_chronicle(self):
         
     # Create a task that requests configuration from input unit
     id0 = uuid.uuid4().hex 
     id1 = uuid.uuid4().hex
     id2 = uuid.uuid4().hex
     taskboard = Taskboard(id1)
     command = {"announce":{}}
     chronicle = [{'time_ms': 0.0, 'unit_id': id0, 'hop': 1}]
                
     task = Task(unit_id = id0, chronicle = chronicle, from_unit = id1, to_unit = id2, command = command)     
     taskboard.add(task)  
     self.assertTrue(len(task.chronicle)==1)
     
     c = Chronicle(unit_id = id1, chronicle = task.chronicle)
     c.update()
     
     task.chronicle = c.json()      
     self.assertTrue(len(task.chronicle)==2)
     
     c = Chronicle(unit_id = id2, chronicle = task.chronicle)
     c.update()
     
     self.assertTrue(len(task.chronicle)==3)
     
     print "task.chronicle", c.chronicle
      
     # Test last
     self.assertEquals(c.last(), id2)
     # Test next
     self.assertEquals(c.next(id1), id2)
     self.assertEquals(c.next(id0), id1)
     # Test previous
     self.assertEquals(c.previous(id1), id0)
     self.assertEquals(c.previous(id2), id1)
     
     # Test error condition
     
     self.assertRaises(LookupError, lambda: c.next(id2))
     
     self.assertRaises(LookupError, lambda: c.previous(id0))
Example #7
0
    def test_taskboard(self):
        # Taskboard - Add and remove a task
        from_id = uuid.uuid4().hex
        to_id = uuid.uuid4().hex
        taskboard = Taskboard(from_id)
        task = Task(unit_id=from_id, from_unit=from_id, to_unit=to_id)
        expected_task_id = task.task_id
        taskboard.add(task)
        task = taskboard.find_task(task.task_id)
        # Expect to find the task we have just added
        self.assertEquals(expected_task_id, task.task_id)

        taskboard.remove(task)
        self.assertRaises(LookupError, taskboard.find_task, expected_task_id)
Example #8
0
    def test_json(self):

        unit_id = uuid.uuid4().hex
        destination = uuid.uuid4().hex
        neighbour = uuid.uuid4().hex
        task_id = uuid.uuid4().hex

        task = Task(unit_id,
                    task_id=task_id,
                    from_unit=unit_id,
                    to_unit=destination,
                    command={"announce": {
                        "fallback_ids": [neighbour]
                    }})

        json_result = task.json()

        self.assertTrue(json_result['from_unit'] == unit_id)
        self.assertTrue(json_result['to_unit'] == destination)
        self.assertTrue(json_result['command'] ==
                        {"announce": {
                            "fallback_ids": [neighbour]
                        }})
        self.assertTrue(json_result['response'] == {})
Example #9
0
    def test_add(self):
        # Taskboard - Add and remove a task
        id = uuid.uuid4().hex
        taskboard = Taskboard(id)
        task = Task(unit_id=id,
                    from_unit=uuid.uuid4().hex,
                    to_unit=uuid.uuid4().hex)
        task_id = task.task_id
        taskboard.add(task)
        search_task = taskboard.find_task(task.task_id)
        # Expect to find the task we have just added
        self.assertEquals(task_id, search_task.task_id)

        taskboard.remove(task)
        self.assertRaises(LookupError, lambda: taskboard.find_task(task_id))
Example #10
0
    def test_command_setting(self):
        inputunit = configure_unit(unit_setup=unit_core.GenericUnit,
                                   input_ids=[],
                                   update_cycle=0,
                                   description="Input unit - clock")
        print "INPUTUNIT.unit_id", inputunit.unit_id
        processunit = configure_unit(unit_setup=unit_core.PassThruUnit,
                                     input_ids=[inputunit.unit_id],
                                     update_cycle=0,
                                     description="PassThruUnit unit")
        print "PROCESSUNIT.unit_id", processunit.unit_id
        # Process announces

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        # Create a task that requests configuration from input unit
        id = uuid.uuid4().hex
        taskboard = Taskboard(id)
        command = {
            "setting": {
                "common": {
                    "configurable": {
                        "update_cycle": 9999
                    }
                }
            }
        }
        task = Task(unit_id=processunit.unit_id,
                    from_unit=processunit.unit_id,
                    to_unit=inputunit.unit_id,
                    command=command)
        task_id = task.task_id
        taskboard.add(task)

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        # Should be no memory initially on inputunit
        self.assertEquals(
            inputunit.configuration.unit_config["common"]["configurable"]
            ["update_cycle"], 9999)
Example #11
0
    def test_request(self):
        from_unit = uuid.uuid4().hex
        taskboard = Taskboard(uuid.uuid4().hex)

        command = {"test": "test"}
        to = uuid.uuid4().hex

        task = Task(unit_id=from_unit,
                    command=command,
                    to_unit=to,
                    from_unit=from_unit)

        # Pass a dict object to to taskboard.request
        taskboard.request(to, command)

        print "task_id", taskboard.tasks[0].task_id

        r = taskboard_interface.get_task(taskboard.tasks[0].task_id)
        print r
        t = r['task_id']
        print t
        self.assertEquals(t, taskboard.tasks[0].task_id)
Example #12
0
    def test_command_configuration(self):
        # Test sending a configuration request, and confirming the unit responds
        # with current configuration.
        inputunit = configure_unit(unit_setup=unit_core.GenericUnit,
                                   input_ids=[],
                                   update_cycle=0,
                                   description="Test string af31")
        print "INPUTUNIT.unit_id", inputunit.unit_id
        processunit = configure_unit(unit_setup=unit_core.PassThruUnit,
                                     input_ids=[inputunit.unit_id],
                                     update_cycle=0,
                                     description="PassThruUnit unit")
        print "PROCESSUNIT.unit_id", processunit.unit_id
        # Process announces

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        # Create a task that requests configuration
        id = uuid.uuid4().hex
        taskboard = Taskboard(id)
        command = {"configuration": "Null"}

        task = Task(unit_id=processunit.unit_id,
                    from_unit=processunit.unit_id,
                    to_unit=inputunit.unit_id,
                    command=command)
        task_id = task.task_id
        taskboard.add(task)

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        # Should be no memory initially on inputunit
        self.assertEquals(
            inputunit.configuration.unit_config["common"]["non_configurable"]
            ["description"], "Test string af31")
Example #13
0
    def test_command_announce_a(self):
        inputunit = configure_unit(unit_setup=unit_core.GenericUnit,
                                   input_ids=[],
                                   update_cycle=0,
                                   description="Input unit - clock")
        print "INPUTUNIT.unit_id", inputunit.unit_id
        processunit = configure_unit(unit_setup=unit_core.PassThruUnit,
                                     input_ids=[inputunit.unit_id],
                                     update_cycle=0,
                                     description="PassThruUnit unit")
        print "PROCESSUNIT.unit_id", processunit.unit_id
        # Process announces

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        # Create a task that requests configuration from input unit
        id = uuid.uuid4().hex
        taskboard = Taskboard(id)
        command = {"announce": {}}
        task = Task(unit_id=processunit.unit_id,
                    from_unit=processunit.unit_id,
                    to_unit=inputunit.unit_id,
                    command=command)
        task_id = task.task_id
        taskboard.add(task)

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        print "task.response", task.response
        inputunit.taskboard.debug()
        processunit.taskboard.debug()

        self.assertTrue(False)
def network_simulator():
    G = nx.Graph()
    nodes = []
    node_ids = []
    for _ in xrange(10):
        node_id = uuid.uuid4().hex
        node = configure_unit(unit_id=node_id,
                              unit_setup=unit_core.RandomUnit,
                              input_ids=[],
                              update_cycle=0,
                              description="Simulated unit",
                              neighbours=[])
        #G.add_node(node.unit_id, unit_id = node.unit_id[:4])
        G.add_node(node, unit_id=node.unit_id[:4])
        nodes.append(node)
        node_ids.append(node.unit_id)

    # Assign random neighbours to each node
    neighbour_dict = {}
    for node in nodes:
        ip = "127.0.0.1"
        port = 8080

        for _ in xrange(2):

            random_neighbour = random.choice(nodes)

            node.router.add_neighbour(ip, port, random_neighbour.unit_id)
            random_neighbour.router.add_neighbour(ip, port, node.unit_id)

            G.add_edge(node, random_neighbour)

        neighbour_list = []
        for neighbour in node.router.neighbours:
            print neighbour.unit_id, node.unit_id
            assert (neighbour.unit_id != node.unit_id)
            neighbour_list.append(neighbour.unit_id)
        neighbour_dict[node.unit_id] = neighbour_list

    setup_server(nodes)

    num_tasks = 0

    starttime = time.time()

    while (time.time() - starttime) < 90:

        allowable_commands = [{"output": {}}, {"output": {}}]
        #allowable_commands = ({"announce":{}},
        #                      {"announce":{}}
        #                      )

        from_unit = random.choice(nodes)

        allowable_nodes = []

        for node in nodes:
            if node.unit_id == from_unit.unit_id:
                pass
            else:
                print node.unit_id, node.unit_id not in neighbour_dict[
                    from_unit.unit_id]
                if node.unit_id not in neighbour_dict[from_unit.unit_id]:
                    allowable_nodes.append(node)

        to_unit = random.choice(allowable_nodes)

        num_tasks = num_tasks + 1
        task = Task(from_unit.unit_id,
                    to_unit=to_unit.unit_id,
                    from_unit=from_unit.unit_id,
                    command=random.choice(allowable_commands))

        assert (from_unit.unit_id != to_unit.unit_id)

        from_unit.taskboard.queue(task)
        #from_unit.router.transmit.queue(task)
        #from_unit.taskboard.add_taskset([task])

        #node = random.choice(nodes)
        #node._new()

        for node in nodes:
            node.controller_update()

    # Simulation complete
    # Assess effeciency
    tasks = []
    failed = 0
    success = 0
    for node in nodes:
        node.taskboard.debug(onlyerrors=True)
        for task in node.taskboard.removed_tasks:
            command = task.command.keys()[0]
            #print command

            if command == 'output' and node.unit_id == task.from_unit:
                if task.isResponse() == False:
                    failed = failed + 1
                if task.isResponse() == True:
                    success = success + 1
                    task.debug()
                tasks.append(task)

        node.taskboard.debug()
        node.router.destination_goodness_table()
    #for node in nodes:
    #    node.taskboard.debug()
    print "tasks created", num_tasks
    print "success", success
    print "failed", failed

    print G.nodes()
    print G.edges()

    labels = dict((n, d['unit_id']) for n, d in G.nodes(data=True))
    print labels
    nx.draw(G,
            with_labels=True,
            node_color='white',
            labels=labels,
            node_size=1000)
    #nx.draw_networkx_labels(G, labels=labels)
    plt.show()
    def test_add_response(self):
        # Create an "existing" task with a task_id
        # should create a task with this uuid as a reference.
        unit_id = uuid.uuid4().hex
        destination = uuid.uuid4().hex
        neighbour = uuid.uuid4().hex
        task_id = uuid.uuid4().hex
        task = Task(unit_id, 
                    task_id = task_id,
                    from_unit = unit_id, 
                    to_unit = destination, 
                    command = {"announce":{"fallback_ids":[neighbour]}})
    
        self.assertTrue(task.task_id == task_id)
        
        # Try to write over existing task with same task
        # should be accepted as this has no adverse effect.
        task.add_response(task_id = task_id,
                          from_unit = unit_id, 
                          to_unit = destination, 
                          command = {"announce":{"fallback_ids":[neighbour]}})
        
        new_unit_id = uuid.uuid4().hex
        self.assertRaises(ValidationError, lambda: task.add_response(task_id = task_id,
                                                                     from_unit = new_unit_id, 
                                                                     to_unit = destination, 
                                                                     command = {"announce":{"fallback_ids":[neighbour]}}))
        
        # Try to write over existing task with same task
        # should be accepted without raises an exception
        # as this has no adverse effect.
        task.add_response(task_id = task_id,
                          from_unit = unit_id, 
                          to_unit = destination, 
                          command = {"announce":{"fallback_ids":[neighbour]}})
        
        # Try adding only a response should be accepted.
        task.add_response(response = {"this is a response":"yes"})
        self.assertTrue(task.response == {"this is a response":"yes"})

        # Trying to add a response with another mismatching key should raise
        # a ValidationError
        task.add_response(task_id = task_id,
                          from_unit = unit_id, 
                          to_unit = destination, 
                          command = {"announce":{"fallback_ids":[neighbour]}})
        
        self.assertRaises(ValidationError, lambda: task.add_response(command = {"output":{"this is a different command"}}, 
                                                                     response = {"this is a response":"yes"}))
def network_simulator():
    G = nx.Graph()
    nodes = []
    node_ids = []
    for _ in xrange(10):
        node_id = uuid.uuid4().hex
        node = configure_unit(unit_id = node_id, 
                               unit_setup = unit_core.RandomUnit, 
                               input_ids = [], 
                               update_cycle = 0, 
                               description = "Simulated unit", 
                               neighbours=[])
        #G.add_node(node.unit_id, unit_id = node.unit_id[:4])
        G.add_node(node, unit_id = node.unit_id[:4])
        nodes.append(node)
        node_ids.append(node.unit_id)
    
    # Assign random neighbours to each node
    neighbour_dict ={}
    for node in nodes:
        ip = "127.0.0.1"
        port = 8080
        
        for _ in xrange(2):

            random_neighbour = random.choice(nodes)
            
            node.router.add_neighbour(ip, port, random_neighbour.unit_id)
            random_neighbour.router.add_neighbour(ip, port, node.unit_id)

            G.add_edge(node, random_neighbour)
            
        neighbour_list = []    
        for neighbour in node.router.neighbours:
            print neighbour.unit_id, node.unit_id
            assert(neighbour.unit_id != node.unit_id)
            neighbour_list.append(neighbour.unit_id)
        neighbour_dict[node.unit_id] = neighbour_list
        
    setup_server(nodes)
    

    num_tasks = 0

    starttime =time.time()
    
    
    while (time.time() - starttime) < 90:  
        
        allowable_commands = [{"output":{}}, {"output":{}}]
        #allowable_commands = ({"announce":{}},
        #                      {"announce":{}}
        #                      )
        
        
        from_unit = random.choice(nodes)
        
        allowable_nodes= []

        
        for node in nodes:
            if node.unit_id == from_unit.unit_id:
                pass
            else:
                print node.unit_id, node.unit_id not in neighbour_dict[from_unit.unit_id]
                if node.unit_id not in neighbour_dict[from_unit.unit_id]:
                    allowable_nodes.append(node)
        
        to_unit = random.choice(allowable_nodes)
                    
        num_tasks = num_tasks +1
        task = Task(from_unit.unit_id, 
                    to_unit = to_unit.unit_id, 
                    from_unit = from_unit.unit_id,
                    command = random.choice(allowable_commands))   
   
        assert(from_unit.unit_id != to_unit.unit_id)
        
        from_unit.taskboard.queue(task)              
        #from_unit.router.transmit.queue(task)
        #from_unit.taskboard.add_taskset([task])  
        
        #node = random.choice(nodes)
        #node._new()
        
        for node in nodes:      
            node.controller_update()  
    
    # Simulation complete
    # Assess effeciency
    tasks = []
    failed = 0
    success = 0
    for node in nodes:
        node.taskboard.debug(onlyerrors = True)
        for task in node.taskboard.removed_tasks:
            command = task.command.keys()[0]
            #print command
            
            if command == 'output' and node.unit_id == task.from_unit:
                if task.isResponse() == False:
                    failed = failed + 1
                if task.isResponse() == True:
                    success = success +1
                    task.debug()
                tasks.append(task)
        
        node.taskboard.debug()        
        node.router.destination_goodness_table()
    #for node in nodes:
    #    node.taskboard.debug()
    print "tasks created", num_tasks
    print "success", success
    print "failed", failed
    
    print G.nodes()
    print G.edges()
    
    
    labels=dict((n,d['unit_id']) for n,d in G.nodes(data=True))
    print labels
    nx.draw(G, with_labels = True, node_color='white', labels=labels, node_size =1000)
    #nx.draw_networkx_labels(G, labels=labels)
    plt.show()
Example #17
0
    def test_add_response(self):
        # Create an "existing" task with a task_id
        # should create a task with this uuid as a reference.
        unit_id = uuid.uuid4().hex
        destination = uuid.uuid4().hex
        neighbour = uuid.uuid4().hex
        task_id = uuid.uuid4().hex
        task = Task(unit_id,
                    task_id=task_id,
                    from_unit=unit_id,
                    to_unit=destination,
                    command={"announce": {
                        "fallback_ids": [neighbour]
                    }})

        self.assertTrue(task.task_id == task_id)

        # Try to write over existing task with same task
        # should be accepted as this has no adverse effect.
        task.add_response(task_id=task_id,
                          from_unit=unit_id,
                          to_unit=destination,
                          command={"announce": {
                              "fallback_ids": [neighbour]
                          }})

        new_unit_id = uuid.uuid4().hex
        self.assertRaises(
            ValidationError, lambda: task.add_response(
                task_id=task_id,
                from_unit=new_unit_id,
                to_unit=destination,
                command={"announce": {
                    "fallback_ids": [neighbour]
                }}))

        # Try to write over existing task with same task
        # should be accepted without raises an exception
        # as this has no adverse effect.
        task.add_response(task_id=task_id,
                          from_unit=unit_id,
                          to_unit=destination,
                          command={"announce": {
                              "fallback_ids": [neighbour]
                          }})

        # Try adding only a response should be accepted.
        task.add_response(response={"this is a response": "yes"})
        self.assertTrue(task.response == {"this is a response": "yes"})

        # Trying to add a response with another mismatching key should raise
        # a ValidationError
        task.add_response(task_id=task_id,
                          from_unit=unit_id,
                          to_unit=destination,
                          command={"announce": {
                              "fallback_ids": [neighbour]
                          }})

        self.assertRaises(
            ValidationError, lambda: task.add_response(
                command={"output": {"this is a different command"}},
                response={"this is a response": "yes"}))
Example #18
0
    def test_command_memory(self):
        # Test sending memory and replacing contents of
        # existing memory

        # Set update cycle to 10,000 to avoid generating data.
        inputunit = configure_unit(unit_setup=unit_core.GenericUnit,
                                   input_ids=[],
                                   update_cycle=0,
                                   description="Test string af31")
        print "INPUTUNIT.unit_id", inputunit.unit_id
        processunit = configure_unit(unit_setup=unit_core.PassThruUnit,
                                     input_ids=[inputunit.unit_id],
                                     update_cycle=0,
                                     description="PassThruUnit unit")
        print "PROCESSUNIT.unit_id", processunit.unit_id
        # Process announces

        for __ in xrange(5):
            inputunit.update()
            processunit.update()

        # Test that empty strings can be sent/
        id = uuid.uuid4().hex
        taskboard = Taskboard(id)
        command = {"memory": {'forecast': [], 'history': []}}

        task = Task(unit_id=processunit.unit_id,
                    from_unit=processunit.unit_id,
                    to_unit=inputunit.unit_id,
                    command=command)
        taskboard.add(task)

        for __ in xrange(1):
            inputunit.update()
            processunit.update()

        print "forecast"
        for i in inputunit.memory.forecast:
            print i.data
        print "history"
        for i in inputunit.memory.history:
            print i.data

        self.assertRaises(IndexError,
                          lambda: inputunit.memory.forecast[0].data)
        self.assertRaises(IndexError, lambda: inputunit.memory.history[0].data)
        ''' Test that array of data points can be sent and received '''

        #         print "forecast"
        #         for i in inputunit.memory.forecast:
        #             print i.data
        #         print "history"
        #         for i in inputunit.memory.history:
        #             print i.data

        self.assertRaises(IndexError, lambda: inputunit.memory.history[0].data)
        self.assertRaises(IndexError,
                          lambda: inputunit.memory.forecast[0].data)

        command = {
            "memory": {
                'forecast': [{
                    'time_stamp': 'Sat, 27 Sep 2014 23:32:00 -0000',
                    'data': {
                        'dummy_reading': 707
                    }
                }, {
                    'time_stamp': 'Sat, 27 Sep 2014 23:31:59 -0000',
                    'data': {
                        'dummy_reading': 808
                    }
                }, {
                    'time_stamp': 'Sat, 27 Sep 2014 23:31:57 -0000',
                    'data': {
                        'dummy_reading': 909
                    }
                }],
                'history': [{
                    'time_stamp': 'Sat, 27 Sep 2014 23:32:00 -0000',
                    'data': {
                        'dummy_reading': 404
                    }
                }, {
                    'time_stamp': 'Sat, 27 Sep 2014 23:31:59 -0000',
                    'data': {
                        'dummy_reading': 505
                    }
                }, {
                    'time_stamp': 'Sat, 27 Sep 2014 23:31:57 -0000',
                    'data': {
                        'dummy_reading': 606
                    }
                }]
            }
        }

        task = Task(unit_id=processunit.unit_id,
                    from_unit=processunit.unit_id,
                    to_unit=inputunit.unit_id,
                    command=command)
        taskboard.add(task)

        for __ in xrange(1):
            inputunit.update()
            processunit.update()

        print "forecast"
        for i in inputunit.memory.forecast:
            print i.data
        print "history"
        for i in inputunit.memory.history:
            print i.data
        # Should be no memory initially on inputunit

        self.assertEquals({"dummy_reading": 707},
                          inputunit.memory.forecast[0].data)
        self.assertEquals({"dummy_reading": 404},
                          inputunit.memory.history[0].data)
def test_response_chain():

    possible_commands = ({"output": {}})

    nodea_id = uuid.uuid4().hex
    nodeb_id = uuid.uuid4().hex
    nodec_id = uuid.uuid4().hex
    noded_id = uuid.uuid4().hex
    nodee_id = uuid.uuid4().hex
    nodef_id = uuid.uuid4().hex
    nodeg_id = uuid.uuid4().hex

    node_a = configure_unit(unit_id=nodea_id,
                            unit_setup=unit_core.RandomUnit,
                            input_ids=[],
                            update_cycle=0,
                            description="Random unit",
                            neighbours=[nodeb_id])
    node_b = configure_unit(unit_id=nodeb_id,
                            unit_setup=unit_core.PassThruUnit,
                            input_ids=[nodea_id],
                            update_cycle=0,
                            description="Pass-through unit",
                            neighbours=[nodea_id, nodec_id])
    node_c = configure_unit(unit_id=nodec_id,
                            unit_setup=unit_core.PassThruUnit,
                            input_ids=[nodeb_id],
                            update_cycle=0,
                            description="Output unit",
                            neighbours=[nodeb_id, noded_id])

    node_d = configure_unit(unit_id=noded_id,
                            unit_setup=unit_core.PassThruUnit,
                            input_ids=[nodec_id],
                            update_cycle=0,
                            description="Empty node",
                            neighbours=[nodec_id])

    node_e = configure_unit(unit_id=nodee_id,
                            unit_setup=unit_core.GenericUnit,
                            input_ids=[],
                            update_cycle=0,
                            description="Empty node",
                            neighbours=[])

    node_f = configure_unit(unit_id=nodef_id,
                            unit_setup=unit_core.GenericUnit,
                            input_ids=[],
                            update_cycle=0,
                            description="Empty node",
                            neighbours=[])

    node_g = configure_unit(unit_id=nodeg_id,
                            unit_setup=unit_core.GenericUnit,
                            input_ids=[],
                            update_cycle=0,
                            description="Empty node",
                            neighbours=[])

    setup_server([node_a, node_b, node_c, node_d, node_e, node_f, node_g])

    #for neighbour in inputunit.router.neighbours:
    #    print neighbour.unit_id

    command = random.choice(possible_commands)

    task = Task(node_d.unit_id,
                to_unit=random.choice([node_a.unit_id]),
                from_unit=node_d.unit_id,
                command=command)
    node_d.router.transmit.queue(task)

    node_d.taskboard.add_taskset([task])

    starttime = time.time()
    #while (time.time() - starttime) <1:
    for _ in xrange(15):
        print time.time() - starttime

        #         task = Task(node_a.unit_id,
        #                     to_unit = random.choice([node_b.unit_id,node_c.unit_id,node_d.unit_id,node_e.unit_id,node_f.unit_id,node_g.unit_id]),
        #                     from_unit = node_a.unit_id,
        #                     command = {"output":{}})

        #         task = Task(node_a.unit_id,
        #                     to_unit = random.choice([node_a.unit_id,node_c.unit_id,node_d.unit_id,node_e.unit_id,node_f.unit_id,node_g.unit_id]),
        #                     from_unit = node_a.unit_id,
        #                     command = {"output":{}})
        #         node_b.taskboard.add(task)

        #         task = Task(node_a.unit_id,
        #                     to_unit = random.choice([node_a.unit_id,node_b.unit_id,node_d.unit_id,node_e.unit_id,node_f.unit_id,node_g.unit_id]),
        #                     from_unit = node_a.unit_id,
        #                     command = {"output":{}})
        #         node_c.taskboard.add(task)

        #for _ in xrange(50):
        node_a.controller_update()
        node_b.controller_update()
        node_c.controller_update()
        node_d.controller_update()
        node_e.controller_update()
        node_f.controller_update()
        node_g.controller_update()

        #inputunit.taskboard.debug()
        #print "===INPUTUNIT===", inputunit.unit_id[:4]
        #inputunit.taskboard.debug_tasksets()
        print "NODE A"
        node_a.taskboard.debug()
        node_a.router.received.debug()
        node_a.router.transmit.debug()
        node_a.router.subscriptions.debug()

        print "NODE B"
        node_b.taskboard.debug()
        node_b.router.received.debug()
        node_b.router.transmit.debug()
        node_b.router.subscriptions.debug()

        print "NODE C"
        node_c.taskboard.debug()
        node_c.router.received.debug()
        node_c.router.transmit.debug()
        node_c.router.subscriptions.debug()

        print "NODE D"
        node_d.taskboard.debug()
        node_d.router.received.debug()
        node_d.router.transmit.debug()
        node_d.router.subscriptions.debug()

        node_e.taskboard.debug()
        node_f.taskboard.debug()
        node_g.taskboard.debug()

        node_a.router.destination_goodness_table()
        node_b.router.destination_goodness_table()
        node_c.router.destination_goodness_table()
        node_d.router.destination_goodness_table()
        node_e.router.destination_goodness_table()
        node_f.router.destination_goodness_table()
        node_g.router.destination_goodness_table()


#         print "===PROCESSUNIT===", processunit.unit_id[:4]
#         processunit.taskboard.debug_tasksets()
#         processunit.taskboard.debug()
#         processunit.router.destination_goodness_table()
#
#         print "===OUTPUTUNIT===", outputunit.unit_id[:4]
#         outputunit.taskboard.debug_tasksets()
#         outputunit.taskboard.debug()
#         outputunit.router.destination_goodness_table()

    print "node_a memory"
    for history in node_a.memory.history:
        print history.time_stamp, history.data
    for input in node_a.inputconnector.inputunits:
        input.debug()
    print "node_b memory"
    for history in node_b.memory.history:
        print history.time_stamp, history.data

    for input in node_b.inputconnector.inputunits:
        input.debug()

    print "node_c memory"
    for history in node_c.memory.history:
        print history.time_stamp, history.data
    for input in node_c.inputconnector.inputunits:
        input.debug()
    print "node_d memory"
    for history in node_d.memory.history:
        print history.time_stamp, history.data
    for input in node_d.inputconnector.inputunits:
        input.debug()