Add unit tests for the tid allocator (#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.
This commit is contained in:
@ -8,8 +8,14 @@
|
||||
|
||||
#include "platform_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TID_ALLOCATOR_INIT_SIZE CLUSTER_MAX_THREAD_NUM
|
||||
enum {
|
||||
/* Keep it in sync with
|
||||
https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids */
|
||||
TID_MIN = 1,
|
||||
TID_MAX = 0x1FFFFFFF
|
||||
}; // Reserved TIDs (WASI specification)
|
||||
@ -33,4 +39,8 @@ tid_allocator_get_tid(TidAllocator *tid_allocator);
|
||||
void
|
||||
tid_allocator_release_tid(TidAllocator *tid_allocator, int32 thread_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TID_ALLOCATOR_H */
|
||||
@ -0,0 +1,6 @@
|
||||
# Copyright (C) 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
create_wamr_unit_test(wasi_threads
|
||||
${CMAKE_CURRENT_LIST_DIR}/test_tid_allocator.cpp
|
||||
)
|
||||
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "tid_allocator.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class TidAllocatorTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
void SetUp() override { ASSERT_TRUE(tid_allocator_init(&_allocator)); }
|
||||
|
||||
void TearDown() override { tid_allocator_deinit(&_allocator); }
|
||||
|
||||
TidAllocator _allocator;
|
||||
};
|
||||
|
||||
static bool
|
||||
is_tid_valid(int32 tid)
|
||||
{
|
||||
/* See: https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids
|
||||
*/
|
||||
return tid >= TID_MIN && tid <= TID_MAX;
|
||||
}
|
||||
|
||||
TEST_F(TidAllocatorTest, BasicTest)
|
||||
{
|
||||
int32 tid = tid_allocator_get_tid(&_allocator);
|
||||
|
||||
ASSERT_TRUE(is_tid_valid(tid));
|
||||
}
|
||||
|
||||
TEST_F(TidAllocatorTest, ShouldFailOnAllocatingMoreThanAllowedThreadIDs)
|
||||
{
|
||||
int32 last_tid = 0;
|
||||
for (int32 i = 0; i < TID_MAX + 1; i++) {
|
||||
last_tid = tid_allocator_get_tid(&_allocator);
|
||||
if (last_tid < 0) {
|
||||
break;
|
||||
}
|
||||
ASSERT_TRUE(is_tid_valid(last_tid));
|
||||
}
|
||||
|
||||
ASSERT_LT(last_tid, 0);
|
||||
}
|
||||
|
||||
TEST_F(TidAllocatorTest, ShouldAllocateMoreThanAllowedTIDsIfOldTIDsAreReleased)
|
||||
{
|
||||
int32 last_tid = 0;
|
||||
for (int32 i = 0; i < TID_MAX + 1; i++) {
|
||||
if (last_tid != 0) {
|
||||
tid_allocator_release_tid(&_allocator, last_tid);
|
||||
}
|
||||
|
||||
last_tid = tid_allocator_get_tid(&_allocator);
|
||||
ASSERT_TRUE(is_tid_valid(last_tid));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user