示例#1
0
 def _make_batch(self, msgs):
     if len(msgs) > 1:
         batch = Batch(msgs, None)
         serialized_batch = self.sign_and_serialize(batch)
     else:
         serialized_batch = msgs[0]
     return serialized_batch
def test_unpack_node_msg_with_str_as_msg_in_batch(create_node_and_not_start):
    node = create_node_and_not_start
    while node.nodeInBox:
        node.nodeInBox.pop()
    batch = Batch(
        ['pi', '{"op": "INSTANCE_CHANGE",'
         ' "viewNo": 1, '
         ' "reason": 25}'], None)
    node.unpackNodeMsg(batch, 'SomeNode')
    assert len(node.nodeInBox) == 1
    m, frm = node.nodeInBox.pop()
    assert isinstance(m, InstanceChange)
示例#3
0
 def flushOutBoxes(self) -> None:
     """
     Clear the outBoxes and transmit batched messages to remotes.
     """
     removedRemotes = []
     for rid, msgs in self.outBoxes.items():
         try:
             dest = self.remotes[rid].name
         except KeyError:
             removedRemotes.append(rid)
             continue
         if msgs:
             if len(msgs) == 1:
                 msg = msgs.popleft()
                 # Setting timeout to never expire
                 self.transmit(msg,
                               rid,
                               timeout=self.messageTimeout,
                               serialized=True)
                 logger.trace("{} sending msg {} to {}".format(
                     self, msg, dest))
             else:
                 logger.debug(
                     "{} batching {} msgs to {} into one transmission".
                     format(self, len(msgs), dest))
                 logger.trace("    messages: {}".format(msgs))
                 batch = Batch(list(msgs), None)
                 msgs.clear()
                 # don't need to sign the batch, when the composed msgs are
                 # signed
                 payload, err_msg = self.signAndSerialize(batch)
                 if payload is not None:
                     logger.trace("{} sending payload to {}: {}".format(
                         self, dest, payload))
                     # Setting timeout to never expire
                     self.transmit(payload,
                                   rid,
                                   timeout=self.messageTimeout,
                                   serialized=True)
                 else:
                     logger.warning("{} error {}. tried to {}: {}".format(
                         self, err_msg, dest, payload))
     for rid in removedRemotes:
         logger.warning("{}{} rid {} has been removed".format(
             CONNECTION_PREFIX, self, rid),
                        extra={"cli": False})
         msgs = self.outBoxes[rid]
         if msgs:
             self.discard(msgs,
                          "{}rid {} no longer available".format(
                              CONNECTION_PREFIX, rid),
                          logMethod=logger.debug)
         del self.outBoxes[rid]
示例#4
0
 def _make_batch(self, msgs):
     batch = Batch(msgs, None)
     serialized_batch = self.sign_and_serialize(batch)
     return serialized_batch
示例#5
0
def test_use_send_from_zstack_on_resend(func_create_stacks, looper):
    aStack, bStack = func_create_stacks(2)
    connectStack(aStack, bStack)
    """
    Sending some pi msgs for creating a batch on flashOutBox
    This function just put 'pi ' message into outBoxes queue, not send
    """
    aStack.sendPingPong(bStack.name)
    aStack.sendPingPong(bStack.name)
    """
    Emulate batch creation and sending. Batch should be added into _stashed_to_disconnected queue
    """
    aStack.flushOutBoxes()
    assert len(aStack._stashed_to_disconnected[bStack.name]) == 1
    batch_to_disconnected = aStack.deserializeMsg(
        aStack._stashed_to_disconnected[bStack.name][0])
    assert OP_FIELD_NAME in batch_to_disconnected and batch_to_disconnected[
        OP_FIELD_NAME] == BATCH
    """
    This method call connect method for bStack and put 'pi' message into outBoxes queue
    """
    connectStack(bStack, aStack)
    """
    Wait for socket's connecting routines
    """
    looper.runFor(1)
    """
    This instruction get 'pi' message from outBoxes queue, create a batch if needed and send it to aStack
    """
    bStack.flushOutBoxes()
    """
    It needs for getting 'pi' message from bStack. It process 'pi' message and put 'po' message to outBoxes queue
    """
    looper.run(aStack.service())
    """
    Send 'po' message to bStack
    """
    aStack.flushOutBoxes()
    """
    Processing previous sending batch (zmq feature) and 'po'
    """
    looper.run(bStack.service())
    """
    For sending 'po' message to aStack
    """
    bStack.flushOutBoxes()
    """
    Append 'pi' msg for checking that batch into batch will not be included
    """
    aStack._stashed_to_disconnected[bStack.name].append('pi')
    """
    Emulate that aStack got 'po' message from bStack and it must run _resend_to_disconnected
    """
    looper.run(aStack.service())
    """
    Emulate batch creating and sending
    """
    aStack.flushOutBoxes()

    looper.run(bStack._serviceStack(bStack.age, None))
    """
    rxMsgs queue should contains only one 'pi' message from step 3 and batch
    which was failed to sending to disconnected stack from step 2
    """
    got_pi = False
    got_batch = False
    while bStack.rxMsgs:
        m, frm = bStack.rxMsgs.popleft()
        if m.encode() not in bStack.healthMessages:
            msg = bStack.deserializeMsg(m)
        else:
            got_pi = True
            continue
        if OP_FIELD_NAME in msg and msg[OP_FIELD_NAME] == BATCH:
            if msg == batch_to_disconnected:
                """
                Exactly the same batch which should be sent to disconnected node
                """
                got_batch = True
                continue
            else:
                """Check that there is no batches with batch as message"""
                batch = Batch(messages=msg[f.MSGS.nm], signature=msg[f.SIG.nm])
                for m in batch.messages:
                    assert OP_FIELD_NAME not in m and BATCH not in m
    assert got_pi and got_batch