20
20
#include <aws/http/request_response.h>
21
21
#include <aws/io/logging.h>
22
22
#include <aws/io/uri.h>
23
- #include <aws/testing/aws_test_harness .h>
23
+ #include <aws/testing/aws_test_allocators .h>
24
24
25
25
#if _MSC_VER
26
26
# pragma warning(disable : 4204) /* non-constant aggregate initializer */
@@ -563,46 +563,12 @@ TEST_CASE(websocket_boot_report_unexpected_http_shutdown) {
563
563
return s_websocket_boot_fail_at_step_test (allocator , ctx , BOOT_STEP_HTTP_SHUTDOWN );
564
564
}
565
565
566
- #define TIMEBOMB_MAX_TIMER 0xFFFF
567
-
568
- struct timebomb_impl {
569
- struct aws_allocator * real_allocator ;
570
- struct aws_atomic_var timer ;
571
- };
572
-
573
- static void * s_timebomb_mem_acquire (struct aws_allocator * alloc , size_t size ) {
574
- struct timebomb_impl * timebomb = alloc -> impl ;
575
-
576
- /* ALL allocations should fail after timer reaches zero.
577
- * But timer is unsigned atomic.
578
- * Therefore, when timer wraps around 0 and becomes huge, continue failing. */
579
- size_t timer = aws_atomic_fetch_sub (& timebomb -> timer , 1 );
580
- if (timer == 0 || timer > TIMEBOMB_MAX_TIMER ) {
581
- return NULL ;
582
- }
583
-
584
- return timebomb -> real_allocator -> mem_acquire (timebomb -> real_allocator , size );
585
- }
586
-
587
- static void s_timebomb_mem_release (struct aws_allocator * alloc , void * ptr ) {
588
- struct timebomb_impl * timebomb = alloc -> impl ;
589
- timebomb -> real_allocator -> mem_release (timebomb -> real_allocator , ptr );
590
- }
591
-
592
566
/* Run connection process with an allocator that fakes running out of memory after N allocations. */
593
567
TEST_CASE (websocket_boot_fail_because_oom ) {
594
568
(void )ctx ;
595
569
596
- struct timebomb_impl timebomb_impl = {
597
- .real_allocator = allocator ,
598
- };
599
- aws_atomic_init_int (& timebomb_impl .timer , TIMEBOMB_MAX_TIMER );
600
-
601
- struct aws_allocator timebomb_alloc = {
602
- .mem_acquire = s_timebomb_mem_acquire ,
603
- .mem_release = s_timebomb_mem_release ,
604
- .impl = & timebomb_impl ,
605
- };
570
+ struct aws_allocator timebomb_alloc ;
571
+ ASSERT_SUCCESS (aws_timebomb_allocator_init (& timebomb_alloc , allocator , SIZE_MAX ));
606
572
607
573
/* Only use the timebomb allocator with actual the tester, not the logger or other systems. */
608
574
s_tester .alloc = & timebomb_alloc ;
@@ -615,7 +581,7 @@ TEST_CASE(websocket_boot_fail_because_oom) {
615
581
const int max_tries = 10000 ;
616
582
int timer ;
617
583
for (timer = 0 ; timer < max_tries ; ++ timer ) {
618
- aws_atomic_store_int ( & timebomb_impl . timer , timer );
584
+ aws_timebomb_allocator_reset_countdown ( & timebomb_alloc , timer );
619
585
620
586
int websocket_connect_error_code ;
621
587
ASSERT_SUCCESS (s_drive_websocket_connect (& websocket_connect_error_code ));
@@ -634,7 +600,7 @@ TEST_CASE(websocket_boot_fail_because_oom) {
634
600
ASSERT_TRUE (timer >= 2 ); /* Assert that we actually did fail a few times */
635
601
636
602
ASSERT_SUCCESS (s_tester_clean_up ());
637
-
603
+ aws_timebomb_allocator_clean_up ( & timebomb_alloc );
638
604
return AWS_OP_SUCCESS ;
639
605
}
640
606
0 commit comments