randomsync / robotframework-mqttlibrary

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

I get only empty lines form a topic #17

Closed deneb78it closed 4 years ago

deneb78it commented 4 years ago

I tried to connect to a topic and get the payload without luck.

I used the lines below.

My topic contains wildchars and is something like: Company/Buildings/Data/+/Rooms/+/Desks/+/Objects/+

During the test I connect to the same broker with mosquitto and it correctly catches the payload as expected.

Something wrong in my code? Or is there an issue?

*** Test Cases ***
Test title
    [Tags]    study
    Given Set Username And Password        ${user}      ${password}
    When Connect                        ${broker}   ${port}
    log to console  ${topic}
    ${messages}     Subscribe          topic=${topic}  qos=0  limit=0   timeout=20

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

Regards

deneb78it commented 4 years ago

I made other tests, and the root cause seems to be on de wild cards.

If I specify the topic without + or # the library works. If I insert + or # it does not.

randomsync commented 4 years ago

@deneb78it I'm not able to repro this issue locally. Here's the complete test case that I'm running. Note that I'm using the same topic you specified: Company/Buildings/Data/+/Rooms/+/Desks/+/Objects/+ to subscribe.

| *Settings*    | *Value*
| Resource      | keywords.robot
| Test Timeout  | 30 seconds

| *Test Cases*
| Publish and Subscribe to a broker that requires username, password authentication
| | [Tags]      | auth
| | ${time}     | Get Time      | epoch
| | ${client}   | Catenate      | SEPARATOR=.   | robot.mqtt | ${time}
| | ${topic}    | Set Variable  | Company/Buildings/Data/+/Rooms/+/Desks/+/Objects/+
| | ${message}  | Set Variable  | subscription test message
| | Set username and password   | authuser1     | password1
| | Run Keyword And Expect Error                | The expected payload didn't arrive in the topic
| | ... | Subscribe to MQTT Broker and Validate | broker.uri=127.0.0.1  | client.id=${client}
| | ... | topic=${topic}        | message=${message}
| | Connect     | 127.0.0.1
| | Publish     | Company/Buildings/Data/test/Rooms/test/Desks/test/Objects/test      | test message with username and password   | qos=1
| | Subscribe to MQTT Broker and Validate
| | ...         | broker.uri=127.0.0.1  | client.id=${client}   | topic=${topic}        | message=test message with username and password
| | [Teardown]  | Disconnect

I'm running the mosquitto broker locally using their docker image: docker run -p 1883:1883 -p 9001:9001 eclipse-mosquitto:latest

Here's the test result:

> robot -L debug -P src  tests/wildcards.robot
==============================================================================
Wildcards
==============================================================================
Publish and Subscribe to a broker that requires username, password... | PASS |
------------------------------------------------------------------------------
Wildcards                                                             | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
deneb78it commented 4 years ago

Thank you for your answer.

have you tried to catch a message list with the function Subscribe? I repeated today and have the same problem. When I replace + with a path value, the payload is caught.

`${messages} Subscribe topic=${topic} qos=0 limit=0 timeout=20

log to console ${messages}`

deneb78it commented 4 years ago

Hi

With this test, I reproduced both the async problem and the wrong behaviour in case of + # symbols.

Environment I work on Mac OS I use a mosquitto server in my local PC. I run it with the command: /usr/local/sbin/mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf

During my test I use the mosquitto publisher to send messages and the robot framework test to subscribe and detect. Here below the full test content. Please ignore the keyword, it is not relevant for the issue.

Robot Framework 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/test

${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}

TEST 1: subscribe to the topic not using special chars

  1. MQTT Server is running
  2. I run the robot framework script using the topic commentend in the code above Company/Buildings/Data/test/Rooms/test/Desks/test/Objects/test
  3. I publish the following topic: _mosquittopub -h localhost -p 1883 -t 'Company/Buildings/Data/test/Rooms/test/Desks/test/Objects/test' -m 'this is a test'
  4. this is the output:
Test title                                                            emq_subscriber2020-07-05161753
..Company/Buildings/Data/test/Rooms/test/Desks/test/Objects/test
...['this is a test']
Test title                                                            | PASS |
------------------------------------------------------------------------------
Subscription :: this is a suite to study the robot mqtt lib           | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed

TEST 2: use a special char in the topic

  1. MQTT Server is running
  2. I run the robot framework script using the topic NOT commentend in the code above Company/Buildings/Data/test/Rooms/test/Desks/+/Objects/test
  3. I publish the following topic: _mosquittopub -h localhost -p 1883 -t 'Company/Buildings/Data/test/Rooms/test/Desks/test/Objects/test' -m 'this is a test'
  4. this is the output:
Test title                                                            emq_subscriber2020-07-05161850
..Company/Buildings/Data/test/Rooms/test/Desks/+/Objects/test
...[]
Test title                                                            | PASS |
------------------------------------------------------------------------------
Subscription :: this is a suite to study the robot mqtt lib           | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed

While the Expected result is the same of test 1.

TEST 3: Check ASYNC behavior

repeat test 1 or 2 some times and randomly obtain the error below. It is issued immediatly ad test start.

Test title                                                            emq_subscriber2020-07-05164004
..Company/Buildings/Data/test/Rooms/test/Desks/+/Objects/test
**[ WARN ] Cannot listen when not subscribed to a topic**                         
Test title                                                            .[]
Test title                                                            | PASS |
------------------------------------------------------------------------------
Subscription :: this is a suite to study the robot mqtt lib           | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
deneb78it commented 4 years ago

closed for mistake. In my previous comment how to reproduce.

randomsync commented 4 years ago

Thanks @deneb78it I'll take a look.

randomsync commented 4 years ago

For the messages not getting received with subscription with wildcard topics, the issue was that 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 key (eg: 'Company/test/Data'). So when listening and getting messages for the wildcard topic, those were not added in the right place.

Thankfully, paho library provides a Matcher to match whether a topic matches a wildcard subscription.

@deneb78it I've added a fix in #19. I'm still checking if more tests need to be added, but can you give it a try with your scenarios with this branch and see if they pass now?