Skip to content

Commit

Permalink
[Test] Add the async invoke test in executor.
Browse files Browse the repository at this point in the history
Signed-off-by: YiYing He <yiying@secondstate.io>
  • Loading branch information
q82419 committed May 4, 2023
1 parent 421307e commit 8d29885
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 65 deletions.
42 changes: 14 additions & 28 deletions test/aot/AOTcoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,27 @@ INSTANTIATE_TEST_SUITE_P(TestUnit, NativeCoreTest,
INSTANTIATE_TEST_SUITE_P(TestUnit, CustomWasmCoreTest,
testing::ValuesIn(T.enumerate()));

std::array<WasmEdge::Byte, 46> AsyncWasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};

TEST(AsyncRunWsmFile, NativeInterruptTest) {
WasmEdge::Configure Conf;
Conf.getCompilerConfigure().setInterruptible(true);
Conf.getCompilerConfigure().setOutputFormat(
CompilerConfigure::OutputFormat::Native);

WasmEdge::VM::VM VM(Conf);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
WasmEdge::Loader::Loader Loader(Conf);
WasmEdge::Validator::Validator ValidatorEngine(Conf);
WasmEdge::AOT::Compiler Compiler(Conf);
auto Path = std::filesystem::temp_directory_path() /
std::filesystem::u8path("AOTcoreTest" WASMEDGE_LIB_EXTENSION);
auto Module = *Loader.parseModule(Wasm);
auto Module = *Loader.parseModule(AsyncWasm);
ASSERT_TRUE(ValidatorEngine.validate(*Module));
ASSERT_TRUE(Compiler.compile(Wasm, *Module, Path));
ASSERT_TRUE(Compiler.compile(AsyncWasm, *Module, Path));
{
auto Timeout =
std::chrono::system_clock::now() + std::chrono::milliseconds(1);
Expand Down Expand Up @@ -315,19 +316,14 @@ TEST(AsyncExecute, NativeInterruptTest) {
CompilerConfigure::OutputFormat::Native);

WasmEdge::VM::VM VM(Conf);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
WasmEdge::Loader::Loader Loader(Conf);
WasmEdge::Validator::Validator ValidatorEngine(Conf);
WasmEdge::AOT::Compiler Compiler(Conf);
auto Path = std::filesystem::temp_directory_path() /
std::filesystem::u8path("AOTcoreTest" WASMEDGE_LIB_EXTENSION);
auto Module = *Loader.parseModule(Wasm);
auto Module = *Loader.parseModule(AsyncWasm);
ASSERT_TRUE(ValidatorEngine.validate(*Module));
ASSERT_TRUE(Compiler.compile(Wasm, *Module, Path));
ASSERT_TRUE(Compiler.compile(AsyncWasm, *Module, Path));
ASSERT_TRUE(VM.loadWasm(Path));
ASSERT_TRUE(VM.validate());
ASSERT_TRUE(VM.instantiate());
Expand Down Expand Up @@ -361,19 +357,14 @@ TEST(AsyncRunWsmFile, CustomWasmInterruptTest) {
CompilerConfigure::OutputFormat::Wasm);

WasmEdge::VM::VM VM(Conf);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
WasmEdge::Loader::Loader Loader(Conf);
WasmEdge::Validator::Validator ValidatorEngine(Conf);
WasmEdge::AOT::Compiler Compiler(Conf);
auto Path = std::filesystem::temp_directory_path() /
std::filesystem::u8path("AOTcoreTest.aot.wasm");
auto Module = *Loader.parseModule(Wasm);
auto Module = *Loader.parseModule(AsyncWasm);
ASSERT_TRUE(ValidatorEngine.validate(*Module));
ASSERT_TRUE(Compiler.compile(Wasm, *Module, Path));
ASSERT_TRUE(Compiler.compile(AsyncWasm, *Module, Path));
{
auto Timeout =
std::chrono::system_clock::now() + std::chrono::milliseconds(1);
Expand Down Expand Up @@ -404,19 +395,14 @@ TEST(AsyncExecute, CustomWasmInterruptTest) {
CompilerConfigure::OutputFormat::Wasm);

WasmEdge::VM::VM VM(Conf);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
WasmEdge::Loader::Loader Loader(Conf);
WasmEdge::Validator::Validator ValidatorEngine(Conf);
WasmEdge::AOT::Compiler Compiler(Conf);
auto Path = std::filesystem::temp_directory_path() /
std::filesystem::u8path("AOTcoreTest.aot.wasm");
auto Module = *Loader.parseModule(Wasm);
auto Module = *Loader.parseModule(AsyncWasm);
ASSERT_TRUE(ValidatorEngine.validate(*Module));
ASSERT_TRUE(Compiler.compile(Wasm, *Module, Path));
ASSERT_TRUE(Compiler.compile(AsyncWasm, *Module, Path));
ASSERT_TRUE(VM.loadWasm(Path));
ASSERT_TRUE(VM.validate());
ASSERT_TRUE(VM.instantiate());
Expand Down
51 changes: 51 additions & 0 deletions test/api/APIStepsCoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,57 @@ TEST_P(CoreTest, TestSuites) {

// Initiate test suite.
INSTANTIATE_TEST_SUITE_P(TestUnit, CoreTest, testing::ValuesIn(T.enumerate()));

std::array<WasmEdge::Byte, 46> AsyncWasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};

TEST(AsyncInvoke, InterruptTest) {
WasmEdge_LoaderContext *Loader = WasmEdge_LoaderCreate(nullptr);
WasmEdge_ValidatorContext *Validator = WasmEdge_ValidatorCreate(nullptr);
WasmEdge_ExecutorContext *Executor =
WasmEdge_ExecutorCreate(nullptr, nullptr);
WasmEdge_StoreContext *Store = WasmEdge_StoreCreate();

ASSERT_NE(Loader, nullptr);
ASSERT_NE(Validator, nullptr);
ASSERT_NE(Executor, nullptr);
ASSERT_NE(Store, nullptr);

WasmEdge_ASTModuleContext *AST = nullptr;
ASSERT_TRUE(WasmEdge_ResultOK(WasmEdge_LoaderParseFromBuffer(
Loader, &AST, AsyncWasm.data(), AsyncWasm.size())));
ASSERT_NE(AST, nullptr);
ASSERT_TRUE(WasmEdge_ResultOK(WasmEdge_ValidatorValidate(Validator, AST)));
WasmEdge_ModuleInstanceContext *Module = nullptr;
ASSERT_TRUE(WasmEdge_ResultOK(
WasmEdge_ExecutorInstantiate(Executor, &Module, Store, AST)));
WasmEdge_ASTModuleDelete(AST);
ASSERT_NE(Module, nullptr);
WasmEdge_FunctionInstanceContext *FuncInst =
WasmEdge_ModuleInstanceFindFunction(Module,
WasmEdge_StringWrap("_start", 6));
ASSERT_NE(FuncInst, nullptr);
{
WasmEdge_Async *AsyncCxt =
WasmEdge_ExecutorAsyncInvoke(Executor, FuncInst, nullptr, 0);
EXPECT_NE(AsyncCxt, nullptr);
EXPECT_FALSE(WasmEdge_AsyncWaitFor(AsyncCxt, 1));
WasmEdge_AsyncCancel(AsyncCxt);
WasmEdge_Result Res = WasmEdge_AsyncGet(AsyncCxt, nullptr, 0);
EXPECT_FALSE(WasmEdge_ResultOK(Res));
EXPECT_EQ(WasmEdge_ResultGetCode(Res), WasmEdge_ErrCode_Interrupted);
WasmEdge_AsyncDelete(AsyncCxt);
}
WasmEdge_LoaderDelete(Loader);
WasmEdge_ValidatorDelete(Validator);
WasmEdge_ExecutorDelete(Executor);
WasmEdge_StoreDelete(Store);
WasmEdge_ModuleInstanceDelete(Module);
}

} // namespace

GTEST_API_ int main(int argc, char **argv) {
Expand Down
33 changes: 11 additions & 22 deletions test/api/APIVMCoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,18 @@ TEST_P(CoreTest, TestSuites) {
// Initiate test suite.
INSTANTIATE_TEST_SUITE_P(TestUnit, CoreTest, testing::ValuesIn(T.enumerate()));

TEST(AsyncRunWsmFile, InterruptTest) {
WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
WasmEdge_ConfigureCompilerSetInterruptible(ConfCxt, true);
WasmEdge_VMContext *VM = WasmEdge_VMCreate(ConfCxt, nullptr);
WasmEdge_ConfigureDelete(ConfCxt);
std::array<WasmEdge::Byte, 46> AsyncWasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};

std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
TEST(AsyncRunWsmFile, InterruptTest) {
WasmEdge_VMContext *VM = WasmEdge_VMCreate(nullptr, nullptr);
{
WasmEdge_Async *AsyncCxt = WasmEdge_VMAsyncRunWasmFromBuffer(
VM, Wasm.data(), Wasm.size(), WasmEdge_StringWrap("_start", 6), nullptr,
0);
VM, AsyncWasm.data(), AsyncWasm.size(),
WasmEdge_StringWrap("_start", 6), nullptr, 0);
EXPECT_NE(AsyncCxt, nullptr);
EXPECT_FALSE(WasmEdge_AsyncWaitFor(AsyncCxt, 1));
WasmEdge_AsyncCancel(AsyncCxt);
Expand All @@ -216,17 +213,9 @@ TEST(AsyncRunWsmFile, InterruptTest) {
}

TEST(AsyncExecute, InterruptTest) {
WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
WasmEdge_ConfigureCompilerSetInterruptible(ConfCxt, true);
WasmEdge_VMContext *VM = WasmEdge_VMCreate(ConfCxt, nullptr);
WasmEdge_ConfigureDelete(ConfCxt);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
WasmEdge_VMContext *VM = WasmEdge_VMCreate(nullptr, nullptr);
ASSERT_TRUE(WasmEdge_ResultOK(
WasmEdge_VMLoadWasmFromBuffer(VM, Wasm.data(), Wasm.size())));
WasmEdge_VMLoadWasmFromBuffer(VM, AsyncWasm.data(), AsyncWasm.size())));
ASSERT_TRUE(WasmEdge_ResultOK(WasmEdge_VMValidate(VM)));
ASSERT_TRUE(WasmEdge_ResultOK(WasmEdge_VMInstantiate(VM)));
{
Expand Down
59 changes: 44 additions & 15 deletions test/executor/ExecutorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ TEST_P(CoreTest, TestSuites) {
// Initiate test suite.
INSTANTIATE_TEST_SUITE_P(TestUnit, CoreTest, testing::ValuesIn(T.enumerate()));

std::array<WasmEdge::Byte, 46> AsyncWasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};

TEST(AsyncRunWsmFile, InterruptTest) {
WasmEdge::Configure Conf;
Conf.getCompilerConfigure().setInterruptible(true);
WasmEdge::VM::VM VM(Conf);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
{
auto Timeout =
std::chrono::system_clock::now() + std::chrono::milliseconds(1);
auto AsyncResult = VM.asyncRunWasmFile(Wasm, "_start");
auto AsyncResult = VM.asyncRunWasmFile(AsyncWasm, "_start");
EXPECT_FALSE(AsyncResult.waitUntil(Timeout));
AsyncResult.cancel();
auto Result = AsyncResult.get();
Expand All @@ -134,7 +134,7 @@ TEST(AsyncRunWsmFile, InterruptTest) {
}
{
auto Timeout = std::chrono::milliseconds(1);
auto AsyncResult = VM.asyncRunWasmFile(Wasm, "_start");
auto AsyncResult = VM.asyncRunWasmFile(AsyncWasm, "_start");
EXPECT_FALSE(AsyncResult.waitFor(Timeout));
AsyncResult.cancel();
auto Result = AsyncResult.get();
Expand All @@ -145,14 +145,8 @@ TEST(AsyncRunWsmFile, InterruptTest) {

TEST(AsyncExecute, InterruptTest) {
WasmEdge::Configure Conf;
Conf.getCompilerConfigure().setInterruptible(true);
WasmEdge::VM::VM VM(Conf);
std::array<WasmEdge::Byte, 46> Wasm{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00, 0x0a,
0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b};
ASSERT_TRUE(VM.loadWasm(Wasm));
ASSERT_TRUE(VM.loadWasm(AsyncWasm));
ASSERT_TRUE(VM.validate());
ASSERT_TRUE(VM.instantiate());
{
Expand All @@ -176,6 +170,41 @@ TEST(AsyncExecute, InterruptTest) {
}
}

TEST(AsyncInvoke, InterruptTest) {
WasmEdge::Configure Conf;
WasmEdge::Loader::Loader LoadEngine(Conf);
WasmEdge::Validator::Validator ValidEngine(Conf);
WasmEdge::Executor::Executor ExecEngine(Conf);
WasmEdge::Runtime::StoreManager Store;

auto AST = LoadEngine.parseModule(AsyncWasm);
ASSERT_TRUE(AST);
ASSERT_TRUE(ValidEngine.validate(**AST));
auto Module = ExecEngine.instantiateModule(Store, **AST);
ASSERT_TRUE(Module);
auto FuncInst = (*Module)->findFuncExports("_start");
ASSERT_NE(FuncInst, nullptr);
{
auto Timeout =
std::chrono::system_clock::now() + std::chrono::milliseconds(1);
auto AsyncResult = ExecEngine.asyncInvoke(FuncInst, {}, {});
EXPECT_FALSE(AsyncResult.waitUntil(Timeout));
AsyncResult.cancel();
auto Result = AsyncResult.get();
EXPECT_FALSE(Result);
EXPECT_EQ(Result.error(), WasmEdge::ErrCode::Value::Interrupted);
}
{
auto Timeout = std::chrono::milliseconds(1);
auto AsyncResult = ExecEngine.asyncInvoke(FuncInst, {}, {});
EXPECT_FALSE(AsyncResult.waitFor(Timeout));
AsyncResult.cancel();
auto Result = AsyncResult.get();
EXPECT_FALSE(Result);
EXPECT_EQ(Result.error(), WasmEdge::ErrCode::Value::Interrupted);
}
}

TEST(VM, MultipleVM) {
WasmEdge::Configure Conf;
WasmEdge::VM::VM VM1(Conf);
Expand Down

0 comments on commit 8d29885

Please sign in to comment.