Skip to content

Commit aabc24e

Browse files
authored
Use timebomb allocator from aws-c-common (#97)
1 parent 9797b1d commit aabc24e

File tree

1 file changed

+5
-39
lines changed

1 file changed

+5
-39
lines changed

tests/test_websocket_bootstrap.c

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <aws/http/request_response.h>
2121
#include <aws/io/logging.h>
2222
#include <aws/io/uri.h>
23-
#include <aws/testing/aws_test_harness.h>
23+
#include <aws/testing/aws_test_allocators.h>
2424

2525
#if _MSC_VER
2626
# pragma warning(disable : 4204) /* non-constant aggregate initializer */
@@ -563,46 +563,12 @@ TEST_CASE(websocket_boot_report_unexpected_http_shutdown) {
563563
return s_websocket_boot_fail_at_step_test(allocator, ctx, BOOT_STEP_HTTP_SHUTDOWN);
564564
}
565565

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-
592566
/* Run connection process with an allocator that fakes running out of memory after N allocations. */
593567
TEST_CASE(websocket_boot_fail_because_oom) {
594568
(void)ctx;
595569

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));
606572

607573
/* Only use the timebomb allocator with actual the tester, not the logger or other systems. */
608574
s_tester.alloc = &timebomb_alloc;
@@ -615,7 +581,7 @@ TEST_CASE(websocket_boot_fail_because_oom) {
615581
const int max_tries = 10000;
616582
int timer;
617583
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);
619585

620586
int websocket_connect_error_code;
621587
ASSERT_SUCCESS(s_drive_websocket_connect(&websocket_connect_error_code));
@@ -634,7 +600,7 @@ TEST_CASE(websocket_boot_fail_because_oom) {
634600
ASSERT_TRUE(timer >= 2); /* Assert that we actually did fail a few times */
635601

636602
ASSERT_SUCCESS(s_tester_clean_up());
637-
603+
aws_timebomb_allocator_clean_up(&timebomb_alloc);
638604
return AWS_OP_SUCCESS;
639605
}
640606

0 commit comments

Comments
 (0)