def testInit(self):
        """
        testInit makes assertations about things
        that should happen when a HelperRobot
        is created.
        """
        mediator = RobotMediator()
        helper = HelperRobot(mediator)

        # Is helper an instance of HelperRobot?
        self.assertIsInstance(helper, HelperRobot)
        # Is helper also an instance of Robot?
        self.assertIsInstance(helper, Robot)
        # Robot should not be working on a task yet.
        self.assertFalse(helper.isRunning())
        # helper should have been added to mediator's
        # list of robots.
        self.assertIn(helper, mediator.getRobots())
 def testFinishWork(self):
     """
     testFinishWork makes assertations about things
     that should happen when a HelperRobot is asked
     to finish a task.
     """
     mediator = RobotMediator()
     helper = HelperRobot(mediator)
    
     # Trying to tell the helper to finish the
     # job when it's not working on anything
     # should result in a NotBusyException.
     with self.assertRaises(NotBusyException):
         helper.finishWork()
         helper.finishWork()
     # But if it's working on a task, it should
     # gracefully finish the task.
     try:
         helper.doWork()
         helper.finishWork()
     except Exception as e:
         self.fail(e)
 def testDoWork(self):
     """
     testDoWork makes assertations about things
     that should happen when a HelperRobot is asked
     to perform a task.
     """
     mediator = RobotMediator()
     helper = HelperRobot(mediator)
     
     # Assign one job.
     try:
         helper.doWork()
     except TooBusyException as e:
         self.fail(e)
     # Try to assign another, but helper
     # should be too busy.
     with self.assertRaises(TooBusyException):
         helper.doWork()
         # Try again -- should still be busy.
         helper.doWork()
     # Tell helper it's finished with the task.
     helper.finishWork()
     # Now assign another job -- should accept.
     try:
         helper.doWork()
     except TooBusyException as e:
         self.fail(e)
    def testDoWorkA(self):
        """
        testDoWork makes assertations about things
        that should happen when a MasterRobot is asked
        to perform a task.
        """
        # -------------------------- #
        # 1 master and 1 helper      #
        # -------------------------- #
        mediator = RobotMediator()
        helper = HelperRobot(mediator)
        master = MasterRobot(mediator)
    
        # Assign one job.
        try:
            master.doWork()
        except TooBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 1 running 0 queued.
        self.assertEquals(1, master.getRunningJobs())
        self.assertEquals(0, master.getQueuedJobs())
        
        # Assign second job.
        try:
            master.doWork()
        except TooBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 2 running 0 queued.
        self.assertEquals(2, master.getRunningJobs())
        self.assertEquals(0, master.getQueuedJobs())

        # Assign third job.
        try:
            master.doWork()
        except TooBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 2 running 1 queued.
        self.assertEquals(2, master.getRunningJobs())
        self.assertEquals(1, master.getQueuedJobs())

        # Assign fourth job.
        try:
            master.doWork()
        except TooBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 2 running 2 queued.
        self.assertEquals(2, master.getRunningJobs())
        self.assertEquals(2, master.getQueuedJobs())
        
        # Tell the helper that it's finished doing
        # its job.
        try:
            helper.finishWork()
        except NotBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 2 running 1 queued.
        self.assertEquals(2, master.getRunningJobs())
        self.assertEquals(1, master.getQueuedJobs())
        
        # Tell the helper that it's finished doing
        # its job.
        try:
            helper.finishWork()
        except NotBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 2 running 0 queued.
        self.assertEquals(2, master.getRunningJobs())
        self.assertEquals(0, master.getQueuedJobs())
        

        # Tell the helper that it's finished doing
        # its job.
        try:
            helper.finishWork()
        except NotBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 1 running 0 queued.
        self.assertEquals(1, master.getRunningJobs())
        self.assertEquals(0, master.getQueuedJobs())
        
        # Tell the helper that it's finished doing
        # its job.
        try:
            master.finishWork()
        except NotBusyException as e:
            self.fail(e)
        # Verify the status.
        # Should be 0 running 0 queued.
        self.assertEquals(0, master.getRunningJobs())
        self.assertEquals(0, master.getQueuedJobs())