From 5adc75c088061af6c27e07cd45d1b1fff46b0455 Mon Sep 17 00:00:00 2001 From: lyon Date: Sun, 13 Nov 2022 14:52:39 +0800 Subject: [PATCH] test mqtt-lease-free --- package/mqtt/_mqtt__MQTT.c | 1 + package/mqtt/mqttclient.c | 6 ++ package/mqtt/mqttclient.h | 1 + port/linux/.vscode/launch.json | 2 +- .../pikascript-lib/mqtt/_mqtt__MQTT.c | 1 + .../pikascript-lib/mqtt/mqttclient.c | 6 ++ .../pikascript-lib/mqtt/mqttclient.h | 1 + test/mqtt-test.cpp | 86 +++++++++++++++++++ 8 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 test/mqtt-test.cpp diff --git a/package/mqtt/_mqtt__MQTT.c b/package/mqtt/_mqtt__MQTT.c index 89f060881..8c0cf91c1 100644 --- a/package/mqtt/_mqtt__MQTT.c +++ b/package/mqtt/_mqtt__MQTT.c @@ -1,4 +1,5 @@ #include "_mqtt__MQTT.h" +#include "mqttclient.h" void _mqtt__MQTT___init__(PikaObj* self, char* ip, diff --git a/package/mqtt/mqttclient.c b/package/mqtt/mqttclient.c index 7eab9894e..7e9d614e6 100644 --- a/package/mqtt/mqttclient.c +++ b/package/mqtt/mqttclient.c @@ -1498,3 +1498,9 @@ int mqtt_set_will_options(mqtt_client_t* c, char *topic, mqtt_qos_t qos, uint8_t RETURN_ERROR(MQTT_SUCCESS_ERROR); } + +int mqtt_free(mqtt_client_t* c){ + mqtt_clean_session(c); + mqtt_release(c); + platform_memory_free(c); +} diff --git a/package/mqtt/mqttclient.h b/package/mqtt/mqttclient.h index 55cd3c2d9..6c04a06a0 100644 --- a/package/mqtt/mqttclient.h +++ b/package/mqtt/mqttclient.h @@ -163,6 +163,7 @@ MQTT_CLIENT_SET_STATEMENT(interceptor_handler, interceptor_handler_t) void mqtt_sleep_ms(int ms); mqtt_client_t *mqtt_lease(void); +int mqtt_free(mqtt_client_t* c); int mqtt_release(mqtt_client_t* c); int mqtt_connect(mqtt_client_t* c); int mqtt_disconnect(mqtt_client_t* c); diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 703c702a7..d88f2c6e9 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - "--gtest_filter=vm.class_keyword_mqtt" + // "--gtest_filter=vm.class_keyword_mqtt" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/_mqtt__MQTT.c b/port/linux/package/pikascript/pikascript-lib/mqtt/_mqtt__MQTT.c index 89f060881..8c0cf91c1 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/_mqtt__MQTT.c +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/_mqtt__MQTT.c @@ -1,4 +1,5 @@ #include "_mqtt__MQTT.h" +#include "mqttclient.h" void _mqtt__MQTT___init__(PikaObj* self, char* ip, diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.c b/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.c index 7eab9894e..7e9d614e6 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.c +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.c @@ -1498,3 +1498,9 @@ int mqtt_set_will_options(mqtt_client_t* c, char *topic, mqtt_qos_t qos, uint8_t RETURN_ERROR(MQTT_SUCCESS_ERROR); } + +int mqtt_free(mqtt_client_t* c){ + mqtt_clean_session(c); + mqtt_release(c); + platform_memory_free(c); +} diff --git a/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.h b/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.h index 55cd3c2d9..6c04a06a0 100644 --- a/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.h +++ b/port/linux/package/pikascript/pikascript-lib/mqtt/mqttclient.h @@ -163,6 +163,7 @@ MQTT_CLIENT_SET_STATEMENT(interceptor_handler, interceptor_handler_t) void mqtt_sleep_ms(int ms); mqtt_client_t *mqtt_lease(void); +int mqtt_free(mqtt_client_t* c); int mqtt_release(mqtt_client_t* c); int mqtt_connect(mqtt_client_t* c); int mqtt_disconnect(mqtt_client_t* c); diff --git a/test/mqtt-test.cpp b/test/mqtt-test.cpp new file mode 100644 index 000000000..a51e03d5c --- /dev/null +++ b/test/mqtt-test.cpp @@ -0,0 +1,86 @@ +/* + * @Author: jiejie, lyon + * @Github: https://github.com/jiejieTop + * @Date: 2019-12-11 21:53:07 + * @LastEditTime: 2020-06-08 20:38:41 + * @Description: the code belongs to jiejie, please keep the author information + * and source code according to the license. + */ +#include "test_common.h" +extern "C" { + +#include +#include +#include +#include +#include +#include "../pikascript-lib/mqtt/mqttclient.h" + +// #define TEST_USEING_TLS + +#ifdef TEST_USEING_TLS +static const char* test_baidu_ca_crt = { + "-----BEGIN CERTIFICATE-----\r\n" + "MIIDXzCCAkegAwIBAgILBAAAAAABIVhTCKIwDQYJKoZIhvcNAQELBQAwTDEgMB4G\r\n" + "A1UECxMXR2xvYmFsU2lnbiBSb290IENBIC0gUjMxEzARBgNVBAoTCkdsb2JhbFNp\r\n" + "Z24xEzARBgNVBAMTCkdsb2JhbFNpZ24wHhcNMDkwMzE4MTAwMDAwWhcNMjkwMzE4\r\n" + "MTAwMDAwWjBMMSAwHgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMzETMBEG\r\n" + "A1UEChMKR2xvYmFsU2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjCCASIwDQYJKoZI\r\n" + "hvcNAQEBBQADggEPADCCAQoCggEBAMwldpB5BngiFvXAg7aEyiie/QV2EcWtiHL8\r\n" + "RgJDx7KKnQRfJMsuS+FggkbhUqsMgUdwbN1k0ev1LKMPgj0MK66X17YUhhB5uzsT\r\n" + "gHeMCOFJ0mpiLx9e+pZo34knlTifBtc+ycsmWQ1z3rDI6SYOgxXG71uL0gRgykmm\r\n" + "KPZpO/bLyCiR5Z2KYVc3rHQU3HTgOu5yLy6c+9C7v/U9AOEGM+iCK65TpjoWc4zd\r\n" + "QQ4gOsC0p6Hpsk+QLjJg6VfLuQSSaGjlOCZgdbKfd/+RFO+uIEn8rUAVSNECMWEZ\r\n" + "XriX7613t2Saer9fwRPvm2L7DWzgVGkWqQPabumDk3F2xmmFghcCAwEAAaNCMEAw\r\n" + "DgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFI/wS3+o\r\n" + "LkUkrk1Q+mOai97i3Ru8MA0GCSqGSIb3DQEBCwUAA4IBAQBLQNvAUKr+yAzv95ZU\r\n" + "RUm7lgAJQayzE4aGKAczymvmdLm6AC2upArT9fHxD4q/c2dKg8dEe3jgr25sbwMp\r\n" + "jjM5RcOO5LlXbKr8EpbsU8Yt5CRsuZRj+9xTaGdWPoO4zzUhw8lo/s7awlOqzJCK\r\n" + "6fBdRoyV3XpYKBovHd7NADdBj+1EbddTKJd+82cEHhXXipa0095MJ6RMG3NzdvQX\r\n" + "mcIfeg7jLQitChws/zyrVQ4PkX4268NXSb7hLi18YIvDQVETI53O9zJrlAGomecs\r\n" + "Mx86OyXShkDOOyyGeMlhLxS67ttVb9+E7gUJTb0o2HLO02JQZR7rkpeDMdmztcpH\r\n" + "WD9f\r\n" + "-----END CERTIFICATE-----"}; +#endif + +static void topic1_handler(void* client, message_data_t* msg) { + (void)client; + MQTT_LOG_I( + "----------------------------------------------------------------------" + "-------------"); + MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, + __FUNCTION__, msg->topic_name, (char*)msg->message->payload); + MQTT_LOG_I( + "----------------------------------------------------------------------" + "-------------"); +} + +void* mqtt_publish_thread(void* arg) { + mqtt_client_t* client = (mqtt_client_t*)arg; + + char buf[100] = {0}; + mqtt_message_t msg; + memset(&msg, 0, sizeof(msg)); + sprintf(buf, "welcome to mqttclient, this is a publish test..."); + + sleep(2); + + mqtt_list_subscribe_topic(client); + + msg.payload = (void*)buf; + msg.qos = QOS0; + while (1) { + sprintf(buf, + "welcome to mqttclient, this is a publish test, a rand number: " + "%d ...", + random_number()); + mqtt_publish(client, "topic1", &msg); + sleep(4); + } +} +} + +TEST(mqtt, lease_free) { + mqtt_client_t* client = mqtt_lease(); + mqtt_free(client); +}