示例#1
0
async def test_onMessageReceived(app: Application):
    listener = MagicMock()
    app.addListener(listener)
    command = NetworkManagementInclusion.NodeAddStatus()
    assert await app.onMessageReceived(None,
                                       Zip.ZipPacket(command=command)) is True
    listener.addNodeStatus.assert_called_once_with(app, command)

    listener.reset_mock()
    command = NetworkManagementInclusion.NodeRemoveStatus(
        status=NetworkManagementInclusion.NodeRemoveStatus.Status.FAILED)
    assert await app.onMessageReceived(None,
                                       Zip.ZipPacket(command=command)) is True
    listener.nodeRemoved.assert_not_called()
    listener.nodesRemoved.assert_not_called()

    listener.reset_mock()
    command = NetworkManagementInclusion.NodeRemoveStatus(
        status=NetworkManagementInclusion.NodeRemoveStatus.Status.DONE,
        nodeID=0)
    assert await app.onMessageReceived(None,
                                       Zip.ZipPacket(command=command)) is True
    listener.nodeRemoved.assert_called_once_with(app, 0)
    listener.nodesRemoved.assert_called_once_with(app, [0])

    assert (await app.onMessageReceived(
        None, Zip.ZipPacket(command=Basic.Get())) is False)

    listener.reset_mock()
    listener.messageReceived.return_value = True
    assert await app.onMessageReceived(
        None, Zip.ZipPacket(command=Basic.Get())) is True

    assert await app.onMessageReceived(None, Basic.Get()) is False
    assert await app.onMessageReceived(None, Zip.ZipPacket()) is False
示例#2
0
async def test_sendAndReceive(node: Node):
    async def noop(_):
        return True

    node.send = noop
    values = await asyncio.gather(
        node.sendAndReceive(Basic.Get(), Basic.Report),
        runDelayed(node.messageReceived, Basic.Report()),
    )
    assert isinstance(values[0], Basic.Report)
示例#3
0
async def test_sendAndReceive(adapter: Adapter):
    async def noop(_):
        pass

    adapter.send = noop  # Throws not implemented as default
    values = await asyncio.gather(
        adapter.sendAndReceive(Basic.Get(), Basic.Report),
        runDelayed(adapter.commandReceived, Basic.Report()),
    )
    assert isinstance(values[0], Basic.Report)
示例#4
0
async def test_handleMessage_onMessage(node: Node):
    # Test catch all message handler
    listener = MagicMock()
    node.addListener(listener)

    listener.onMessage.return_value = True
    assert await node.handleMessage(Basic.Report(value=0), 0) is True

    listener.onMessage.return_value = False
    assert await node.handleMessage(Basic.Report(value=0), 0) is False
示例#5
0
async def test_handleMessage(node: Node):
    # Skip handlers, when there is a session wating for the message
    node.addWaitingSession(Basic.Get)
    assert await node.handleMessage(Basic.Get(), 0) is True

    # Try again without any sessions
    assert await node.handleMessage(Basic.Get(), 0) is False

    # Send message with handler
    assert (await node.handleMessage(
        AssociationGrpInfo.GroupNameReport(groupingsIdentifier=1,
                                           name="Lifeline"),
        0,
    ) is True)
示例#6
0
async def test_messageReceived(app: Application):
    handler = MagicMock()
    handler.onSet.return_value = False  # Do not handle these messages
    cmdClass = app.nodes["3:0"].supported[Basic.COMMAND_CLASS_BASIC]
    cmdClass.addListener(handler)
    assert await app.messageReceived(None, 4, 0, Basic.Report(value=42),
                                     0) is False
    assert await app.messageReceived(None, 3, 0, Basic.Report(value=42),
                                     0) is True
    assert await app.messageReceived(None, 3, 0, Basic.Set(value=0),
                                     0) is False
    handler.onReport.assert_called_once_with(cmdClass, Basic.Report(value=42),
                                             0)
    assert (await app.messageReceived(None, app.adapter.nodeId, 0,
                                      Basic.Report(value=42), 0) is True)
示例#7
0
async def test_sendAndReceive_no_wakeup(node: Node):
    async def noop(_):
        return False

    node.send = noop
    with pytest.raises(asyncio.TimeoutError):
        await node.sendAndReceive(Basic.Get(), Basic.Report)
示例#8
0
async def test_sendToNode(gateway: ZIPGateway):
    connection = await gateway.connectToNode(6)
    [res, _] = await asyncio.gather(
        gateway.sendToNode(6, Basic.Get()),
        runDelayed(connection.ackReceived, Zip.ZipPacket(seqNo=1)),
    )
    assert res == True
示例#9
0
async def test_send(node: Node):
    msg = Basic.Get()
    assert await node.send(msg) is True
    node._adapter.sendToNode.assert_called_with(2,
                                                msg,
                                                sourceEP=0,
                                                destEP=0,
                                                timeout=3)
示例#10
0
def test_onMessageReceived(gateway: ZIPGateway):
    connection = DummyConnection()
    msg = Basic.Get()
    gateway._connections = {2: connection}
    gateway.onMessageReceived(connection, Zip.ZipPacket(sourceEP=0,
                                                        command=msg))
    gateway.listener.messageReceived.assert_called_once_with(
        gateway, 2, 0, msg, 0)
示例#11
0
async def test_send(connection: ZIPConnection):
    connection._conn.send = MagicMock()
    basicGet = Basic.Get()
    [res, _] = await asyncio.gather(
        connection.send(basicGet),
        runDelayed(connection.ackReceived, Zip.ZipPacket(seqNo=1)),
    )
    assert res is True
    connection._conn.send.assert_called_once_with(
        b"#\x02\x80\x50\x01\x00\x00 \x02")
示例#12
0
async def test_supervision_not_handled(node: Node):
    supervision = Supervision.Get(statusUpdates=False,
                                  sessionID=42,
                                  command=Basic.Report(value=1))
    assert await node.handleMessage(supervision, 0) is False
    node._adapter.sendToNode.assert_called_with(
        node.rootNodeId,
        Supervision.Report(
            moreStatusUpdates=False,
            wakeUpRequest=False,
            sessionID=supervision.sessionID,
            status=0x0,
            duration=0,
        ),
        destEP=0,
        sourceEP=0,
        timeout=3,
    )
示例#13
0
def test_onPacket_Zip(connection: ZIPConnection):
    connection.ackReceived = MagicMock()
    connection.commandReceived = MagicMock()

    ackResponse = Zip.ZipPacket(
        ackRequest=False,
        ackResponse=True,
        nackResponse=False,
        nackWaiting=False,
        nackQueueFull=False,
        nackOptionError=False,
        headerExtIncluded=False,
        zwCmdIncluded=False,
        moreInformation=False,
        secureOrigin=True,
        seqNo=0,
        sourceEP=0,
        destEP=0,
        command=None,
    )
    assert connection.onPacket(ackResponse.compose()) is True
    connection.ackReceived.assert_called_once()

    nackResponse = Zip.ZipPacket(
        ackRequest=False,
        ackResponse=False,
        nackResponse=True,
        nackWaiting=False,
        nackQueueFull=False,
        nackOptionError=False,
        headerExtIncluded=False,
        zwCmdIncluded=False,
        moreInformation=False,
        secureOrigin=True,
        seqNo=0,
        sourceEP=0,
        destEP=0,
        command=None,
    )
    msg = Message.decode(nackResponse.compose())
    assert msg.ackRequest == False
    assert msg.ackResponse == False
    assert msg.nackResponse == True
    assert connection.onPacket(nackResponse.compose()) is False

    nackResponse = Zip.ZipPacket(
        ackRequest=False,
        ackResponse=False,
        nackResponse=True,
        nackWaiting=True,
        nackQueueFull=False,
        nackOptionError=False,
        headerExtIncluded=False,
        zwCmdIncluded=False,
        moreInformation=False,
        secureOrigin=True,
        seqNo=0,
        sourceEP=0,
        destEP=0,
        command=None,
    )
    msg = Message.decode(nackResponse.compose())
    assert msg.ackRequest == False
    assert msg.ackResponse == False
    assert msg.nackResponse == True
    # This should be treated as success
    assert connection.onPacket(nackResponse.compose()) is True

    ackRequest = Zip.ZipPacket(
        ackRequest=True,
        ackResponse=False,
        nackResponse=False,
        nackWaiting=False,
        nackQueueFull=False,
        nackOptionError=False,
        headerExtIncluded=False,
        zwCmdIncluded=False,
        moreInformation=False,
        secureOrigin=True,
        seqNo=0,
        sourceEP=0,
        destEP=0,
        command=None,
    )
    assert connection.onPacket(ackRequest.compose()) is False

    pkt = Zip.ZipPacket(
        ackRequest=False,
        ackResponse=False,
        nackResponse=False,
        nackWaiting=False,
        nackQueueFull=False,
        nackOptionError=False,
        headerExtIncluded=False,
        zwCmdIncluded=True,
        moreInformation=False,
        secureOrigin=True,
        seqNo=0,
        sourceEP=0,
        destEP=0,
        command=Basic.Get(),
    )
    assert connection.onPacket(pkt.compose()) is True
    connection.commandReceived.assert_called_once()
示例#14
0
async def test_send_timeout(connection: ZIPConnection):
    basicGet = Basic.Get()
    assert await connection.send(basicGet, timeout=0) is False
示例#15
0
def test_commandReceived(adapter: Adapter):
    adapter.messageReceived = MagicMock()
    adapter.commandReceived(Zip.ZipPacket(command=Basic.Report(value=0)))
    adapter.messageReceived.assert_called_once_with(Basic.Report(value=0))
示例#16
0
def test_onMessageReceived_noNode(gateway: ZIPGateway):
    gateway._connections = {2: DummyConnection()}
    gateway.onMessageReceived(DummyConnection(), Basic.Get())
    gateway.listener.messageReceived.assert_not_called()
示例#17
0
async def test_sendToNode(adapter: Adapter):
    with pytest.raises(NotImplementedError):
        await adapter.sendToNode(6, Basic.Get())
示例#18
0
async def test_waitForMessage(adapter: Adapter):
    await asyncio.gather(
        adapter.waitForMessage(Basic.Get),
        runDelayed(adapter.commandReceived, Basic.Get()),
    )