Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing virtual keyword for some funcktions #411

Closed
Hadatko opened this issue Oct 14, 2022 · 4 comments
Closed

Missing virtual keyword for some funcktions #411

Hadatko opened this issue Oct 14, 2022 · 4 comments
Assignees
Labels
fix added A fix has been pushed to the repo and is being tested missing feature A feature of the MQTT protocol that is missing from the library
Milestone

Comments

@Hadatko
Copy link

Hadatko commented Oct 14, 2022

Hi,
in class class client : private callback are most of functions virtual but some of them are not (most of them at the bottom). Is there reason why not? I would like to override implementation with my google tests for testing purpose:

image

@StefanoLusardi
Copy link

Hi @Hadatko, I have the exact same need: I would like to override some methods in my unit tests. Have you found a workaround for this issue?
Thanks

@Hadatko
Copy link
Author

Hadatko commented Sep 2, 2023

Hi @StefanoLusardi , yesterday i wrote you nice answer but it looks like i didn't send it :'( So once again :
Yes i found solution which works for me:

  1. We needed add include path for test directory: https://github.com/eclipse/paho.mqtt.cpp/tree/master/test/unit
  2. we needed include #include "mock_action_listener.h" and #include "mock_async_client.h"
  3. we created custom class:
class MqttClientPaho : public mqtt::client
{
public:
  /**
   * @brief Construct a new Mqtt Client Paho object
   *
   * @param[in] serverURI mqtt server address
   * @param[in] clientId client identifier
   * @param[in] opts paho client options passed by the user
   */
  MqttClientPaho(const std::string &serverURI, const std::string &clientId, const mqtt::create_options &opts);

  /**
   * @brief Calls the start_consuming paho library method
   */
  virtual void startConsuming(void);

  /**
   * @brief Calls the consume_message paho library method
   *
   * @return mqtt::const_message_ptr consumed message to be processed further
   */
  virtual mqtt::const_message_ptr consumeMessage(void);
};

MqttClientPaho::MqttClientPaho(const std::string &serverURI, const std::string &clientId,
                               const mqtt::create_options &opts) :
    client(serverURI, clientId, opts)
{
}

void MqttClientPaho::startConsuming(void) { start_consuming(); }

mqtt::const_message_ptr MqttClientPaho::consumeMessage(void) { return consume_message(); }

MqttClient::MqttClient(MqttClientPaho &client, const std::string &topic, LogCb *logCb) :
    SetLogCb(logCb), m_client(client), m_topic(topic), m_receiveLoopThread(nullptr), m_deinit(true)
{
}
  1. Google Mock class
class MockMqttClientPaho : public aeler::MqttClientPaho
{
public:
  MockMqttClientPaho(const std::string &serverURI = "uri", const std::string &clientId = "clientId",
                     const mqtt::create_options &opts = mqtt::create_options()) :
      aeler::MqttClientPaho(serverURI, clientId, opts){};

  MOCK_METHOD(void, disconnect, (), (override));

  MOCK_METHOD(mqtt::connect_response, connect, (mqtt::connect_options opts), (override));

  MOCK_METHOD(mqtt::subscribe_response, subscribe,
              (const std::string &topicFilter, int qos, const mqtt::subscribe_options &opts,
               const mqtt::properties &props),
              (override));

  MOCK_METHOD(void, startConsuming, (), (override));

  MOCK_METHOD(mqtt::const_message_ptr, consumeMessage, (), (override));

  MOCK_METHOD(bool, is_connected, (), (const, override));

  MOCK_METHOD(void, publish, (mqtt::string_ref top, const void *payload, size_t n), (override));
};
  1. Created test (here must be included the header files mentioned at the beginning). Partial code snippet:
  ::testing::StrictMock<MockMqttClientPaho> clientPaho;

  mqtt::mock_async_client ac;
  mqtt::token conToken(mqtt::token::CONNECT, ac);
  MQTTAsync_successData conData;
  conData.alt.connect.sessionPresent = false;
  conData.alt.connect.serverURI = const_cast<char *>("tcp://some_server.com");
  mqtt::mock_async_client::succeed(&conToken, &conData);
  MQTTAsync_successData subData;
  mqtt::token subToken(mqtt::token::SUBSCRIBE, ac);
  mqtt::mock_async_client::succeed(&subToken, &subData);

  EXPECT_CALL(clientPaho, connect).WillOnce(::testing::Return(conToken.get_connect_response()));
  EXPECT_CALL(clientPaho, subscribe).WillOnce(::testing::Return(subToken.get_subscribe_response()));
  EXPECT_CALL(clientPaho, publish).Times(1);
  EXPECT_CALL(clientPaho, publish).WillOnce(MqttException()).RetiresOnSaturation();
  EXPECT_CALL(clientPaho, startConsuming).Times(1);
  EXPECT_CALL(clientPaho, consumeMessage).WillRepeatedly(::testing::Return(nullptr));
  EXPECT_CALL(clientPaho, is_connected).WillOnce(::testing::Return(true)).WillOnce(::testing::Return(false));
  EXPECT_CALL(clientPaho, disconnect).Times(1);

@fpagliughi
Copy link
Contributor

fpagliughi commented Sep 5, 2023

Apologies. Just back from holiday and catching up on issues...

This was probably just an oversight. I will add the virtual keyword as needed in the next release. Hopefully I can get to that soon; this library hasn't been updated in a long time.

@fpagliughi fpagliughi added this to the v1.3 milestone Nov 11, 2023
@fpagliughi fpagliughi self-assigned this Nov 11, 2023
@fpagliughi fpagliughi added the missing feature A feature of the MQTT protocol that is missing from the library label Nov 11, 2023
@fpagliughi fpagliughi added the fix added A fix has been pushed to the repo and is being tested label Nov 13, 2023
@fpagliughi
Copy link
Contributor

Done. Also for the async client. Re-open this if I missed any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix added A fix has been pushed to the repo and is being tested missing feature A feature of the MQTT protocol that is missing from the library
Projects
None yet
Development

No branches or pull requests

3 participants