Skip to content

Commit 0604422

Browse files
authored
Reset global type state between tests (#4468)
Add a `destroyAllTypes` function to clear the global state of the type system and use it in a custom gtest test fixture to ensure that each test starts and ends with a fresh state.
1 parent 93f69e5 commit 0604422

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/wasm-type.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ void setTypeSystem(TypeSystem system);
4545

4646
TypeSystem getTypeSystem();
4747

48+
// Dangerous! Frees all types and heap types that have ever been created and
49+
// resets the type system's internal state. This is only really meant to be used
50+
// for tests.
51+
void destroyAllTypesForTestingPurposesOnly();
52+
4853
// The types defined in this file. All of them are small and typically passed by
4954
// value except for `Tuple` and `Struct`, which may own an unbounded amount of
5055
// data.

src/wasm/wasm-type.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@ template<typename Info> struct Store {
653653
}
654654
bool hasCanonical(const Info& info, typename Info::type_t& canonical);
655655

656+
void clear() {
657+
typeIDs.clear();
658+
constructedTypes.clear();
659+
}
660+
656661
private:
657662
template<typename Ref> typename Info::type_t doInsert(Ref& infoRef) {
658663
const Info& info = [&]() {
@@ -754,12 +759,20 @@ struct SignatureTypeCache {
754759
std::lock_guard<std::mutex> lock(mutex);
755760
cache.insert({type.getSignature(), type});
756761
}
762+
763+
void clear() { cache.clear(); }
757764
};
758765

759766
static SignatureTypeCache nominalSignatureCache;
760767

761768
} // anonymous namespace
762769

770+
void destroyAllTypesForTestingPurposesOnly() {
771+
globalTypeStore.clear();
772+
globalHeapTypeStore.clear();
773+
nominalSignatureCache.clear();
774+
}
775+
763776
Type::Type(std::initializer_list<Type> types) : Type(Tuple(types)) {}
764777

765778
Type::Type(const Tuple& tuple) {

test/gtest/type-builder.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,35 @@
33

44
using namespace wasm;
55

6+
// Helper test fixture for managing the global type system state.
7+
template<TypeSystem system> class TypeSystemTest : public ::testing::Test {
8+
TypeSystem originalSystem;
9+
10+
protected:
11+
void SetUp() override {
12+
originalSystem = getTypeSystem();
13+
setTypeSystem(system);
14+
}
15+
void TearDown() override {
16+
destroyAllTypesForTestingPurposesOnly();
17+
setTypeSystem(originalSystem);
18+
}
19+
};
20+
using EquirecursiveTest = TypeSystemTest<TypeSystem::Equirecursive>;
21+
using NominalTest = TypeSystemTest<TypeSystem::Nominal>;
22+
using IsorecursiveTest = TypeSystemTest<TypeSystem::Isorecursive>;
23+
624
TEST(TypeBuilder, Growth) {
725
TypeBuilder builder;
826
EXPECT_EQ(builder.size(), size_t{0});
927
builder.grow(3);
1028
EXPECT_EQ(builder.size(), size_t{3});
1129
}
1230

13-
TEST(TypeBuilder, Basics) {
31+
TEST_F(EquirecursiveTest, Basics) {
1432
// (type $sig (func (param (ref $struct)) (result (ref $array) i32)))
1533
// (type $struct (struct (field (ref null $array) (mut rtt 0 $array))))
1634
// (type $array (array (mut externref)))
17-
1835
TypeBuilder builder(3);
1936
ASSERT_EQ(builder.size(), size_t{3});
2037

0 commit comments

Comments
 (0)