[209] | 1 | #include "PubSubClient.h"
|
---|
| 2 | #include "ShimClient.h"
|
---|
| 3 | #include "Buffer.h"
|
---|
| 4 | #include "BDDTest.h"
|
---|
| 5 | #include "trace.h"
|
---|
| 6 | #include <unistd.h>
|
---|
| 7 |
|
---|
| 8 | byte server[] = { 172, 16, 0, 2 };
|
---|
| 9 |
|
---|
| 10 | void callback(char* topic, byte* payload, unsigned int length) {
|
---|
| 11 | // handle message arrived
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | int test_keepalive_pings_idle() {
|
---|
| 16 | IT("keeps an idle connection alive (takes 1 minute)");
|
---|
| 17 |
|
---|
| 18 | ShimClient shimClient;
|
---|
| 19 | shimClient.setAllowConnect(true);
|
---|
| 20 |
|
---|
| 21 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
| 22 | shimClient.respond(connack,4);
|
---|
| 23 |
|
---|
| 24 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
| 25 | int rc = client.connect((char*)"client_test1");
|
---|
| 26 | IS_TRUE(rc);
|
---|
| 27 |
|
---|
| 28 | byte pingreq[] = { 0xC0,0x0 };
|
---|
| 29 | shimClient.expect(pingreq,2);
|
---|
| 30 | byte pingresp[] = { 0xD0,0x0 };
|
---|
| 31 | shimClient.respond(pingresp,2);
|
---|
| 32 |
|
---|
| 33 | for (int i = 0; i < 50; i++) {
|
---|
| 34 | sleep(1);
|
---|
| 35 | if ( i == 15 || i == 31 || i == 47) {
|
---|
| 36 | shimClient.expect(pingreq,2);
|
---|
| 37 | shimClient.respond(pingresp,2);
|
---|
| 38 | }
|
---|
| 39 | rc = client.loop();
|
---|
| 40 | IS_TRUE(rc);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | IS_FALSE(shimClient.error());
|
---|
| 44 |
|
---|
| 45 | END_IT
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | int test_keepalive_pings_with_outbound_qos0() {
|
---|
| 49 | IT("keeps a connection alive that only sends qos0 (takes 1 minute)");
|
---|
| 50 |
|
---|
| 51 | ShimClient shimClient;
|
---|
| 52 | shimClient.setAllowConnect(true);
|
---|
| 53 |
|
---|
| 54 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
| 55 | shimClient.respond(connack,4);
|
---|
| 56 |
|
---|
| 57 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
| 58 | int rc = client.connect((char*)"client_test1");
|
---|
| 59 | IS_TRUE(rc);
|
---|
| 60 |
|
---|
| 61 | byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
|
---|
| 62 |
|
---|
| 63 | for (int i = 0; i < 50; i++) {
|
---|
| 64 | TRACE(i<<":");
|
---|
| 65 | shimClient.expect(publish,16);
|
---|
| 66 | rc = client.publish((char*)"topic",(char*)"payload");
|
---|
| 67 | IS_TRUE(rc);
|
---|
| 68 | IS_FALSE(shimClient.error());
|
---|
| 69 | sleep(1);
|
---|
| 70 | if ( i == 15 || i == 31 || i == 47) {
|
---|
| 71 | byte pingreq[] = { 0xC0,0x0 };
|
---|
| 72 | shimClient.expect(pingreq,2);
|
---|
| 73 | byte pingresp[] = { 0xD0,0x0 };
|
---|
| 74 | shimClient.respond(pingresp,2);
|
---|
| 75 | }
|
---|
| 76 | rc = client.loop();
|
---|
| 77 | IS_TRUE(rc);
|
---|
| 78 | IS_FALSE(shimClient.error());
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | END_IT
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | int test_keepalive_pings_with_inbound_qos0() {
|
---|
| 85 | IT("keeps a connection alive that only receives qos0 (takes 1 minute)");
|
---|
| 86 |
|
---|
| 87 | ShimClient shimClient;
|
---|
| 88 | shimClient.setAllowConnect(true);
|
---|
| 89 |
|
---|
| 90 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
| 91 | shimClient.respond(connack,4);
|
---|
| 92 |
|
---|
| 93 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
| 94 | int rc = client.connect((char*)"client_test1");
|
---|
| 95 | IS_TRUE(rc);
|
---|
| 96 |
|
---|
| 97 | byte publish[] = {0x30,0xe,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
|
---|
| 98 |
|
---|
| 99 | for (int i = 0; i < 50; i++) {
|
---|
| 100 | TRACE(i<<":");
|
---|
| 101 | sleep(1);
|
---|
| 102 | if ( i == 15 || i == 31 || i == 47) {
|
---|
| 103 | byte pingreq[] = { 0xC0,0x0 };
|
---|
| 104 | shimClient.expect(pingreq,2);
|
---|
| 105 | byte pingresp[] = { 0xD0,0x0 };
|
---|
| 106 | shimClient.respond(pingresp,2);
|
---|
| 107 | }
|
---|
| 108 | shimClient.respond(publish,16);
|
---|
| 109 | rc = client.loop();
|
---|
| 110 | IS_TRUE(rc);
|
---|
| 111 | IS_FALSE(shimClient.error());
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | END_IT
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | int test_keepalive_no_pings_inbound_qos1() {
|
---|
| 118 | IT("does not send pings for connections with inbound qos1 (takes 1 minute)");
|
---|
| 119 |
|
---|
| 120 | ShimClient shimClient;
|
---|
| 121 | shimClient.setAllowConnect(true);
|
---|
| 122 |
|
---|
| 123 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
| 124 | shimClient.respond(connack,4);
|
---|
| 125 |
|
---|
| 126 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
| 127 | int rc = client.connect((char*)"client_test1");
|
---|
| 128 | IS_TRUE(rc);
|
---|
| 129 |
|
---|
| 130 | byte publish[] = {0x32,0x10,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,0x12,0x34,0x70,0x61,0x79,0x6c,0x6f,0x61,0x64};
|
---|
| 131 | byte puback[] = {0x40,0x2,0x12,0x34};
|
---|
| 132 |
|
---|
| 133 | for (int i = 0; i < 50; i++) {
|
---|
| 134 | shimClient.respond(publish,18);
|
---|
| 135 | shimClient.expect(puback,4);
|
---|
| 136 | sleep(1);
|
---|
| 137 | rc = client.loop();
|
---|
| 138 | IS_TRUE(rc);
|
---|
| 139 | IS_FALSE(shimClient.error());
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | END_IT
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | int test_keepalive_disconnects_hung() {
|
---|
| 146 | IT("disconnects a hung connection (takes 30 seconds)");
|
---|
| 147 |
|
---|
| 148 | ShimClient shimClient;
|
---|
| 149 | shimClient.setAllowConnect(true);
|
---|
| 150 |
|
---|
| 151 | byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
---|
| 152 | shimClient.respond(connack,4);
|
---|
| 153 |
|
---|
| 154 | PubSubClient client(server, 1883, callback, shimClient);
|
---|
| 155 | int rc = client.connect((char*)"client_test1");
|
---|
| 156 | IS_TRUE(rc);
|
---|
| 157 |
|
---|
| 158 | byte pingreq[] = { 0xC0,0x0 };
|
---|
| 159 | shimClient.expect(pingreq,2);
|
---|
| 160 |
|
---|
| 161 | for (int i = 0; i < 32; i++) {
|
---|
| 162 | sleep(1);
|
---|
| 163 | rc = client.loop();
|
---|
| 164 | }
|
---|
| 165 | IS_FALSE(rc);
|
---|
| 166 |
|
---|
| 167 | int state = client.state();
|
---|
| 168 | IS_TRUE(state == MQTT_CONNECTION_TIMEOUT);
|
---|
| 169 |
|
---|
| 170 | IS_FALSE(shimClient.error());
|
---|
| 171 |
|
---|
| 172 | END_IT
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | int main()
|
---|
| 176 | {
|
---|
| 177 | SUITE("Keep-alive");
|
---|
| 178 | test_keepalive_pings_idle();
|
---|
| 179 | test_keepalive_pings_with_outbound_qos0();
|
---|
| 180 | test_keepalive_pings_with_inbound_qos0();
|
---|
| 181 | test_keepalive_no_pings_inbound_qos1();
|
---|
| 182 | test_keepalive_disconnects_hung();
|
---|
| 183 |
|
---|
| 184 | FINISH
|
---|
| 185 | }
|
---|