GeezerVison is showing github.com — Add wpcp_get_subscription_count() and wpcp_iterate_subscriptions() · WebProcessControl/libwpcp@749782b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: include/wpcp.h +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -110,6 +110,7 @@ typedef void(WPCP_CDECL *wpcp_unsubscribe_cb_t)(void* user, struct wpcp_result_t | 110 | 110 | typedef void(WPCP_CDECL *wpcp_unsubscribe_ex_cb_t)(void* user, struct wpcp_result_t* result, struct wpcp_subscription_t* subscription, void** context, uint32_t remaining); | 111 | 111 | typedef void(WPCP_CDECL *wpcp_republish_cb_t)(void* user, struct wpcp_publish_handle_t* publish_handle, struct wpcp_subscription_t* subscription); | 112 | 112 | typedef void(WPCP_CDECL *wpcp_republish_ex_cb_t)(void* user, struct wpcp_publish_handle_t* publish_handle, struct wpcp_subscription_t* subscription, void** context, uint32_t remaining); | | 113 | +typedef void(WPCP_CDECL *wpcp_iterate_subscriptions_cb_t)(void* user, struct wpcp_subscription_t* subscription, struct wpcp_publish_handle_t* publish_handle); | 113 | 114 |
| 114 | 115 | union wpcp_has_out_message_callback_t { | 115 | 116 | wpcp_has_out_message_cb_t cb; | @@ -202,6 +203,10 @@ WPCP_EXPORT void wpcp_subscription_set_user(struct wpcp_subscription_t* subscrip | 202 | 203 |
| 203 | 204 | WPCP_EXPORT size_t wpcp_subscription_get_usage_count(struct wpcp_subscription_t* subscription); | 204 | 205 |
| | 206 | +WPCP_EXPORT size_t wpcp_get_subscription_count(struct wpcp_t* wpcp); | | 207 | + | | 208 | +WPCP_EXPORT void wpcp_iterate_subscriptions(struct wpcp_t* wpcp, void* user, wpcp_iterate_subscriptions_cb_t callback); | | 209 | + | 205 | 210 | WPCP_EXPORT void wpcp_return_read_data(struct wpcp_result_t* result, const struct wpcp_value_t* diagnostic_info, const struct wpcp_value_t* value, double timestamp, uint32_t status, const struct wpcp_key_value_pair_t* additional, uint32_t additional_count); | 206 | 211 |
| 207 | 212 | WPCP_EXPORT void wpcp_return_write_data(struct wpcp_result_t* result, const struct wpcp_value_t* diagnostic_info, bool success); | |
Collapse file Copy file name to clipboardExpand all lines: src/wpcp.c +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -206,6 +206,23 @@ static bool wpcp_session_handle_unsubscribe_message(struct wpcp_session_t* sessi | 206 | 206 | return ret; | 207 | 207 | } | 208 | 208 |
| | 209 | +size_t wpcp_get_subscription_count(struct wpcp_t* wpcp) | | 210 | +{ | | 211 | + struct wpcp_internal_t* wpcp_internal = (struct wpcp_internal_t*) wpcp; | | 212 | + return wpcp_internal->subscription_count; | | 213 | +} | | 214 | + | | 215 | +void wpcp_iterate_subscriptions(struct wpcp_t* wpcp, void* user, wpcp_iterate_subscriptions_cb_t callback) | | 216 | +{ | | 217 | + size_t i; | | 218 | + struct wpcp_internal_t* wpcp_internal = (struct wpcp_internal_t*) wpcp; | | 219 | + | | 220 | + for (i = 0; i < wpcp_internal->subscription_count; ++i) { | | 221 | + struct wpcp_subscription_t* subscription = wpcp_internal->subscriptions[i]; | | 222 | + callback(user, subscription, &subscription->publish_handle); | | 223 | + } | | 224 | +} | | 225 | + | 209 | 226 | void wpcp_return_unsubscribe(struct wpcp_result_t* result, const struct wpcp_value_t* diagnostic_info, struct wpcp_subscription_t* subscription) | 210 | 227 | { | 211 | 228 | if (subscription) | |
Collapse file Copy file name to clipboardExpand all lines: test/test_session.c +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -201,6 +201,63 @@ START_TEST(subscription_type) | 201 | 201 | } | 202 | 202 | END_TEST | 203 | 203 |
| | 204 | +START_TEST(get_subscription_count) | | 205 | +{ | | 206 | + ck_assert_int_eq(wpcp_get_subscription_count(&g_wpcp_internal.wpcp), 0); | | 207 | + | | 208 | + struct wpcp_subscription_t* subsa = wpcp_request_subscription(&g_wpcp_internal, 0, NULL, 0); | | 209 | + ck_assert_int_eq(wpcp_get_subscription_count(&g_wpcp_internal.wpcp), 1); | | 210 | + | | 211 | + struct wpcp_subscription_t* subsb = wpcp_request_subscription(&g_wpcp_internal, 1, NULL, 0); | | 212 | + ck_assert_int_eq(wpcp_get_subscription_count(&g_wpcp_internal.wpcp), 2); | | 213 | + | | 214 | + wpcp_release_subscription(&g_wpcp_internal, subsb); | | 215 | + ck_assert_int_eq(wpcp_get_subscription_count(&g_wpcp_internal.wpcp), 1); | | 216 | + | | 217 | + wpcp_release_subscription(&g_wpcp_internal, subsa); | | 218 | + ck_assert_int_eq(wpcp_get_subscription_count(&g_wpcp_internal.wpcp), 0); | | 219 | +} | | 220 | +END_TEST | | 221 | + | | 222 | +struct test_session_iterate_subscriptions_t { | | 223 | + size_t counter; | | 224 | + struct wpcp_subscription_t* subscriptions[2]; | | 225 | +}; | | 226 | + | | 227 | +void test_session_iterate_subscriptions_callback(void* user, struct wpcp_subscription_t* subscription, struct wpcp_publish_handle_t* publish_handle) | | 228 | +{ | | 229 | + struct test_session_iterate_subscriptions_t* helper = (struct test_session_iterate_subscriptions_t*) user; | | 230 | + ck_assert_ptr_eq(subscription, helper->subscriptions[helper->counter]); | | 231 | + ck_assert_ptr_eq(publish_handle, &helper->subscriptions[helper->counter++]->publish_handle); | | 232 | +} | | 233 | + | | 234 | +START_TEST(iterate_subscriptions) | | 235 | +{ | | 236 | + struct test_session_iterate_subscriptions_t helper; | | 237 | + memset(&helper, 0, sizeof(helper)); | | 238 | + | | 239 | + wpcp_iterate_subscriptions(&g_wpcp_internal.wpcp, &helper, test_session_iterate_subscriptions_callback); | | 240 | + ck_assert_int_eq(helper.counter, 0); | | 241 | + | | 242 | + helper.counter = 0; | | 243 | + helper.subscriptions[0] = wpcp_request_subscription(&g_wpcp_internal, 0, NULL, 0); | | 244 | + wpcp_iterate_subscriptions(&g_wpcp_internal.wpcp, &helper, test_session_iterate_subscriptions_callback); | | 245 | + ck_assert_int_eq(helper.counter, 1); | | 246 | + | | 247 | + helper.counter = 0; | | 248 | + helper.subscriptions[1] = wpcp_request_subscription(&g_wpcp_internal, 1, NULL, 0); | | 249 | + wpcp_iterate_subscriptions(&g_wpcp_internal.wpcp, &helper, test_session_iterate_subscriptions_callback); | | 250 | + ck_assert_int_eq(helper.counter, 2); | | 251 | + | | 252 | + wpcp_release_subscription(&g_wpcp_internal, helper.subscriptions[0]); | | 253 | + wpcp_release_subscription(&g_wpcp_internal, helper.subscriptions[1]); | | 254 | + | | 255 | + helper.counter = 0; | | 256 | + wpcp_iterate_subscriptions(&g_wpcp_internal.wpcp, &helper, test_session_iterate_subscriptions_callback); | | 257 | + ck_assert_int_eq(helper.counter, 0); | | 258 | +} | | 259 | +END_TEST | | 260 | + | 204 | 261 | START_TEST(subscription_publish_item) | 205 | 262 | { | 206 | 263 | uint32_t id1 = wpcp_session_get_next_free_publish_id(g_session); | @@ -874,6 +931,8 @@ TCase* testcase_session(void) | 874 | 931 | tcase_add_test (ret, subscription_max_publish_item); | 875 | 932 | tcase_add_test (ret, subscription_release_publish_item_via_session); | 876 | 933 | tcase_add_test (ret, subscription_publish_item_malloc_fail); | | 934 | + tcase_add_test (ret, get_subscription_count); | | 935 | + tcase_add_test (ret, iterate_subscriptions); | 877 | 936 | tcase_add_test (ret, session_out_message); | 878 | 937 | tcase_add_test (ret, session_out_message_malloc_fail); | 879 | 938 | tcase_add_test (ret, subscription_publish_item_publish); | |
|