Skip to content

Commit eb50377

Browse files
authored
Add unit tests for the tid allocator (bytecodealliance#2519)
Add simple infrastructure to add more unit tests in the future. At the moment tests are only executed on Linux, but can be extended to other platforms if needed. Use https://github.com/google/googletest/ as a framework.
1 parent 128d05c commit eb50377

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
with:
7373
os: "ubuntu-22.04"
7474
arch: "X86"
75-
75+
7676
build_wamrc:
7777
needs:
7878
[build_llvm_libraries_on_ubuntu_2204]
@@ -241,6 +241,14 @@ jobs:
241241
cmake --build . --config Release --parallel 4
242242
working-directory: product-mini/platforms/${{ matrix.platform }}
243243

244+
- name: Build and run unit tests
245+
run: |
246+
mkdir build-unittests && cd build-unittests
247+
cmake .. ${{ matrix.make_options_run_mode }} ${{ matrix.make_options_feature }}
248+
cmake --build . --config Release --parallel 4
249+
ctest
250+
working-directory: tests/unit
251+
244252
build_samples_wasm_c_api:
245253
needs:
246254
[
@@ -483,7 +491,7 @@ jobs:
483491
sudo tar -xzf wasi-sdk-*.tar.gz
484492
sudo mv wasi-sdk-20.0 wasi-sdk
485493
486-
# It is a temporary solution until new wasi-sdk that includes bug fixes is released
494+
# It is a temporary solution until new wasi-sdk that includes bug fixes is released
487495
- name: build wasi-libc from source
488496
if: matrix.test_option == '$WASI_TEST_OPTIONS'
489497
run: |

core/iwasm/libraries/lib-wasi-threads/tid_allocator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88

99
#include "platform_common.h"
1010

11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
1115
#define TID_ALLOCATOR_INIT_SIZE CLUSTER_MAX_THREAD_NUM
1216
enum {
17+
/* Keep it in sync with
18+
https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids */
1319
TID_MIN = 1,
1420
TID_MAX = 0x1FFFFFFF
1521
}; // Reserved TIDs (WASI specification)
@@ -33,4 +39,8 @@ tid_allocator_get_tid(TidAllocator *tid_allocator);
3339
void
3440
tid_allocator_release_tid(TidAllocator *tid_allocator, int32 thread_id);
3541

42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
3646
#endif /* _TID_ALLOCATOR_H */
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (C) 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
create_wamr_unit_test(wasi_threads
5+
${CMAKE_CURRENT_LIST_DIR}/test_tid_allocator.cpp
6+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include <gtest/gtest.h>
7+
8+
#include "tid_allocator.h"
9+
10+
#include <stdint.h>
11+
12+
class TidAllocatorTest : public ::testing::Test
13+
{
14+
protected:
15+
void SetUp() override { ASSERT_TRUE(tid_allocator_init(&_allocator)); }
16+
17+
void TearDown() override { tid_allocator_deinit(&_allocator); }
18+
19+
TidAllocator _allocator;
20+
};
21+
22+
static bool
23+
is_tid_valid(int32 tid)
24+
{
25+
/* See: https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids
26+
*/
27+
return tid >= TID_MIN && tid <= TID_MAX;
28+
}
29+
30+
TEST_F(TidAllocatorTest, BasicTest)
31+
{
32+
int32 tid = tid_allocator_get_tid(&_allocator);
33+
34+
ASSERT_TRUE(is_tid_valid(tid));
35+
}
36+
37+
TEST_F(TidAllocatorTest, ShouldFailOnAllocatingMoreThanAllowedThreadIDs)
38+
{
39+
int32 last_tid = 0;
40+
for (int32 i = 0; i < TID_MAX + 1; i++) {
41+
last_tid = tid_allocator_get_tid(&_allocator);
42+
if (last_tid < 0) {
43+
break;
44+
}
45+
ASSERT_TRUE(is_tid_valid(last_tid));
46+
}
47+
48+
ASSERT_LT(last_tid, 0);
49+
}
50+
51+
TEST_F(TidAllocatorTest, ShouldAllocateMoreThanAllowedTIDsIfOldTIDsAreReleased)
52+
{
53+
int32 last_tid = 0;
54+
for (int32 i = 0; i < TID_MAX + 1; i++) {
55+
if (last_tid != 0) {
56+
tid_allocator_release_tid(&_allocator, last_tid);
57+
}
58+
59+
last_tid = tid_allocator_get_tid(&_allocator);
60+
ASSERT_TRUE(is_tid_valid(last_tid));
61+
}
62+
}

tests/unit/CMakeLists.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (C) 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
project (wamr_unit_tests)
7+
8+
include (CTest)
9+
10+
if (NOT DEFINED WAMR_BUILD_INTERP)
11+
# Enable Interpreter by default
12+
set (WAMR_BUILD_INTERP 1)
13+
endif ()
14+
15+
if (NOT DEFINED WAMR_BUILD_PLATFORM)
16+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
17+
endif ()
18+
19+
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
20+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
21+
add_library (vmlib ${WAMR_RUNTIME_LIB_SOURCE})
22+
23+
include (FetchContent)
24+
FetchContent_Declare (
25+
googletest
26+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
27+
)
28+
# For Windows: Prevent overriding the parent project's compiler/linker settings
29+
set (gtest_force_shared_crt ON CACHE BOOL "" FORCE)
30+
FetchContent_MakeAvailable (googletest)
31+
32+
include (GoogleTest)
33+
34+
add_library (wamr_gtest_main main.cpp)
35+
target_link_libraries (wamr_gtest_main PUBLIC gtest vmlib)
36+
37+
function (create_wamr_unit_test test_name)
38+
set (sources ${ARGN})
39+
add_executable (${test_name} ${sources})
40+
target_link_libraries (
41+
${test_name}
42+
wamr_gtest_main
43+
vmlib
44+
${LLVM_AVAILABLE_LIBS}
45+
)
46+
gtest_discover_tests (${test_name})
47+
endfunction ()
48+
49+
if (WAMR_BUILD_LIB_WASI_THREADS EQUAL 1)
50+
include (${IWASM_DIR}/libraries/lib-wasi-threads/unit-test/lib_wasi_threads_unit_tests.cmake)
51+
endif ()

tests/unit/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
#include <gtest/gtest.h>
6+
#include "wasm_runtime_common.h"
7+
8+
int
9+
main(int argc, char **argv)
10+
{
11+
::testing::InitGoogleTest(&argc, argv);
12+
13+
if (!wasm_runtime_init()) {
14+
return -1;
15+
}
16+
17+
int ret = RUN_ALL_TESTS();
18+
wasm_runtime_destroy();
19+
20+
return ret;
21+
}

0 commit comments

Comments
 (0)