@@ -23,58 +23,61 @@ namespace proxy_wasm {
23
23
24
24
auto test_values = testing::ValuesIn(getRuntimes());
25
25
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);
27
58
28
59
// 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 ;
37
65
// 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_,
39
67
plugin_config, fail_open, " plugin_key" );
40
68
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
-
66
69
// 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 );
70
72
73
+ ASSERT_TRUE (base_wasm_handle && base_wasm_handle->wasm ());
71
74
// Create a thread local plugin.
72
75
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_ );
74
77
ASSERT_TRUE (thread_local_plugin && thread_local_plugin->plugin ());
75
78
// 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_ )
78
81
->wasm (),
79
82
thread_local_plugin->wasm ());
80
83
@@ -87,7 +90,7 @@ TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
87
90
// Create another thread local plugin with the same configuration.
88
91
// This one should not end up using the failed VM.
89
92
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_ );
91
94
ASSERT_TRUE (thread_local_plugin2 && thread_local_plugin2->plugin ());
92
95
ASSERT_FALSE (thread_local_plugin2->wasm ()->isFailed ());
93
96
// Verify the pointer to WasmBase is different from the failed one.
@@ -101,58 +104,36 @@ TEST_P(TestVM, GetOrCreateThreadLocalWasmFailCallbacks) {
101
104
102
105
// This time, create another thread local plugin with *different* plugin key for the same vm_key.
103
106
// 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_,
105
108
plugin_config, fail_open, " another_plugin_key" );
106
109
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_ );
108
111
ASSERT_TRUE (thread_local_plugin3 && thread_local_plugin3->plugin ());
109
112
ASSERT_FALSE (thread_local_plugin3->wasm ()->isFailed ());
110
113
// Verify the pointer to WasmBase is different from the failed one.
111
114
ASSERT_NE (thread_local_plugin3->wasm (), thread_local_plugin->wasm ());
112
115
ASSERT_NE (thread_local_plugin3->wasm (), thread_local_plugin2->wasm ());
113
116
}
114
117
115
- TEST_P (TestVM , DifferentRootContextsFromDifferentPluginKeys) {
118
+ TEST_P (WasmTest , DifferentRootContextsFromDifferentPluginKeys) {
116
119
const std::string plugin_name = " plugin_name" ;
117
120
const std::string root_id = " root_id" ;
118
- const std::string vm_id = " vm_id" ;
119
- const std::string vm_config = " vm_config" ;
120
121
const std::string plugin_config = " plugin_config" ;
121
122
const bool fail_open = false ;
122
-
123
123
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" );
144
125
145
126
const std::string vm_key = " vm_key" ;
146
127
// Create base Wasm via createWasm.
147
128
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 );
149
130
base_wasm_handle->wasm ()->start (plugin1);
150
131
ContextBase *root_context1 = base_wasm_handle->wasm ()->getRootContext (plugin1, false );
151
132
EXPECT_TRUE (root_context1 != nullptr );
152
133
153
134
// Create a new plugin with different key.
154
135
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" );
156
137
EXPECT_TRUE (base_wasm_handle->wasm ()->getRootContext (plugin2, false ) == nullptr );
157
138
158
139
// Create context from a plugin2.
0 commit comments