Skip to content

Commit 9c9dc9f

Browse files
SeppoTakalommahadevan108
authored andcommitted
tests: coap_client: Add tests for poll() errors
If we receive poll() error during a request, it must be forwarded to application. If instead receive poll() error later, it should not be forwarded as it might be result of application closing the socket. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent 350d20e commit 9c9dc9f

File tree

1 file changed

+61
-0
lines changed
  • tests/net/lib/coap_client/src

1 file changed

+61
-0
lines changed

tests/net/lib/coap_client/src/main.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ static void test_setup(void *data)
322322

323323
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake;
324324
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake;
325+
clear_socket_events(ZSOCK_POLLIN | ZSOCK_POLLOUT | ZSOCK_POLLERR);
325326

326327
for (i = 0; i < ARRAY_SIZE(messages_needing_response); i++) {
327328
messages_needing_response[i] = 0;
@@ -727,5 +728,65 @@ ZTEST(coap_client, test_multiple_clients)
727728
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
728729
zassert_ok(k_sem_take(&sem2, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
729730
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
731+
}
732+
733+
734+
ZTEST(coap_client, test_poll_err)
735+
{
736+
int ret = 0;
737+
struct sockaddr address = {0};
738+
struct coap_client_request client_request = {
739+
.method = COAP_METHOD_GET,
740+
.confirmable = true,
741+
.path = test_path,
742+
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
743+
.cb = coap_callback,
744+
.payload = short_payload,
745+
.len = strlen(short_payload),
746+
};
747+
748+
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
749+
set_socket_events(ZSOCK_POLLERR);
750+
751+
k_sleep(K_MSEC(1));
752+
753+
LOG_INF("Send request");
754+
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
755+
zassert_true(ret >= 0, "Sending request failed, %d", ret);
756+
757+
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
758+
zassert_equal(last_response_code, -EIO, "Unexpected response");
759+
}
760+
761+
ZTEST(coap_client, test_poll_err_after_response)
762+
{
763+
int ret = 0;
764+
struct k_sem sem1;
765+
struct sockaddr address = {0};
766+
struct coap_client_request client_request = {
767+
.method = COAP_METHOD_GET,
768+
.confirmable = true,
769+
.path = test_path,
770+
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
771+
.cb = coap_callback,
772+
.payload = short_payload,
773+
.len = strlen(short_payload),
774+
.user_data = &sem1
775+
};
776+
777+
zassert_ok(k_sem_init(&sem1, 0, 1));
778+
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
779+
set_socket_events(ZSOCK_POLLIN);
780+
781+
k_sleep(K_MSEC(1));
782+
783+
LOG_INF("Send request");
784+
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
785+
zassert_true(ret >= 0, "Sending request failed, %d", ret);
786+
787+
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
788+
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
730789

790+
set_socket_events(ZSOCK_POLLERR);
791+
zassert_not_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
731792
}

0 commit comments

Comments
 (0)