def test_processBlock_put(self): store = Store() block = B.create("test", 1, metadata={ "method": PUT_METHOD, "path": "t1" }) store.processBlock(B.serialize(block)) self.assertEqual(store.get("t1"), [1])
def test_journal_sharing(self): storeA = Store() storeA.addAll("t1", [1, 2, 3, 4]) storeB = Store() for journal in storeA.get(JOURNAL_PATH): block_serialized = B.serialize(journal) storeB.processBlock(block_serialized) self.assertEqual(storeB.get("t1"), [1, 2, 3, 4])
def test_client_inbound(self): block1 = B.serialize( B.create("t1", "A", { "method": ADD_METHOD, "path": "p1" })) block2 = B.serialize( B.create("t1", "B", { "method": ADD_METHOD, "path": "p1" })) signal1 = Signal(data=block1, channel="Ch1") signal2 = Signal(data=block2, channel="Ch1") socket1 = MockSocket() socket1.listOnRecv( [HUB_VERSION + SEPARATOR, signal1.bytes(), signal2.bytes()]) hub = MockHub(store=MockStore(), subscriptions=["Ch1"]) hub.mockSockets([socket1, socket1]) def callback(ami): s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s1.connect((hub.host, hub.port)) s1.close() time.sleep(0.2) self.assertEqual(Util.getStates(Util.run(hub, callback)), STATE_SEQUENCE) inbounds = [i[DATA] for i in hub.store.getLogs(INBOUND_PATH)] self.assertEqual( inbounds, [str(signal1.bytes()), str(signal2.bytes())]) p1_path = [i[DATA] for i in hub.store.getLogs("p1")] self.assertEqual(p1_path, ["A", "B"])
def test_HubClient_inboundQueue(self): signal = Signal(data=B.serialize(B.create('a', 'p')), channel="ABCDEF") socket = MockSocket() socket.listOnRecv([HUB_VERSION + SEPARATOR, signal.bytes()]) def callback(ami): time.sleep(0.2) hubClient = HubClient(socket, "127.0.0.1", store=MockStore()) self.assertEqual(Util.getStates(Util.run(hubClient, callback)), STATE_SEQUENCE) inbounds = hubClient.store.get(INBOUND_PATH) self.assertEqual(inbounds, [signal.bytes()])
def onTimeout(self): """ This method is run every self.timeout seconds """ outbounds = None if not self.last_outbound: # first outbound processing outbounds = self.store.get(JOURNAL_PATH) or [] else: outbounds = self.store.getSince(JOURNAL_PATH, self.last_outbound) # TODO: do filtering based on signal.data.metadata.path for outbound in outbounds: self.last_outbound = outbound if not B.is_block(outbound) or not B.is_valid(outbound): self.fail("Not a block: %s", outbound) raise TypeError() path = outbound[B.METADATA]['path'] if path.split('.')[0].endswith('_'): self.logger.debug("skipping block path %s", path) continue signal = Signal.parse(outbound) if not isinstance(signal, Signal): signal = Signal(data=B.serialize(outbound), channel="X") self.emit(signal) # Inbound processing inbounds = None if not self.last_inbound: inbounds = self.store.get(INBOUND_PATH) or [] else: inbounds = self.store.getSince(INBOUND_PATH, self.last_inbound) for signal in inbounds: signal = Signal.parse(signal) if not isinstance(signal, Signal): self.fail("Not a signal: %s", signal) raise TypeError() self.last_inbound = signal # ignore exact duplicated signal if signal.id in self.signal_seens: continue #TODO: truncate self.signal_seens to last 10000 self.signal_seens.add(signal.id) if self.hub_subscriptions.intersection(set(signal.channel)): self.store.processBlock(signal.data)
def test_emit_offline(self): hub = Hub(store=MockStore()) assert (hub.store.get(OUBOUND_PATH) is None) signal = Signal(channel="CH1", data=B.serialize(B.create('a', 'p'))) hub.emit(signal) assert (hub.store.get(OUBOUND_PATH) == [signal.bytes()])
def test_serialize_deserialize(self): block = B.create("author", {"data": 123}) serialized = B.serialize(block) self.assertIsInstance(serialized, bytes) self.assertEqual(block, B.deserialize(serialized))