authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2019-12-08 21:22:07-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2019-12-08 21:22:07-06:00
log608d36ad8c910c9a9c112f3d0c047655aa2f91e8
treea964181cde8120c0302b9b6c1f5bf6efece10b2c
parente91522b875b72cc3990fd4086a331ab63ac70dc8

Rewrite WasmPageAllocator tests to be less flaky on environment


1 files changed, 32 insertions(+), 27 deletions(-)

lib/std/heap.zig+32-27
......@@ -804,33 +804,38 @@ test "c_allocator" {
804804
805805test "WasmPageAllocator internals" {
806806 if (comptime std.Target.current.isWasm()) {
807 const none_free = WasmPageAllocator.PageStatus.none_free;
808 std.debug.assert(none_free == WasmPageAllocator.conventional.data[0]); // Passes if this test runs first
809 std.debug.assert(!WasmPageAllocator.extended.isInitialized()); // Passes if this test runs first
810
811 const tmp = try page_allocator.alloc(u8, 1);
812 testing.expect(none_free == WasmPageAllocator.conventional.data[0]);
813 page_allocator.free(tmp);
814 testing.expect(none_free < WasmPageAllocator.conventional.data[0]);
815
816 const a_little_free = WasmPageAllocator.conventional.data[0];
817 const tmp_large = try page_allocator.alloc(u8, std.mem.page_size + 1);
818 testing.expect(a_little_free == WasmPageAllocator.conventional.data[0]);
819 const tmp_small = try page_allocator.alloc(u8, 1);
820 testing.expect(none_free == WasmPageAllocator.conventional.data[0]);
821
822 page_allocator.free(tmp_small);
823 testing.expect(a_little_free == WasmPageAllocator.conventional.data[0]);
824 page_allocator.free(tmp_large);
825 testing.expect(a_little_free < WasmPageAllocator.conventional.data[0]);
826
827 const more_free = WasmPageAllocator.conventional.data[0];
828 const supersize = try page_allocator.alloc(u8, std.mem.page_size * (WasmPageAllocator.conventional.totalPages() + 1));
829 testing.expect(more_free == WasmPageAllocator.conventional.data[0]);
830 testing.expect(!WasmPageAllocator.extended.isInitialized());
831 page_allocator.free(supersize);
832 testing.expect(WasmPageAllocator.extended.isInitialized());
833 testing.expect(more_free < WasmPageAllocator.conventional.data[0]);
807 const conventional_memsize = WasmPageAllocator.conventional.totalPages() * std.mem.page_size;
808 const initial = try page_allocator.alloc(u8, std.mem.page_size);
809 std.debug.assert(@ptrToInt(initial.ptr) < conventional_memsize); // If this isn't conventional, the rest of these tests don't make sense. Also we have a serious memory leak in the test suite.
810
811 var inplace = try page_allocator.realloc(initial, 1);
812 testing.expectEqual(initial.ptr, inplace.ptr);
813 inplace = try page_allocator.realloc(inplace, 4);
814 testing.expectEqual(initial.ptr, inplace.ptr);
815 page_allocator.free(inplace);
816
817 const reuse = try page_allocator.alloc(u8, 1);
818 testing.expectEqual(initial.ptr, reuse.ptr);
819 page_allocator.free(reuse);
820
821 // This segment may span conventional and extended which has really complex rules so we're just ignoring it for now.
822 const padding = try page_allocator.alloc(u8, conventional_memsize);
823 page_allocator.free(padding);
824
825 const extended = try page_allocator.alloc(u8, conventional_memsize);
826 testing.expect(@ptrToInt(extended.ptr) >= conventional_memsize);
827
828 const use_small = try page_allocator.alloc(u8, 1);
829 testing.expectEqual(initial.ptr, use_small.ptr);
830 page_allocator.free(use_small);
831
832 inplace = try page_allocator.realloc(extended, 1);
833 testing.expectEqual(extended.ptr, inplace.ptr);
834 page_allocator.free(inplace);
835
836 const reuse_extended = try page_allocator.alloc(u8, conventional_memsize);
837 testing.expectEqual(extended.ptr, reuse_extended.ptr);
838 page_allocator.free(reuse_extended);
834839 }
835840}
836841