From f6dae67aff54afe4e8b00f28859fce85c2611f75 Mon Sep 17 00:00:00 2001 From: chaoqin-li1123 Date: Tue, 1 Mar 2022 22:51:28 +0000 Subject: [PATCH 1/3] remove default implementation of time api Signed-off-by: chaoqin-li1123 --- include/proxy-wasm/context.h | 6 ++++-- test/exports_test.cc | 7 +------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/proxy-wasm/context.h b/include/proxy-wasm/context.h index 7eaf19c52..64e46690f 100644 --- a/include/proxy-wasm/context.h +++ b/include/proxy-wasm/context.h @@ -239,10 +239,12 @@ class ContextBase : public RootInterface, } uint32_t getLogLevel() override { return static_cast(LogLevel::info); } uint64_t getCurrentTimeNanoseconds() override { - return std::chrono::system_clock::now().time_since_epoch().count(); + unimplemented(); + return 0; } uint64_t getMonotonicTimeNanoseconds() override { - return std::chrono::steady_clock::now().time_since_epoch().count(); + unimplemented(); + return 0; } std::string_view getConfiguration() override { unimplemented(); diff --git a/test/exports_test.cc b/test/exports_test.cc index aab95fcab..694184d7f 100644 --- a/test/exports_test.cc +++ b/test/exports_test.cc @@ -107,12 +107,7 @@ TEST_P(TestVM, Clock) { WasmCallVoid<0> run; wasm_base.wasm_vm()->getFunction("run", &run); ASSERT_TRUE(run); - run(current_context_); - - // Check logs. - auto msg = context.log_msg(); - EXPECT_NE(std::string::npos, msg.find("monotonic: ")) << msg; - EXPECT_NE(std::string::npos, msg.find("realtime: ")) << msg; + EXPECT_DEATH(run(current_context_), "unimplemented proxy-wasm API"); } } // namespace From 4653dadc06db266482e7f2c2cc2f93699a235c8a Mon Sep 17 00:00:00 2001 From: chaoqin-li1123 Date: Wed, 2 Mar 2022 20:45:42 +0000 Subject: [PATCH 2/3] implement time interface for test context Signed-off-by: chaoqin-li1123 --- include/proxy-wasm/context.h | 5 ++++- test/exports_test.cc | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/proxy-wasm/context.h b/include/proxy-wasm/context.h index 64e46690f..652b86b6e 100644 --- a/include/proxy-wasm/context.h +++ b/include/proxy-wasm/context.h @@ -237,7 +237,10 @@ class ContextBase : public RootInterface, WasmResult log(uint32_t /* level */, std::string_view /* message */) override { return unimplemented(); } - uint32_t getLogLevel() override { return static_cast(LogLevel::info); } + uint32_t getLogLevel() override { + unimplemented(); + return 0; + } uint64_t getCurrentTimeNanoseconds() override { unimplemented(); return 0; diff --git a/test/exports_test.cc b/test/exports_test.cc index 694184d7f..a57f0ee48 100644 --- a/test/exports_test.cc +++ b/test/exports_test.cc @@ -38,6 +38,12 @@ INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVM, testing::ValuesIn(getWasmEngines() class TestContext : public ContextBase { public: TestContext(WasmBase *base) : ContextBase(base){}; + uint64_t getCurrentTimeNanoseconds() override { + return std::chrono::system_clock::now().time_since_epoch().count(); + } + uint64_t getMonotonicTimeNanoseconds() override { + return std::chrono::steady_clock::now().time_since_epoch().count(); + } WasmResult log(uint32_t /*log_level*/, std::string_view msg) override { log_ += std::string(msg) + "\n"; return WasmResult::Ok; @@ -107,7 +113,12 @@ TEST_P(TestVM, Clock) { WasmCallVoid<0> run; wasm_base.wasm_vm()->getFunction("run", &run); ASSERT_TRUE(run); - EXPECT_DEATH(run(current_context_), "unimplemented proxy-wasm API"); + run(current_context_); + + // Check logs. + auto msg = context.log_msg(); + EXPECT_NE(std::string::npos, msg.find("monotonic: ")) << msg; + EXPECT_NE(std::string::npos, msg.find("realtime: ")) << msg; } } // namespace From bce3ba13662337b9de48432f914a56a1299e3812 Mon Sep 17 00:00:00 2001 From: chaoqin-li1123 Date: Wed, 2 Mar 2022 22:40:36 +0000 Subject: [PATCH 3/3] add duration cast Signed-off-by: chaoqin-li1123 --- test/utility.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/utility.h b/test/utility.h index 68ed993b7..d5d2d2f42 100644 --- a/test/utility.h +++ b/test/utility.h @@ -99,10 +99,14 @@ class TestContext : public ContextBase { bool isLogged(std::string_view message) { return log_.find(message) != std::string::npos; } uint64_t getCurrentTimeNanoseconds() override { - return std::chrono::system_clock::now().time_since_epoch().count(); + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); } uint64_t getMonotonicTimeNanoseconds() override { - return std::chrono::steady_clock::now().time_since_epoch().count(); + return std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()) + .count(); } private: