Exemplo n.º 1
0
class StateStreamTestCase(unittest.TestCase):
    object = None  # should be set in subclass setUp

    def setUpForObject(self, obj):
        # pylint: disable=attribute-defined-outside-init
        self.object = obj
        self.updates = []
        self.poller = Poller()

        def send(value):
            self.updates.extend(json.loads(value))

        self.stream = StateStreamInner(
            send,
            self.object,
            'urlroot',
            lambda: None,  # TODO test noteDirty or make it unnecessary
            poller=self.poller)

    def getUpdates(self):
        # pylint: disable=attribute-defined-outside-init

        # warning: implementation poking
        self.poller.poll()
        self.stream._flush()
        u = self.updates
        self.updates = []
        return u
Exemplo n.º 2
0
class TestPoller(unittest.TestCase):
    def setUp(self):
        self.poller = Poller()
        self.cells = PollerCellsSpecimen()
    
    def test_trivial(self):
        cell = self.cells.state()['foo']
        called = [0]
        
        def callback():
            called[0] += 1
        
        sub = self.poller.subscribe(cell, callback, fast=True)
        self.assertEqual(0, called[0], 'initial')
        self.poller.poll(True)
        self.assertEqual(0, called[0], 'noop poll')
        self.cells.set_foo('a')
        self.assertEqual(0, called[0], 'after set')
        self.poller.poll(True)
        self.assertEqual(1, called[0], 'poll after set')
        
        sub.unsubscribe()
        self.cells.set_subscribable('b')
        self.poller.poll(True)
        self.assertEqual(1, called[0], 'no poll after unsubscribe')
Exemplo n.º 3
0
class TestPoller(unittest.TestCase):
    def setUp(self):
        self.log_tester = LogTester()
        self.poller = Poller(log=self.log_tester.log)
    
    def tearDown(self):
        self.flushLoggedErrors(DummyBrokenGetterException)
    
    def test_trivial_direct(self):
        cells = PollerCellsSpecimen()
        cell = cells.state()['foo']
        called = []
        
        def callback(value):
            called.append(value)
        
        sub = self.poller.subscribe(cell, callback, fast=True)
        self.assertEqual([], called, 'initial')
        self.poller.poll(True)
        self.assertEqual([], called, 'noop poll')
        cells.set_foo('a')
        self.assertEqual([], called, 'after set')
        self.poller.poll(True)
        self.assertEqual(['a'], called, 'poll after set')
        
        sub.unsubscribe()
        cells.set_subscribable('b')
        self.poller.poll(True)
        self.assertEqual(['a'], called, 'no poll after unsubscribe')
    
    def test_initially_throwing_getter(self):
        """Check fallback behavior when a cell getter throws."""
        called = []
        
        def callback(value):
            called.append(value)
        
        broken_cell = BrokenGetterSpecimen(True).state()['foo']
        self.poller.subscribe(broken_cell, callback, fast=True)
        self.log_tester.check(dict(log_format='Exception in {cell}.get()', cell=broken_cell))
        
        self.assertEqual(called, [])
        self.poller.poll(True)
        self.assertEqual(called, [])
        
    def test_later_throwing_getter(self):
        called = []
        
        def make_callback(key):
            def callback(value):
                called.append((key, value))
            return callback
        
        bgs = BrokenGetterSpecimen(False)
        not_broken_1 = PollerCellsSpecimen().state()['foo']
        broken_cell = bgs.state()['foo']
        not_broken_2 = PollerCellsSpecimen().state()['foo']
        self.poller.subscribe(not_broken_1, make_callback('1'), fast=True)
        self.poller.subscribe(broken_cell, make_callback('b'), fast=True)
        self.poller.subscribe(not_broken_2, make_callback('2'), fast=True)
        self.assertEqual(called, [])
        bgs.broken = True
        self.log_tester.check()
        self.poller.poll(True)
        self.log_tester.check(dict(log_format='Exception in {cell}.get()', cell=broken_cell))
        self.assertEqual(called, [])
Exemplo n.º 4
0
class TestPoller(unittest.TestCase):
    def setUp(self):
        self.log_tester = LogTester()
        self.poller = Poller(log=self.log_tester.log)
    
    def tearDown(self):
        self.flushLoggedErrors(DummyBrokenGetterException)
    
    def test_trivial_direct(self):
        cells = PollerCellsSpecimen()
        cell = cells.state()['foo']
        called = []
        
        def callback(value):
            called.append(value)
        
        sub = self.poller.subscribe(cell, callback, fast=True)
        self.assertEqual([], called, 'initial')
        self.poller.poll(True)
        self.assertEqual([], called, 'noop poll')
        cells.set_foo('a')
        self.assertEqual([], called, 'after set')
        self.poller.poll(True)
        self.assertEqual(['a'], called, 'poll after set')
        
        sub.unsubscribe()
        cells.set_subscribable('b')
        self.poller.poll(True)
        self.assertEqual(['a'], called, 'no poll after unsubscribe')
    
    def test_initially_throwing_getter(self):
        """Check fallback behavior when a cell getter throws."""
        called = []
        
        def callback(value):
            called.append(value)
        
        broken_cell = BrokenGetterSpecimen(True).state()['foo']
        self.poller.subscribe(broken_cell, callback, fast=True)
        self.log_tester.check(dict(log_format='Exception in {cell}.get()', cell=broken_cell))
        
        self.assertEqual(called, [])
        self.poller.poll(True)
        self.assertEqual(called, [])
        
    def test_later_throwing_getter(self):
        called = []
        
        def make_callback(key):
            def callback(value):
                called.append((key, value))
            return callback
        
        bgs = BrokenGetterSpecimen(False)
        not_broken_1 = PollerCellsSpecimen().state()['foo']
        broken_cell = bgs.state()['foo']
        not_broken_2 = PollerCellsSpecimen().state()['foo']
        self.poller.subscribe(not_broken_1, make_callback('1'), fast=True)
        self.poller.subscribe(broken_cell, make_callback('b'), fast=True)
        self.poller.subscribe(not_broken_2, make_callback('2'), fast=True)
        self.assertEqual(called, [])
        bgs.broken = True
        self.log_tester.check()
        self.poller.poll(True)
        self.log_tester.check(dict(log_format='Exception in {cell}.get()', cell=broken_cell))
        self.assertEqual(called, [])