randomsync / robotframework-mqttlibrary

MQTT Keyword Library for Robot Framework
Apache License 2.0
25 stars 30 forks source link

fix issue with subscribe async with wildcard topics #19

Closed randomsync closed 4 years ago

randomsync commented 4 years ago

With the addition of async subscribe, we are keeping a dict of topic names and the corresponding messages received. When subscribing with a wildcard topic (eg: 'Company/+/Data'), that topic is added to dict, but when a publish is received on a matching topic, it is added to a new exact key (eg: 'Company/test/Data'). So when listening and getting messages for the wildcard topic, those were not added in the right place.

Used paho's Matcher to match whether a topic matches a wildcard subscription, and added messages to all matched topics.

Fixes #17

deneb78it commented 4 years ago

There is still an issue.

If you repeat the test cases in my comment but also use the # char at the end of the topic, it doesn't work.

In details:

TEST CASE CODE

*** Settings ***
Documentation    this is a suite to study the robot mqtt lib
Library             MQTTLibrary
Library             DateTime
Library             os

*** Variables ***

#${topic}    Company/Buildings/Data/test/Rooms/test/Desks/test/Objects/test
${topic}    Company/Buildings/Data/test/Rooms/test/Desks/+/Objects/#
#${broker}          mqtt.flespi.io
${broker}           127.0.0.1
${port}             1883
#${user}             MS_MONITORING
#${password}         MS_MONITORING_PASSWORD

${base_client}   emq_subscriber

*** Test Cases ***
Test title
    [Tags]    study
    ${client}=   Generate ClientId

    When Connect                        ${broker}   ${port}  client_id= ${client}
    log to console          ${topic}

    Subscribe         topic=${topic}  qos=0  timeout=0  limit=30

    ${messages}     Listen          topic=${topic}    limit=20   timeout=10

    log to console          ${messages}
    [Teardown]  Run Keywords    unsubscribe    ${topic}   AND   disconnect

*** Keywords ***
Generate ClientId
    ${time}=    Get Current Date    result_format=%Y-%m-%d%H%M%S
    ${client}=  catenate  ${base_client}${time}
    log to console  ${client}
    [Return]    ${client}`

OUTPUT

It often (not always) exit with this output

Test title emq_subscriber2020-07-12164746 ..Company/Buildings/Data/test/Rooms/test/Desks/+/Objects/# [ WARN ] Cannot listen when not subscribed to a topic Test title .[] Test title

Sorry for the format issue but basically i changed only the topic variable.

randomsync commented 4 years ago

If you repeat the test cases in my comment but also use the # char at the end of the topic, it doesn't work.

I have a test case which does that and it's passing (see https://github.com/randomsync/robotframework-mqttlibrary/pull/19/files#diff-5876264ee56d55af0499cc22b6b7b693R18). I think the issue you're encountering is: Cannot listen when not subscribed to a topic, which is the other issue that I haven't looked into yet. It seems to be a timing issue, so can you put a few seconds wait in between subscribe and listen, and try running the test again. Let's just make sure for now the wildcards issue is fixed.

randomsync commented 4 years ago

@deneb78it can you try this branch? It has both the fixes.

deneb78it commented 4 years ago

It seems OK