class TestSequenceFinished(unittest.TestCase):
    def setUp(self):
        self.sim = Simulator()
        self.m0 = Message(self.sim, 1, 'han0', 'cmd')
        self.m1 = Message(self.sim, 2, 'han1', 'cmd')
        self.m2 = Message(self.sim, 3, 'han2', 'cmd', preqs=[self.m0, self.m1])
        self.adj_list = {
            self.m0: [self.m2],
            self.m1: [self.m2],
            self.m2: []
        }
        self.graph = DirectedGraph()
        self.graph.from_adjacency_list(self.adj_list)
        self.seq = Sequence(self.graph)
        self.m0.state = Message.DONE
        self.m1.state = Message.DONE
        self.m2.state = Message.DONE

    def test_frontier_at_finish(self):
        self.assertEqual(
            sorted(self.seq.find_frontier()), [])

    def test_is_finished(self):
        a = self.seq.is_finished()
        self.assertEqual(True, a)

    def test_update(self):
        self.seq.update()
        self.assertEqual(self.seq.find_frontier(), [])
Exemple #2
0
class TestDirectedGraphBuild(unittest.TestCase):
    def setUp(self):
        self.graph = DirectedGraph()
        self.adj_list = {'a': ['c'], 'b': ['c'], 'c': []}

    def test_adjacency_building(self):
        self.graph.from_adjacency_list(self.adj_list)
        self.assertEqual(self.graph.neighbors_for_vertex('a'), ['c'])
Exemple #3
0
class TestDirectedGraphNeighbors(unittest.TestCase):
    def setUp(self):
        self.graph = DirectedGraph()
        self.adj_list = {'a': ['c'], 'b': ['c'], 'c': []}
        self.graph.from_adjacency_list(self.adj_list)

    def test_parents_for_vertex(self):
        self.assertEqual([
            self.graph.vertex_at(v) for v in self.graph.parents_for_vertex('c')
        ], ['a', 'b'])

    def test_find_sources(self):
        self.assertEqual(self.graph.find_sources(), ['a', 'b'])
Exemple #4
0
class TestSequenceMemory(unittest.TestCase):
    def setUp(self):
        self.sim = Simulator()
        self.m0 = Message(self.sim, 1, 'han0', 'cmd')
        self.m1 = Message(self.sim, 2, 'han1', 'cmd')
        self.m2 = Message(self.sim, 3, 'han2', 'cmd', preqs=[self.m0, self.m1])
        self.adj_list = {
            self.m0: [self.m2],
            self.m1: [self.m2],
            self.m2: []
        }
        self.graph = DirectedGraph()
        self.graph.from_adjacency_list(self.adj_list)
        self.seq = Sequence(self.graph)
        self.sequence_memory = SequenceMemory()

    def test_add_sequence(self):
        self.sequence_memory.add_sequence(self.seq)
        self.assertEqual(True, self.seq in self.sequence_memory.sequence_finder.values())
        with self.assertRaises(KeyError):
            self.sequence_memory.add_sequence(self.seq)

    def test_find_sequence_key(self):
        self.sequence_memory.add_sequence(self.seq)
        self.assertEqual(self.sequence_memory.get_sequence_key(self.seq), 0)
    
    def test_get_sequence(self):
        self.sequence_memory.add_sequence(self.seq)
        self.assertEqual(self.seq, self.sequence_memory.get_sequence(self.seq))

    def test_remove_sequence(self):
        self.sequence_memory.add_sequence(self.seq)
        self.sequence_memory.remove_sequence(self.seq)
        self.assertEqual(self.sequence_memory.sequence_finder, {})
        self.assertEqual(False, self.sequence_memory.exists_message(self.m0))
        with self.assertRaises(KeyError):
            self.sequence_memory.remove_sequence(self.seq)

    def test_exists_message(self):
        self.sequence_memory.add_sequence(self.seq)
        self.assertEqual(True, self.sequence_memory.exists_message(self.m0))

    def test_get_sequence_by_message(self):
        self.sequence_memory.add_sequence(self.seq)
        self.assertEqual(
            self.seq, self.sequence_memory.get_sequence_by_message(self.m0))
class TestSequenceEnding(unittest.TestCase):
    def setUp(self):
        self.sim = Simulator()
        self.m0 = Message(self.sim, 1, 'han0', 'cmd')
        self.m1 = Message(self.sim, 2, 'han1', 'cmd')
        self.m2 = Message(self.sim, 3, 'han2', 'cmd', preqs=[self.m0, self.m1])
        self.adj_list = {
            self.m0: [self.m2],
            self.m1: [self.m2],
            self.m2: []
        }
        self.graph = DirectedGraph()
        self.graph.from_adjacency_list(self.adj_list)
        self.seq = Sequence(self.graph)
        self.m0.state = Message.DONE
        self.m1.state = Message.DONE

    def test_get_reachable(self):
        self.assertListEqual(self.seq.get_reachable(self.m0), [self.m2])

    def test_frontier_at_ending(self):
        self.assertEqual(
            sorted(self.seq.find_frontier()), 
            sorted([self.m2]))

    def test_is_finished(self):
        a = self.seq.is_finished()
        self.assertEqual(False, a)

    def test_update(self):
        self.seq.update()
        self.assertEqual(
            sorted(self.seq.find_frontier()), 
            sorted([self.m2]))
        self.assertEqual(self.sim.queue.queue.queue, [self.m2])
        self.assertEqual(self.m2.state, Message.READY)
class TestBasicAggregation(unittest.TestCase):
    def setUp(self):
        self.sim = Simulator()
        self.agg = MessageAggregator()
        self.m0 = Message(self.sim, 1, 'han0', 'cmd')
        self.m1 = Message(self.sim, 2, 'han1', 'cmd')
        self.m2 = Message(self.sim, 3, 'han2', 'cmd', preqs=[self.m0, self.m1])
        self.adj_list = {self.m0: [self.m2], self.m1: [self.m2], self.m2: []}
        self.graph = DirectedGraph()
        self.graph.from_adjacency_list(self.adj_list)
        self.seq = Sequence(self.graph)
        self.m_alone_pending = Message(self.sim, 4, 'han_alone', 'cmd')
        self.m_alone_ready = Message(self.sim, 5, 'han_far_away', 'cmd')
        self.m_alone_ready.state = Message.READY
        self.m_alone_done = Message(self.sim, 5, 'han_far_away', 'cmd')
        self.m_alone_done.state = Message.DONE

    def test_handle_unknown_pending_messages(self):
        self.agg.handle(self.m_alone_pending)
        self.assertEqual(self.sim.queue.queue.queue[0], self.m_alone_pending)

    def test_handle_unknown_ready_messages(self):
        with self.assertRaises(RuntimeError):
            self.agg.handle(self.m_alone_ready)

    def test_handle_unknown_done_messages(self):
        with self.assertRaises(RuntimeError):
            self.agg.handle(self.m_alone_done)

    def test_handle_known_pending_messages(self):
        self.agg.handle(self.seq)
        self.m0.state = Message.PENDING
        with self.assertRaises(RuntimeError):
            self.agg.handle(self.m0)

    def test_handle_known_ready_messages(self):
        self.agg.handle(self.seq)
        self.m0.state = Message.READY
        with self.assertRaises(RuntimeError):
            self.agg.handle(self.m0)

    def test_handle_known_done_messages(self):
        self.agg.handle(self.seq)
        self.m0.state = Message.DONE
        self.agg.handle(self.m0)
        self.m1.state = Message.DONE
        self.agg.handle(self.m1)
        self.assertEqual(self.m2.state, Message.READY)

    def test_know_if_sequence_finished(self):
        self.agg.handle(self.seq)
        self.m0.state = Message.DONE
        self.m1.state = Message.DONE
        self.m2.state = Message.DONE
        self.agg.handle(self.m2)
        self.assertEqual({}, self.agg.memory.message_finder)
        self.assertEqual({}, self.agg.memory.sequence_finder)
        self.assertEqual({}, self.agg.memory.message_to_sequence)

    def test_handle_sequences(self):
        self.agg.handle(self.seq)
        self.assertEqual(self.agg.memory.sequence_finder[0], self.seq)
        self.assertListEqual(sorted([self.m0, self.m1]),
                             sorted(self.sim.queue.queue.queue))

    def test_error_double_sequence(self):
        with self.assertRaises(KeyError):
            self.agg.handle(self.seq)
            self.agg.handle(self.seq)