@@ -220,6 +220,22 @@ static ssize_t z_impl_zsock_sendto_custom_fake_echo_next_req(int sock, void *buf
220220 return 1 ;
221221}
222222
223+ static ssize_t z_impl_zsock_sendto_custom_fake_block (int sock , void * buf , size_t len , int flags ,
224+ const struct sockaddr * dest_addr ,
225+ socklen_t addrlen )
226+ {
227+ errno = EAGAIN ;
228+ return -1 ;
229+ }
230+
231+ static ssize_t z_impl_zsock_sendto_custom_fake_err (int sock , void * buf , size_t len , int flags ,
232+ const struct sockaddr * dest_addr ,
233+ socklen_t addrlen )
234+ {
235+ errno = ENETDOWN ;
236+ return -1 ;
237+ }
238+
223239static ssize_t z_impl_zsock_recvfrom_custom_fake_response (int sock , void * buf , size_t max_len ,
224240 int flags , struct sockaddr * src_addr ,
225241 socklen_t * addrlen )
@@ -452,6 +468,30 @@ ZTEST(coap_client, test_get_request)
452468 LOG_INF ("Test done" );
453469}
454470
471+ ZTEST (coap_client , test_request_block )
472+ {
473+ int ret = 0 ;
474+ struct sockaddr address = {0 };
475+ struct coap_client_request client_request = {
476+ .method = COAP_METHOD_GET ,
477+ .confirmable = true,
478+ .path = test_path ,
479+ .fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN ,
480+ .cb = coap_callback ,
481+ .payload = short_payload ,
482+ .len = strlen (short_payload ),
483+ };
484+
485+ z_impl_zsock_sendto_fake .custom_fake = z_impl_zsock_sendto_custom_fake_block ;
486+
487+ k_sleep (K_MSEC (1 ));
488+
489+ LOG_INF ("Send request" );
490+ ret = coap_client_req (& client , 0 , & address , & client_request , NULL );
491+ zassert_equal (ret , - EAGAIN , "" );
492+ }
493+
494+
455495ZTEST (coap_client , test_resend_request )
456496{
457497 int ret = 0 ;
@@ -462,26 +502,29 @@ ZTEST(coap_client, test_resend_request)
462502 .path = test_path ,
463503 .fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN ,
464504 .cb = coap_callback ,
465- .payload = NULL ,
466- .len = 0
505+ .payload = short_payload ,
506+ .len = strlen ( short_payload ),
467507 };
468508
469- client_request .payload = short_payload ;
470- client_request .len = strlen (short_payload );
509+ int (* sendto_fakes [])(int , void * , size_t , int , const struct sockaddr * , socklen_t ) = {
510+ z_impl_zsock_sendto_custom_fake_no_reply ,
511+ z_impl_zsock_sendto_custom_fake_block ,
512+ z_impl_zsock_sendto_custom_fake ,
513+ };
471514
472- z_impl_zsock_sendto_fake .custom_fake = z_impl_zsock_sendto_custom_fake_no_reply ;
515+ SET_CUSTOM_FAKE_SEQ (z_impl_zsock_sendto , sendto_fakes , ARRAY_SIZE (sendto_fakes ));
516+ set_socket_events (client .fd , ZSOCK_POLLOUT );
473517
474518 k_sleep (K_MSEC (1 ));
475519
476520 LOG_INF ("Send request" );
477521 ret = coap_client_req (& client , 0 , & address , & client_request , NULL );
478522 zassert_true (ret >= 0 , "Sending request failed, %d" , ret );
479523 k_sleep (K_MSEC (MORE_THAN_ACK_TIMEOUT_MS ));
480- set_socket_events (client .fd , ZSOCK_POLLIN | ZSOCK_POLLOUT );
481524
482525 k_sleep (K_MSEC (MORE_THAN_EXCHANGE_LIFETIME_MS ));
483526 zassert_equal (last_response_code , COAP_RESPONSE_CODE_OK , "Unexpected response" );
484- zassert_equal (z_impl_zsock_sendto_fake .call_count , 2 );
527+ zassert_equal (z_impl_zsock_sendto_fake .call_count , 3 );
485528 LOG_INF ("Test done" );
486529}
487530
0 commit comments