Пример #1
0
class Observable_Test(unittest.TestCase):
    '''
    Test Observable.
    
    @see TestParser.Common.Observable
    '''

    subscriber = Mock_Observer()
    subscriber2 = Mock_Observer()

    def setUp(self):
        self.subject = Observable()

    def tearDown(self):
        del self.subject

    def testRegister(self):
        self.subject.registerObserver(Observable_Test.subscriber)
        self.assertEqual(len(self.subject._observers), 1)
    
    def testRemove(self):
        self.subject.registerObserver(Observable_Test.subscriber)
        self.subject.registerObserver(Observable_Test.subscriber2)
        
        self.subject.removeObserver(Observable_Test.subscriber)
        self.assertEqual(len(self.subject._observers), 1)

        self.subject.removeObserver(Observable_Test.subscriber2)
        self.assertEqual(len(self.subject._observers), 0)        
        

    def testNotify_unthreaded(self):
        from TestParser.Common.Constants import CONSTANTS
        CONSTANTS.threading = False
        
        self.subject.registerObserver(Observable_Test.subscriber)
        self.subject.registerObserver(Observable_Test.subscriber2)
        
        self.subject.notifyObservers()
        
        self.assertTrue(Observable_Test.subscriber.notified)
        self.assertTrue(Observable_Test.subscriber2.notified)
        
    def testNotify_threaded(self):
        '''
        Test with threading.
        
        
        Need to create the job pool b/c threading is False by default,
        so the job pool isn't created during object initialization.
        Note that its fine during program execution, but needs to be
        done here. 
        '''
        from TestParser.Common.Constants import CONSTANTS
        CONSTANTS.threading = True
        self.subject._updateJobPool.createPool(2)
        
        self.subject.registerObserver(Observable_Test.subscriber)
        self.subject.registerObserver(Observable_Test.subscriber2)
        
        self.subject.notifyObservers()
        
        self.assertTrue(Observable_Test.subscriber.notified)
        self.assertTrue(Observable_Test.subscriber2.notified)