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" {...@@ -804,33 +804,38 @@ test "c_allocator" {
804804
805test "WasmPageAllocator internals" {805test "WasmPageAllocator internals" {
806 if (comptime std.Target.current.isWasm()) {806 if (comptime std.Target.current.isWasm()) {
807 const none_free = WasmPageAllocator.PageStatus.none_free;807 const conventional_memsize = WasmPageAllocator.conventional.totalPages() * std.mem.page_size;
808 std.debug.assert(none_free == WasmPageAllocator.conventional.data[0]); // Passes if this test runs first808 const initial = try page_allocator.alloc(u8, std.mem.page_size);
809 std.debug.assert(!WasmPageAllocator.extended.isInitialized()); // Passes if this test runs first809 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.
810810
811 const tmp = try page_allocator.alloc(u8, 1);811 var inplace = try page_allocator.realloc(initial, 1);
812 testing.expect(none_free == WasmPageAllocator.conventional.data[0]);812 testing.expectEqual(initial.ptr, inplace.ptr);
813 page_allocator.free(tmp);813 inplace = try page_allocator.realloc(inplace, 4);
814 testing.expect(none_free < WasmPageAllocator.conventional.data[0]);814 testing.expectEqual(initial.ptr, inplace.ptr);
815815 page_allocator.free(inplace);
816 const a_little_free = WasmPageAllocator.conventional.data[0];816
817 const tmp_large = try page_allocator.alloc(u8, std.mem.page_size + 1);817 const reuse = try page_allocator.alloc(u8, 1);
818 testing.expect(a_little_free == WasmPageAllocator.conventional.data[0]);818 testing.expectEqual(initial.ptr, reuse.ptr);
819 const tmp_small = try page_allocator.alloc(u8, 1);819 page_allocator.free(reuse);
820 testing.expect(none_free == WasmPageAllocator.conventional.data[0]);820
821821 // This segment may span conventional and extended which has really complex rules so we're just ignoring it for now.
822 page_allocator.free(tmp_small);822 const padding = try page_allocator.alloc(u8, conventional_memsize);
823 testing.expect(a_little_free == WasmPageAllocator.conventional.data[0]);823 page_allocator.free(padding);
824 page_allocator.free(tmp_large);824
825 testing.expect(a_little_free < WasmPageAllocator.conventional.data[0]);825 const extended = try page_allocator.alloc(u8, conventional_memsize);
826826 testing.expect(@ptrToInt(extended.ptr) >= conventional_memsize);
827 const more_free = WasmPageAllocator.conventional.data[0];827
828 const supersize = try page_allocator.alloc(u8, std.mem.page_size * (WasmPageAllocator.conventional.totalPages() + 1));828 const use_small = try page_allocator.alloc(u8, 1);
829 testing.expect(more_free == WasmPageAllocator.conventional.data[0]);829 testing.expectEqual(initial.ptr, use_small.ptr);
830 testing.expect(!WasmPageAllocator.extended.isInitialized());830 page_allocator.free(use_small);
831 page_allocator.free(supersize);831
832 testing.expect(WasmPageAllocator.extended.isInitialized());832 inplace = try page_allocator.realloc(extended, 1);
833 testing.expect(more_free < WasmPageAllocator.conventional.data[0]);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);
834 }839 }
835}840}
836841