Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,18 +555,18 @@ static void createWasmInternal(const VmConfig& vm_config, PluginSharedPtr plugin
wasm = std::make_shared<WasmHandle>(std::make_shared<Wasm>(
vm_config.runtime(), vm_config.vm_id(), vm_config.configuration(), vm_key, scope,
cluster_manager, dispatcher));
if (!wasm->wasm()->initialize(code, vm_config.allow_precompiled())) {
throw WasmException(fmt::format("Failed to initialize WASM code from {}", source));
}
if (!context) {
wasm->wasm()->start(plugin);
} else {
wasm->wasm()->startForTesting(std::move(context), plugin);
}
(*base_wasms_)[vm_key] = wasm;
}
}

if (!wasm->wasm()->initialize(code, vm_config.allow_precompiled())) {
throw WasmException(fmt::format("Failed to initialize WASM code from {}", source));
}
if (!context) {
wasm->wasm()->start(plugin);
} else {
wasm->wasm()->startForTesting(std::move(context), plugin);
}
cb(std::move(wasm));
};

Expand Down
64 changes: 63 additions & 1 deletion test/extensions/common/wasm/wasm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ namespace Wasm {

class TestContext : public Extensions::Common::Wasm::Context {
public:
TestContext(Extensions::Common::Wasm::Wasm* wasm) : Extensions::Common::Wasm::Context(wasm) {}
TestContext() : Extensions::Common::Wasm::Context() {}
explicit TestContext(Extensions::Common::Wasm::Wasm* wasm)
: Extensions::Common::Wasm::Context(wasm) {}
~TestContext() override {}
void scriptLog(spdlog::level::level_enum level, absl::string_view message) override {
std::cerr << std::string(message) << "\n";
Expand Down Expand Up @@ -352,6 +354,66 @@ TEST_P(WasmCommonTest, Foreign) {
wasm->startForTesting(std::move(context), plugin);
}

TEST_P(WasmCommonTest, VmCache) {
Stats::IsolatedStoreImpl stats_store;
Api::ApiPtr api = Api::createApiForTest(stats_store);
NiceMock<Upstream::MockClusterManager> cluster_manager;
NiceMock<Init::MockManager> init_manager;
Event::DispatcherPtr dispatcher(api->allocateDispatcher());
Config::DataSource::RemoteAsyncDataProviderPtr remote_data_provider;
auto scope = Stats::ScopeSharedPtr(stats_store.createScope("wasm."));
NiceMock<LocalInfo::MockLocalInfo> local_info;
auto name = "";
auto root_id = "";
auto vm_id = "";
auto vm_configuration = "vm_cache";
auto plugin = std::make_shared<Extensions::Common::Wasm::Plugin>(
name, root_id, vm_id, envoy::config::core::v3::TrafficDirection::UNSPECIFIED, local_info,
nullptr);

VmConfig vm_config;
vm_config.set_runtime(absl::StrCat("envoy.wasm.runtime.", GetParam()));
vm_config.set_configuration(vm_configuration);
std::string code;
if (GetParam() != "null") {
code = TestEnvironment::readFileToStringForTest(TestEnvironment::substitute(
absl::StrCat("{{ test_rundir }}/test/extensions/common/wasm/test_data/test_cpp.wasm")));
} else {
// The name of the Null VM plugin.
code = "CommonWasmTestCpp";
}
EXPECT_FALSE(code.empty());
vm_config.mutable_code()->mutable_local()->set_inline_bytes(code);
WasmHandleSharedPtr wasm_handle;
auto root_context = new Extensions::Common::Wasm::TestContext();
EXPECT_CALL(*root_context, scriptLog_(spdlog::level::info, Eq("on_vm_start vm_cache")));
EXPECT_CALL(*root_context, scriptLog_(spdlog::level::info, Eq("on_done logging")));
EXPECT_CALL(*root_context, scriptLog_(spdlog::level::info, Eq("on_delete logging")));
createWasmForTesting(vm_config, plugin, scope, cluster_manager, init_manager, *dispatcher, *api,
std::unique_ptr<Context>(root_context), remote_data_provider,
[&wasm_handle](WasmHandleSharedPtr w) { wasm_handle = w; });

EXPECT_NE(wasm_handle, nullptr);

WasmHandleSharedPtr wasm_handle2;
auto root_context2 = new Extensions::Common::Wasm::Context();
createWasmForTesting(vm_config, plugin, scope, cluster_manager, init_manager, *dispatcher, *api,
std::unique_ptr<Context>(root_context2), remote_data_provider,
[&wasm_handle2](WasmHandleSharedPtr w) { wasm_handle2 = w; });
EXPECT_NE(wasm_handle2, nullptr);
EXPECT_EQ(wasm_handle, wasm_handle2);

plugin.reset();
auto wasm = wasm_handle->wasm().get();
wasm_handle.reset();
wasm_handle2.reset();

dispatcher->run(Event::Dispatcher::RunType::NonBlock);
wasm->configure(root_context, plugin, "done");
dispatcher->run(Event::Dispatcher::RunType::NonBlock);
dispatcher->clearDeferredDeleteList();
}

} // namespace Wasm
} // namespace Common
} // namespace Extensions
Expand Down