Skip to content

Commit 5a204b0

Browse files
author
chaoqin-li1123
committed
more tests
Signed-off-by: chaoqin-li1123 <[email protected]>
1 parent 09e8a55 commit 5a204b0

File tree

6 files changed

+70
-82
lines changed

6 files changed

+70
-82
lines changed

src/shared_queue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class SharedQueue {
3131
WasmResult enqueue(uint32_t token, std::string_view value);
3232

3333
void deleteByVmId(std::string_view vm_id);
34-
uint32_t nextQueueToken();
3534

3635
private:
36+
uint32_t nextQueueToken();
3737
struct Queue {
3838
std::string vm_key;
3939
uint32_t context_id;

test/BUILD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ cc_library(
131131
hdrs = ["utility.h"],
132132
deps = [
133133
"//:lib",
134-
"//:wasmtime_lib",
134+
"//:wamr_lib",
135+
# wamr and wasmtime can't coexist for now because they have different definitions for same set of abi.
136+
# "//:wasmtime_lib",
135137
"//:wavm_lib",
136138
"@com_google_googletest//:gtest",
137139
],

test/runtime_test.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <fstream>
1818
#include <iostream>
19-
#include <memory>
2019
#include <sstream>
2120
#include <string>
2221
#include <vector>
@@ -108,11 +107,7 @@ void callback() {
108107
Word callback2(Word val) { return val + 100; }
109108

110109
TEST_P(TestVM, StraceLogLevel) {
111-
if (runtime_ == "wavm") {
112-
// TODO(mathetake): strace is yet to be implemented for WAVM.
113-
// See https://github.com/proxy-wasm/proxy-wasm-cpp-host/issues/120.
114-
return;
115-
}
110+
SKIP_TEST_FOR_RUNTIME(wavm)
116111

117112
auto integration = static_cast<DummyIntegration *>(vm_->integration().get());
118113
auto source = readTestWasmFile("callback.wasm");

test/shared_queue_test.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/shared_queue.h"
1616

1717
#include <thread>
18+
#include <atomic>
1819

1920
#include "gtest/gtest.h"
2021

@@ -24,10 +25,14 @@ namespace proxy_wasm {
2425

2526
TEST(SharedQueue, NextQueueToken) {
2627
SharedQueue shared_queue(false);
27-
for (auto i = 1; i < 5; i++) {
28-
EXPECT_EQ(i, shared_queue.nextQueueToken());
28+
std::string_view vm_id = "id";
29+
std::string_view vm_key = "vm_key";
30+
std::string_view queue_name = "name";
31+
uint32_t context_id = 1;
32+
33+
for (auto i = 1; i <= 5; i++) {
34+
EXPECT_EQ(i, shared_queue.registerQueue(vm_id, std::to_string(i), context_id, nullptr, vm_key));
2935
}
30-
EXPECT_EQ(5, shared_queue.registerQueue("a", "b", 1, nullptr, "c"));
3136
}
3237

3338
TEST(SharedQueue, SingleThread) {
@@ -88,7 +93,7 @@ TEST(SharedQueue, Concurrent) {
8893
std::string_view queue_name = "name";
8994
uint32_t context_id = 1;
9095

91-
auto queued_count = 0;
96+
std::atomic<uint32_t> queued_count = 0;
9297
std::mutex mutex;
9398
std::function<void(std::function<void()>)> call_on_thread =
9499
[&mutex, &queued_count](const std::function<void()> &f) {

test/utility.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
#include "include/proxy-wasm/wamr.h"
3737
#endif
3838

39+
#define SKIP_TEST_FOR_RUNTIME(runtime) \
40+
if (runtime_ == #runtime) { \
41+
return; \
42+
}
43+
3944
namespace proxy_wasm {
4045

4146
std::vector<std::string> getRuntimes();

test/wasm_test.cc

Lines changed: 51 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,61 @@ namespace proxy_wasm {
2323

2424
auto test_values = testing::ValuesIn(getRuntimes());
2525

26-
INSTANTIATE_TEST_SUITE_P(Runtimes, TestVM, test_values);
26+
struct WasmTest : public TestVM {
27+
WasmTest() {
28+
wasm_handle_factory_ = [this](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
29+
auto base_wasm = std::make_shared<WasmBase>(newVm(), vm_id_, vm_config_, vm_key,
30+
std::unordered_map<std::string, std::string>{},
31+
AllowedCapabilitiesMap{});
32+
return std::make_shared<WasmHandleBase>(base_wasm);
33+
};
34+
35+
wasm_handle_clone_factory_ =
36+
[this](
37+
std::shared_ptr<WasmHandleBase> base_wasm_handle) -> std::shared_ptr<WasmHandleBase> {
38+
auto wasm = std::make_shared<WasmBase>(
39+
base_wasm_handle, [this]() -> std::unique_ptr<WasmVm> { return newVm(); });
40+
return std::make_shared<WasmHandleBase>(wasm);
41+
};
42+
43+
plugin_handle_factory_ =
44+
[](std::shared_ptr<WasmHandleBase> base_wasm,
45+
std::shared_ptr<PluginBase> plugin) -> std::shared_ptr<PluginHandleBase> {
46+
return std::make_shared<PluginHandleBase>(base_wasm, plugin);
47+
};
48+
}
49+
const std::string vm_id_ = "vm_id";
50+
const std::string vm_config_ = "vm_config";
51+
std::string source_ = readTestWasmFile("abi_export.wasm");
52+
WasmHandleFactory wasm_handle_factory_;
53+
WasmHandleCloneFactory wasm_handle_clone_factory_;
54+
PluginHandleFactory plugin_handle_factory_;
55+
};
56+
57+
INSTANTIATE_TEST_SUITE_P(Runtimes, WasmTest, test_values);
2758

2859
// Failcallbacks only used for runtimes - not available for nullvm.
29-
TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
30-
const auto plugin_name = "plugin_name";
31-
const auto root_id = "root_id";
32-
const auto vm_id = "vm_id";
33-
const auto vm_config = "vm_config";
34-
const auto plugin_config = "plugin_config";
35-
const auto fail_open = false;
36-
60+
TEST_P(WasmTest, GetOrCreateThreadLocalWasmFailCallbacks) {
61+
const std::string plugin_name = "plugin_name";
62+
const std::string root_id = "root_id";
63+
const std::string plugin_config = "plugin_config";
64+
const bool fail_open = false;
3765
// Create a plugin.
38-
const auto plugin = std::make_shared<PluginBase>(plugin_name, root_id, vm_id, runtime_,
66+
const auto plugin = std::make_shared<PluginBase>(plugin_name, root_id, vm_id_, runtime_,
3967
plugin_config, fail_open, "plugin_key");
4068

41-
// Define callbacks.
42-
WasmHandleFactory wasm_handle_factory =
43-
[this, vm_id, vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
44-
auto base_wasm = std::make_shared<WasmBase>(newVm(), vm_id, vm_config, vm_key,
45-
std::unordered_map<std::string, std::string>{},
46-
AllowedCapabilitiesMap{});
47-
return std::make_shared<WasmHandleBase>(base_wasm);
48-
};
49-
50-
WasmHandleCloneFactory wasm_handle_clone_factory =
51-
[this](std::shared_ptr<WasmHandleBase> base_wasm_handle) -> std::shared_ptr<WasmHandleBase> {
52-
auto wasm = std::make_shared<WasmBase>(base_wasm_handle,
53-
[this]() -> std::unique_ptr<WasmVm> { return newVm(); });
54-
return std::make_shared<WasmHandleBase>(wasm);
55-
};
56-
57-
PluginHandleFactory plugin_handle_factory =
58-
[](std::shared_ptr<WasmHandleBase> base_wasm,
59-
std::shared_ptr<PluginBase> plugin) -> std::shared_ptr<PluginHandleBase> {
60-
return std::make_shared<PluginHandleBase>(base_wasm, plugin);
61-
};
62-
63-
// Read the minimal loadable binary.
64-
auto source = readTestWasmFile("abi_export.wasm");
65-
6669
// Create base Wasm via createWasm.
67-
auto base_wasm_handle =
68-
createWasm("vm_key", source, plugin, wasm_handle_factory, wasm_handle_clone_factory, false);
69-
ASSERT_TRUE(base_wasm_handle && base_wasm_handle->wasm());
70+
auto base_wasm_handle = createWasm("vm_key", source_, plugin, wasm_handle_factory_,
71+
wasm_handle_clone_factory_, false);
7072

73+
ASSERT_TRUE(base_wasm_handle && base_wasm_handle->wasm());
7174
// Create a thread local plugin.
7275
auto thread_local_plugin = getOrCreateThreadLocalPlugin(
73-
base_wasm_handle, plugin, wasm_handle_clone_factory, plugin_handle_factory);
76+
base_wasm_handle, plugin, wasm_handle_clone_factory_, plugin_handle_factory_);
7477
ASSERT_TRUE(thread_local_plugin && thread_local_plugin->plugin());
7578
// If the VM is not failed, same WasmBase should be used for the same configuration.
76-
ASSERT_EQ(getOrCreateThreadLocalPlugin(base_wasm_handle, plugin, wasm_handle_clone_factory,
77-
plugin_handle_factory)
79+
ASSERT_EQ(getOrCreateThreadLocalPlugin(base_wasm_handle, plugin, wasm_handle_clone_factory_,
80+
plugin_handle_factory_)
7881
->wasm(),
7982
thread_local_plugin->wasm());
8083

@@ -87,7 +90,7 @@ TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
8790
// Create another thread local plugin with the same configuration.
8891
// This one should not end up using the failed VM.
8992
auto thread_local_plugin2 = getOrCreateThreadLocalPlugin(
90-
base_wasm_handle, plugin, wasm_handle_clone_factory, plugin_handle_factory);
93+
base_wasm_handle, plugin, wasm_handle_clone_factory_, plugin_handle_factory_);
9194
ASSERT_TRUE(thread_local_plugin2 && thread_local_plugin2->plugin());
9295
ASSERT_FALSE(thread_local_plugin2->wasm()->isFailed());
9396
// Verify the pointer to WasmBase is different from the failed one.
@@ -101,58 +104,36 @@ TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
101104

102105
// This time, create another thread local plugin with *different* plugin key for the same vm_key.
103106
// This one also should not end up using the failed VM.
104-
const auto plugin2 = std::make_shared<PluginBase>(plugin_name, root_id, vm_id, runtime_,
107+
const auto plugin2 = std::make_shared<PluginBase>(plugin_name, root_id, vm_id_, runtime_,
105108
plugin_config, fail_open, "another_plugin_key");
106109
auto thread_local_plugin3 = getOrCreateThreadLocalPlugin(
107-
base_wasm_handle, plugin2, wasm_handle_clone_factory, plugin_handle_factory);
110+
base_wasm_handle, plugin2, wasm_handle_clone_factory_, plugin_handle_factory_);
108111
ASSERT_TRUE(thread_local_plugin3 && thread_local_plugin3->plugin());
109112
ASSERT_FALSE(thread_local_plugin3->wasm()->isFailed());
110113
// Verify the pointer to WasmBase is different from the failed one.
111114
ASSERT_NE(thread_local_plugin3->wasm(), thread_local_plugin->wasm());
112115
ASSERT_NE(thread_local_plugin3->wasm(), thread_local_plugin2->wasm());
113116
}
114117

115-
TEST_P(TestVM, DifferentRootContextsFromDifferentPluginKeys) {
118+
TEST_P(WasmTest, DifferentRootContextsFromDifferentPluginKeys) {
116119
const std::string plugin_name = "plugin_name";
117120
const std::string root_id = "root_id";
118-
const std::string vm_id = "vm_id";
119-
const std::string vm_config = "vm_config";
120121
const std::string plugin_config = "plugin_config";
121122
const bool fail_open = false;
122-
123123
const std::shared_ptr<PluginBase> plugin1 = std::make_shared<PluginBase>(
124-
plugin_name, root_id, vm_id, runtime_, plugin_config, fail_open, "plugin1_key");
125-
126-
// Define callbacks.
127-
WasmHandleFactory wasm_handle_factory =
128-
[this, vm_id, vm_config](std::string_view vm_key) -> std::shared_ptr<WasmHandleBase> {
129-
auto base_wasm = std::make_shared<WasmBase>(newVm(), vm_id, vm_config, vm_key,
130-
std::unordered_map<std::string, std::string>{},
131-
AllowedCapabilitiesMap{});
132-
return std::make_shared<WasmHandleBase>(base_wasm);
133-
};
134-
135-
WasmHandleCloneFactory wasm_handle_clone_factory =
136-
[this](std::shared_ptr<WasmHandleBase> base_wasm_handle) -> std::shared_ptr<WasmHandleBase> {
137-
std::shared_ptr<WasmBase> wasm = std::make_shared<WasmBase>(
138-
base_wasm_handle, [this]() -> std::unique_ptr<WasmVm> { return newVm(); });
139-
return std::make_shared<WasmHandleBase>(wasm);
140-
};
141-
142-
// Read the minimal loadable binary.
143-
std::string source = readTestWasmFile("abi_export.wasm");
124+
plugin_name, root_id, vm_id_, runtime_, plugin_config, fail_open, "plugin1_key");
144125

145126
const std::string vm_key = "vm_key";
146127
// Create base Wasm via createWasm.
147128
std::shared_ptr<WasmHandleBase> base_wasm_handle =
148-
createWasm(vm_key, source, plugin1, wasm_handle_factory, wasm_handle_clone_factory, false);
129+
createWasm(vm_key, source_, plugin1, wasm_handle_factory_, wasm_handle_clone_factory_, false);
149130
base_wasm_handle->wasm()->start(plugin1);
150131
ContextBase *root_context1 = base_wasm_handle->wasm()->getRootContext(plugin1, false);
151132
EXPECT_TRUE(root_context1 != nullptr);
152133

153134
// Create a new plugin with different key.
154135
const std::shared_ptr<PluginBase> plugin2 = std::make_shared<PluginBase>(
155-
plugin_name, root_id, vm_id, runtime_, plugin_config, fail_open, "plugin2_key");
136+
plugin_name, root_id, vm_id_, runtime_, plugin_config, fail_open, "plugin2_key");
156137
EXPECT_TRUE(base_wasm_handle->wasm()->getRootContext(plugin2, false) == nullptr);
157138

158139
// Create context from a plugin2.

0 commit comments

Comments
 (0)