示例#1
0
    def test_event_creation(self):
        """Test creating events with different values."""
        evt = Event(name="evt1")
        self.assertEqual(evt.name, "evt1")
        self.assertEqual(evt.sim_id, None)
        self.assertEqual(evt.sim_time, None)
        self.assertEqual(evt.created_by, None)

        evt = Event(name="evt2", sim_id="1234", sim_time=0, created_by="4567")
        self.assertEqual(evt.name, "evt2")
        self.assertEqual(evt.sim_id, "1234")
        self.assertEqual(evt.sim_time, 0)
        self.assertEqual(evt.created_by, "4567")
示例#2
0
    def test_add_old_event(self):
        """Test adding an event before current time."""
        teq = TimeEventQueue()
        teq.add(Event(name="event_5_1", sim_time=5))

        teq.next()
        self.assertEqual(teq._current_time, 5)

        with self.assertRaises(ValueError):  # earlier time
            teq.add(Event(name="event_4_1", sim_time=4))

        with self.assertRaises(ValueError):  # current time.
            teq.add(Event(name="event_5_1", sim_time=5))
示例#3
0
    def test_copy(self):
        """Tests copying an event."""
        evt1 = Event(name="event", sim_time=1, sim_id="123", created_by="456")
        evt1.dynamic1 = "attr1"
        attr = Property(name="attr2",
                        default_value=2,
                        validator=lambda x: x < 10)
        evt1.attr2 = attr

        evt2 = copy.deepcopy(evt1)
        self.assertEqual(evt2.name, "event")
        self.assertEqual(evt2.sim_id, "123")
        self.assertEqual(evt2.sim_time, 1)
        self.assertEqual(evt2.created_by, "456")

        # make sure the validator got copied over.
        with self.assertRaises(ValueError):
            evt1.attr2 = 15
示例#4
0
 def test_event_creation_with_passed_properties(self):
     """Tests creating an event and passing in properties via the keyword."""
     evt = Event(name="kw_event",
                 attr1="one",
                 attr2=2,
                 attr3=3.3,
                 attr4=None)
     self.assertEqual(evt.name, "kw_event")
     self.assertEqual(evt.attr1, "one")
     self.assertEqual(evt.attr2, 2)
     self.assertEqual(evt.attr3, 3.3)
     self.assertIsNone(evt.attr4)
示例#5
0
    def test_priority_queue(self):
        """Tests creating a priority event queue and adding events."""
        peq = PriorityEventQueue(priorities=["high.*", "medium.*"])
        self.assertEqual(peq.next_time(), None)
        self.assertTrue(peq.is_empty())

        peq.add(Event(name="high-1"))
        peq.add(Event(name="medium-1"))
        peq.add(Event(name="low-1"))

        self.assertFalse(peq.is_empty())

        peq.add(Event(name="high-5", sim_time=5))
        peq.add(Event(name="medium-3", sim_time=3))
        peq.add(Event(name="low-2", sim_time=2))
        peq.add(Event(name="low-4", sim_time=4))
        peq.add(Event(name="medium-4", sim_time=4))
        peq.add(Event(name="low-10", sim_time=10))

        self.assertEqual(peq.next_time(), 1)

        self.assertEqual(peq.next().name, "high-1")
        self.assertEqual(peq.next_time(), 1)
        self.assertEqual(peq.next().name, "medium-1")
        self.assertEqual(peq.next_time(), 1)
        self.assertEqual(peq.next().name, "low-1")
        self.assertEqual(peq.next_time(), 2)

        self.assertEqual(peq.next().name, "low-2")
        self.assertEqual(peq.next_time(), 3)

        self.assertEqual(peq.next().name, "medium-3")
        self.assertEqual(peq.next_time(), 4)

        self.assertEqual(peq.next().name, "medium-4")
        self.assertEqual(peq.next_time(), 4)
        self.assertEqual(peq.next().name, "low-4")
        self.assertEqual(peq.next_time(), 5)

        self.assertEqual(peq.next().name, "high-5")
        self.assertEqual(peq.next_time(), 10)

        self.assertEqual(peq.next().name, "low-10")
        self.assertIsNone(peq.next_time())
示例#6
0
    def test_get_events(self):
        teq = TimeEventQueue()
        # add some events in various orders.
        teq.add(Event(name="event_3_1", sim_time=3))
        teq.add(Event(name="event_3_2", sim_time=3))
        teq.add(Event(name="event_2_1", sim_time=2))
        teq.add(Event(name="event_5_1", sim_time=5))
        teq.add(Event(name="event_1_1"))  # no time given, so 1

        self.assertEqual(len(teq), 5)
        self.assertEqual(teq._current_time, 0)

        event = teq.next()
        self.assertEqual(event.name, "event_1_1")
        self.assertEqual(teq._current_time, 1)
        self.assertEqual(teq.next_time(), 2)

        event = teq.next()
        self.assertEqual(event.name, "event_2_1")
        self.assertEqual(teq._current_time, 2)
        self.assertEqual(teq.next_time(), 3)

        event = teq.next()
        self.assertEqual(event.name, "event_3_1")
        self.assertEqual(teq._current_time, 3)
        self.assertEqual(teq.next_time(), 3)
        event = teq.next()
        self.assertEqual(event.name, "event_3_2")
        self.assertEqual(teq._current_time, 3)
        self.assertEqual(teq.next_time(), 5)

        event = teq.next()
        self.assertEqual(event.name, "event_5_1")
        self.assertEqual(teq._current_time, 5)
        self.assertEqual(teq.next_time(), None)  # what *should* happen here?

        self.assertTrue(teq.is_empty())
        event = teq.next()
        self.assertIsNone(event)
示例#7
0
    def test_is_complete(self):
        """Tests that an event has all of the properties to make it complete.  Events can be created without certain
        properties, but then need to have those set before sending out."""
        evt = Event(name="evt1")
        self.assertFalse(evt.is_complete())

        evt.sim_id = "123"
        evt.created_by = "456"
        self.assertTrue(evt.is_complete())
示例#8
0
    def test_len(self):
        teq = TimeEventQueue()
        for cnt in range(1, 4):  # add three events out of order.
            teq.add(Event(name="event_%d" % cnt, sim_time=10 - cnt))

        self.assertEqual(len(teq), 3)
示例#9
0
 def test_add_event_with_too_low_time(self):
     """Tests trying to add an event without a sim time."""
     event = Event(name="event1", sim_time=-2)
     teq = TimeEventQueue()
     with self.assertRaises(ValueError):
         teq.add(event)