} TEST_ENDDEF TEST_DEF(connect_and_send_over_several_channels_in_group_simultaneously) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M1\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test M2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_subscribe_and_check(pbp, NULL, "gr", 5*SECONDS, "\"Test M1\"", "ch", "\"Test M2\"", "two", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(broken_connection_test_group_multichannel_out) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "three", "\"Test 5\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3\"", "\"Test 4\"", "\"Test 5\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test 4 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "three", "\"Test 5 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); printf("Please disconnect from Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_ADDR_RESOLUTION_FAILED, pbp); printf("Please reconnect to Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3 - 2\"", "\"Test 4 - 2\"", "\"Test 5 - 2\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test 4 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "three", "\"Test 5 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3 - 4\"", "\"Test 4 - 4\"", "\"Test 5 - 4\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(connect_disconnect_and_connect_again_combo) { static pubnub_t *pbp; static pubnub_t *pbp_2; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); pbp_2 = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp_2); pubnub_init(pbp_2, g_pubkey, g_keysub); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4\""), PNR_STARTED); pubnub_cancel(pbp); expect_pnr(pubnub_publish(pbp_2, "two", "\"Test M5\""), PNR_STARTED); pubnub_cancel(pbp_2); await_timed_2(5*SECONDS, PNR_CANCELLED, pbp, PNR_CANCELLED, pbp_2); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4 - 2\""), PNR_STARTED); expect_pnr(pubnub_publish(pbp_2, "two", "\"Test M5 - 2\""), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect_pnr(pubnub_subscribe(pbp, "two", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M4 - 2\"", "\"Test M5 - 2\"", NULL)); pubnub_set_non_blocking_io(pbp); expect_pnr(pubnub_subscribe(pbp, "two", "gr"), PNR_STARTED); pubnub_cancel(pbp); await_timed(5*SECONDS, PNR_CANCELLED, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4 - 3\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test M5 - 3\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "two", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M4 - 3\"", "\"Test M5 - 3\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(simple_connect_and_receiver_over_single_channel_in_group) { static pubnub_t *pbp; static pubnub_t *pbp_2; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); pbp_2 = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp_2); pubnub_init(pbp_2, "demo", "demo"); expect_pnr(pubnub_remove_channel_group(pbp_2, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); expect_pnr(pubnub_add_channel_to_group(pbp_2, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp_2, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); pubnub_set_non_blocking_io(pbp_2); expect_pnr(pubnub_subscribe(pbp_2, NULL, "gr"), PNR_STARTED); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 2\""), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp_2, "\"Test 2\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 2 - 2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp_2, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp_2, "\"Test 2 - 2\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(complex_send_and_receive_over_channel_plus_group_simultaneously) { static pubnub_t *pbp; static pubnub_t *pbp_2; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); pbp_2 = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp_2); pubnub_init(pbp_2, g_pubkey, g_keysub); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "two,three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect_pnr(pubnub_publish(pbp, "two", "\"Test M3\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp_2, "ch", "\"Test M3\""), PNR_STARTED); expect_pnr(pubnub_publish(pbp, "three", "\"Test M3 - 1\""), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp, "\"Test M3\"", "\"Test M3 - 1\"", NULL)); expect(pnfntst_got_messages(pbp_2, "\"Test M3\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "two,three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; TEST_POP_DEFERRED; } TEST_ENDDEF
int main() { int option; char opt_string[4]; char channel[PNC_CHANNEL_NAME_SIZE]; char channel_group[PNC_CHANNEL_NAME_SIZE]; char auth_key[PNC_AUTH_KEY_SIZE]; char uuid[PNC_UUID_SIZE + 1]; char state[2048]; pubnub_t *pn = pubnub_alloc(); pubnub_t *pn_sub = pubnub_alloc(); if (NULL == pn || NULL == pn_sub) { printf("Failed to allocate pubnub context"); return -1; } pubnub_init(pn, pubkey, pubkey); pubnub_init(pn_sub, pubkey, pubkey); pubnub_set_uuid(pn, PNC_DEFAULT_UUID); pubnub_set_uuid(pn, PNC_DEFAULT_SUBSCRIBE_UUID); pnc_ops_init(pn, pn_sub); displayMenuOptions(pn); option = 0; while (option != MENU_EXIT) { fgets(opt_string, sizeof(opt_string), stdin); opt_string[strlen(opt_string) - 1] = '\0'; option = atoi(opt_string); printf("You entered %d(%s)\n", option, opt_string); switch (option) { case MENU_DISPLAY: break; case MENU_PUBLISH: { char message[PNC_READ_STRING_SIZE]; bool store = false; // TODO: add json built string pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_read_bool_from_console_optional("Store", &store, true); puts("Enter the message for publish. To exit loop enter Q"); while (fgets(message, PNC_READ_STRING_SIZE, stdin) != NULL && strcmp(message, "Q\n")) { if (strlen(message) == 1) { puts("Invalid input"); continue; } message[strlen(message) - 1] = '\0'; if (message[0] == '{' && message[strlen(message) - 1] == '}') { pnc_ops_parse_response("pubnub_publish()", pubnub_publish(pn, channel, message), pn); } else { char encoded[PNC_READ_STRING_SIZE]; snprintf(encoded, sizeof encoded, "\"%s\"", message); pnc_ops_parse_response("pubnub_publish()", pubnub_publish(pn, channel, encoded), pn); } puts("Enter the message for publish. To exit loop enter Q."); } break; } case MENU_HISTORY: { int count = 100; bool include_token = false; pnc_read_int_from_console_optional("Count", &count, true); if (count <= 0) { count = 100; puts("Count value cannot be less than 1. Default value(100) was applied."); } pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); // WARNING: Default buffer can be not big enough to get 100-items // history response with timetokens pnc_read_bool_from_console("Include timetokens", &include_token); pnc_ops_parse_response("pubnub_history()", pubnub_history(pn, channel, count, include_token), pn); break; } case MENU_HERE_NOW: pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_here_now()", pubnub_here_now(pn, channel, NULL), pn); break; case MENU_TIME: pnc_ops_parse_response("pubnub_time()", pubnub_time(pn), pn); break; case MENU_AUTH: pnc_read_string_from_console("Auth key", auth_key, PNC_AUTH_KEY_SIZE); pubnub_set_auth(pn, auth_key); break; case MENU_UUID: pnc_read_string_from_console("UUID", uuid, PNC_UUID_SIZE); pubnub_set_uuid(pn, uuid); break; case MENU_STATE_GET: pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console_optional("UUID", uuid, PNC_UUID_SIZE, true); if (strlen(uuid) == 0) { strcpy(uuid, pubnub_uuid_get(pn)); } pnc_ops_parse_response("pubnub_get_state()", pubnub_state_get(pn, channel, NULL, uuid), pn); break; case MENU_SET_STATE: // Set State pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console("JSON state without escaping", state, PNC_READ_STRING_SIZE); pnc_ops_parse_response("pubnub_set_state()", pubnub_set_state(pn, channel, NULL, pubnub_uuid_get(pn), state), pn); break; case MENU_WHERE_NOW: pnc_read_string_from_console_optional("UUID", uuid, PNC_UUID_SIZE, true); if (strlen(uuid) == 0) { puts("Using current UUID"); strcpy(uuid, PNC_DEFAULT_UUID); } pnc_ops_parse_response("pubnub_where_now()", pubnub_where_now(pn, uuid), pn); break; case MENU_ADD_PRESENCE_CHANNEL: { char presence_channel[PNC_CHANNEL_NAME_SIZE + sizeof(PNC_PRESENCE_SUFFIX)]; pnc_read_string_from_console("Channel name", channel, PNC_CHANNEL_NAME_SIZE); strcpy(presence_channel, channel); strcat(presence_channel, PNC_PRESENCE_SUFFIX); pnc_subscribe_add_channel(presence_channel); break; } case MENU_SUBSCRIBE: pnc_ops_subscribe(pn_sub); break; case MENU_UNSUBSCRIBE: pnc_ops_unsubscribe(pn_sub); break; case MENU_SUBSCRIPTION_ADD_CHANNEL: pnc_read_string_from_console("Enter Channel name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_add_channel(channel); break; case MENU_SUBSCRIPTION_ADD_GROUP: pnc_read_string_from_console("Enter Group name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_add_channel_group(channel); break; case MENU_SUBSCRIPTION_REMOVE_CHANNEL: pnc_read_string_from_console("Enter Channel name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_remove_channel(channel); break; case MENU_SUBSCRIPTION_REMOVE_GROUP: pnc_read_string_from_console("Enter Group name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_remove_channel_group(channel); break; case MENU_GROUP_ADD_CHANNEL: pnc_read_string_from_console("Group name", channel_group, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console("Channel to add", channel, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_add_channel_to_group()", pubnub_add_channel_to_group(pn, channel, channel_group), pn); break; case MENU_GROUP_REMOVE_CHANNEL: pnc_read_string_from_console("Group name", channel_group, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console("Channel to remove", channel, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_remove_channel_from_group()", pubnub_remove_channel_from_group(pn, channel, channel_group), pn); break; case MENU_GROUP_LIST_CHANNELS: pnc_read_string_from_console("Group to list", channel_group, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_list_channel_group()", pubnub_list_channel_group(pn, channel_group), pn); break; case MENU_GROUP_REMOVE_GROUP: pnc_read_string_from_console("Group to remove", channel, PNC_CHANNEL_NAME_SIZE); pubnub_remove_channel_group(pn, channel); break; default: printf("Invalid input: %d\n", option); break; } printf("\n================================\n\n"); displayMenuOptions(pn); } puts("Exiting"); if (pubnub_free(pn) != 0) { puts("Failed to free the Pubnub context"); } return 0; }