def testDeepChangeIsSaved(self): data = Data({}) data.ref = TestStore() sync = Synchronous() data.change_obs._add(AutoSave(data, Synchronous())) data['list'] = [] self.assertEqual(data.ref.count, 1) data['list'].append(3) self.assertEqual(data.ref.count, 2)
def test(self): tm = Synchronous() items = [] def second(): items.append('second') def first(): tm.call(second) items.append('first') tm.callFromThread(first) # no threading, same as call. self.assertEqual(items, ['second', 'first'])
def __init__(self, transport, storage, t_model=None, verbose=False, jc_opts=None): thread_model = Synchronous() if t_model is None else t_model self.storage = storage self.node_id = transport.node_id self.vat_id = transport.path self.node = transport self.callbacks = {} self.thread_model = thread_model self.verbose = verbose self.safe = [] transport.subscribe('message', self.handle) transport.subscribe('online', self._notifyNodeObserver) transport.subscribe('connected', self._notifyNodeObserver) self.refs = [] if hasattr(storage, 'setContextFactory'): rpc = weakref.proxy(self) def makeStorageCtx(storage, path=None): return RPCStorageCtx(rpc, storage, path) storage.setContextFactory(makeStorageCtx) # NOTE: this is for the node observer. storage.getRPC = lambda: rpc self.remote_ctx = RemoteCtx(self) self.json_ctx = JSONCodecCtx(self, **(jc_opts or {}))
def testSlaveAndObserverChangesAreSaved(self): store = TestStore() data = Data({}) data.ref = store sync = Synchronous() saver = AutoSave(data, sync) data.change_obs._add(saver) data['name'] = 'Fred' self.assertEqual(store.count, 1) # Add observer data.obs._add(Proxy('A', 'obs')) self.assertEqual(store.count, 2) # Call slave update methods. data.add(['age'], 42) self.assertEqual(store.count, 3) data.change(['name'], 'Fred', 'Barney') self.assertEqual(store.count, 4) data.delete(['age'], 42) self.assertEqual(store.count, 5) # Remove observer data.obs._remove(Proxy('A', 'obs')) self.assertEqual(store.count, 6) self.assertEqual(len(data.obs.group), 0)
def testCallback(self): tm = Synchronous() items = [] def second(cb): items.append('first') cb.success('third') items.append('second') def first(): cb = tm.makeCallback() tm.call(second, cb) items.append(cb.wait()) # third items.append('fourth') tm.callFromThread(first) self.assertEqual(items, ['first', 'second', 'third', 'fourth'])
def testAutoSave(self): store = TestStore() data = Data({}) data.ref = store data['name'] = 'Fred' self.assertEqual(store.count, 0) # no save yet data.save() self.assertEqual(store.count, 1) self.assertEqual(store.value, {'name': 'Fred'}) sync = Synchronous() saver = AutoSave(data, sync) data.change_obs._add(saver) # with a synchronous save, every update is saved immediately data['age'] = 25 self.assertEqual(store.count, 2) self.assertEqual(store.value['age'], 25) data['age'] = 26 self.assertEqual(store.count, 3) self.assertEqual(store.value['age'], 26) # remove it, back to manual saving. data.change_obs._remove(saver) data['age'] = 27 self.assertEqual(store.count, 3) self.assertEqual(store.value['age'], 26) # green-threads are more efficient since a save will be done # only once per yield. green = EventletThread() green.start(thread=True) saver = AutoSave(data, green) data.change_obs._add(saver) cb = Callback() def makeUpdates(): data['age'] = 28 data['pet'] = 'Fido' data['address'] = 'Wiltshire' cb.success(None) green.callFromThread(makeUpdates) cb.wait() self.assertEqual(store.count, 4) self.assertEqual(store.value['address'], 'Wiltshire') green.stop()
class TestRPCHandler(object): def __init__(self): self.online = [] self.thread_model = Synchronous() self.pinged = [] self.node_observer = None def call(self, node, *rest): self.pinged.append(node) cb = self.thread_model.makeCallback() if node not in self.online: cb.failure(socket.error()) else: cb.success(None) self.node_observer.connected(node) return cb def getRPC(self): return self
def __init__(self): self.online = [] self.thread_model = Synchronous() self.pinged = [] self.node_observer = None
def testNoCallback(self): tm = Synchronous() cb = tm.makeCallback() self.assertRaises(Exception, cb.wait)