|
16 | 16 |
|
17 | 17 | #include "gtest/gtest.h"
|
18 | 18 | #include <memory>
|
| 19 | +#include <string> |
19 | 20 |
|
20 | 21 | #include "test/utility.h"
|
21 | 22 |
|
@@ -147,4 +148,47 @@ TEST_P(WasmTest, DifferentRootContextsFromDifferentPluginKeys) {
|
147 | 148 | EXPECT_TRUE(root_context1->root_id() == root_context2->root_id());
|
148 | 149 | }
|
149 | 150 |
|
| 151 | +TEST_P(WasmTest, SharedQueueProducerConsumer) { |
| 152 | + const std::string plugin_name = "plugin_name"; |
| 153 | + const std::string root_id = "root_id"; |
| 154 | + const std::string plugin_config = "plugin_config"; |
| 155 | + const bool fail_open = false; |
| 156 | + const std::shared_ptr<PluginBase> plugin1 = std::make_shared<PluginBase>( |
| 157 | + plugin_name, root_id, vm_id_, runtime_, plugin_config, fail_open, "plugin1_key"); |
| 158 | + |
| 159 | + const std::string vm_key = "vm_key"; |
| 160 | + // Create base Wasm via createWasm. |
| 161 | + std::shared_ptr<WasmHandleBase> base_wasm_handle = |
| 162 | + createWasm(vm_key, source_, plugin1, wasm_handle_factory_, wasm_handle_clone_factory_, false); |
| 163 | + base_wasm_handle->wasm()->start(plugin1); |
| 164 | + ContextBase *root_context1 = base_wasm_handle->wasm()->getRootContext(plugin1, false); |
| 165 | + EXPECT_TRUE(root_context1 != nullptr); |
| 166 | + |
| 167 | + // Create a new plugin with different key. |
| 168 | + const std::shared_ptr<PluginBase> plugin2 = std::make_shared<PluginBase>( |
| 169 | + plugin_name, root_id, vm_id_, runtime_, plugin_config, fail_open, "plugin2_key"); |
| 170 | + EXPECT_TRUE(base_wasm_handle->wasm()->getRootContext(plugin2, false) == nullptr); |
| 171 | + |
| 172 | + // Create context from a plugin2. |
| 173 | + base_wasm_handle->wasm()->start(plugin2); |
| 174 | + ContextBase *root_context2 = base_wasm_handle->wasm()->getRootContext(plugin2, false); |
| 175 | + EXPECT_TRUE(root_context2 != nullptr); |
| 176 | + |
| 177 | + // Verify that the 2 contexts can communicate through a shared queue. |
| 178 | + std::string queue_name{"queue"}; |
| 179 | + SharedQueueDequeueToken token1; |
| 180 | + EXPECT_EQ(root_context1->registerSharedQueue(queue_name, &token1), WasmResult::Ok); |
| 181 | + SharedQueueDequeueToken token2; |
| 182 | + EXPECT_EQ(root_context2->lookupSharedQueue(vm_id_, queue_name, &token2), WasmResult::Ok); |
| 183 | + EXPECT_EQ(token1, token2); |
| 184 | + for (int i = 0; i < 5; i++) { |
| 185 | + root_context1->enqueueSharedQueue(token1, std::to_string(i)); |
| 186 | + } |
| 187 | + std::string data; |
| 188 | + for (int i = 0; i < 5; i++) { |
| 189 | + root_context1->dequeueSharedQueue(token1, &data); |
| 190 | + EXPECT_EQ(data, std::to_string(i)); |
| 191 | + } |
| 192 | +} |
| 193 | + |
150 | 194 | } // namespace proxy_wasm
|
0 commit comments