This is an automated email from the ASF dual-hosted git repository.

lizhanhui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 3138f6c  Add tests back (#150)
3138f6c is described below

commit 3138f6c477a372a32a86685f3bd80417ed5205c4
Author: Zhanhui Li <[email protected]>
AuthorDate: Fri Aug 12 18:28:59 2022 +0800

    Add tests back (#150)
    
    * Add scheduler test
    
    * Add tests for admin
    
    * Add back more tests
---
 cpp/source/admin/tests/AdminServerTest.cpp      |  65 +++++++++++
 cpp/source/admin/tests/BUILD.bazel              |  28 +++++
 cpp/source/base/tests/BUILD.bazel               |  34 ++++++
 cpp/source/base/tests/InvocationContextTest.cpp |  37 ++++++
 cpp/source/base/tests/ThreadPoolTest.cpp        |  76 ++++++++++++
 cpp/source/base/tests/UniqueIdGeneratorTest.cpp |  46 ++++++++
 cpp/source/scheduler/tests/BUILD.bazel          |  30 +++++
 cpp/source/scheduler/tests/SchedulerTest.cpp    | 149 ++++++++++++++++++++++++
 8 files changed, 465 insertions(+)

diff --git a/cpp/source/admin/tests/AdminServerTest.cpp 
b/cpp/source/admin/tests/AdminServerTest.cpp
new file mode 100644
index 0000000..a5d0213
--- /dev/null
+++ b/cpp/source/admin/tests/AdminServerTest.cpp
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <gtest/gtest.h>
+
+#include <thread>
+
+#include "AdminServerImpl.h"
+#include "apache/rocketmq/v2/admin.grpc.pb.h"
+#include "rocketmq/RocketMQ.h"
+#include "spdlog/sinks/basic_file_sink.h"
+#include "spdlog/spdlog.h"
+
+namespace rmq = apache::rocketmq::v2;
+
+ROCKETMQ_NAMESPACE_BEGIN
+namespace admin {
+
+TEST(AdminServerTest, testSetUp) {
+  auto logger = spdlog::basic_logger_mt("rocketmq_logger", "logs/test.log");
+  logger->set_level(spdlog::level::debug);
+  spdlog::set_default_logger(logger);
+
+  AdminServer* admin_server = new AdminServerImpl;
+  admin_server->start();
+
+  std::string address("127.0.0.1:");
+  address.append(std::to_string(admin_server->port()));
+  auto channel = grpc::CreateChannel(address, 
grpc::InsecureChannelCredentials());
+
+  auto stub = rmq::Admin::NewStub(channel);
+
+  rmq::ChangeLogLevelRequest request;
+  request.set_level(rmq::ChangeLogLevelRequest_Level_INFO);
+  rmq::ChangeLogLevelResponse response;
+
+  grpc::ClientContext context;
+  context.set_deadline(std::chrono::system_clock::now() + 
std::chrono::seconds(10));
+
+  auto status = stub->ChangeLogLevel(&context, request, &response);
+
+  EXPECT_TRUE(status.ok());
+  EXPECT_STREQ("OK", response.remark().c_str());
+  EXPECT_EQ(spdlog::level::info, logger->level());
+
+  admin_server->stop();
+
+  delete admin_server;
+}
+
+}  // namespace admin
+ROCKETMQ_NAMESPACE_END
\ No newline at end of file
diff --git a/cpp/source/admin/tests/BUILD.bazel 
b/cpp/source/admin/tests/BUILD.bazel
new file mode 100644
index 0000000..7e0920d
--- /dev/null
+++ b/cpp/source/admin/tests/BUILD.bazel
@@ -0,0 +1,28 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+# 
+#     http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+load("@rules_cc//cc:defs.bzl", "cc_test")
+
+cc_test(
+    name = "admin_server_test",
+    srcs = [
+        "AdminServerTest.cpp",
+    ],
+    deps = [
+        "//source/admin:admin_server_library",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
\ No newline at end of file
diff --git a/cpp/source/base/tests/BUILD.bazel 
b/cpp/source/base/tests/BUILD.bazel
index 4037b9d..3159f36 100644
--- a/cpp/source/base/tests/BUILD.bazel
+++ b/cpp/source/base/tests/BUILD.bazel
@@ -51,4 +51,38 @@ cc_test(
         "RetryPolicyTest.cpp",
     ],
     deps = base_deps,
+)
+
+cc_test(
+    name = "unique_id_generator_test",
+    srcs = [
+        "UniqueIdGeneratorTest.cpp",
+    ],
+    deps = [
+        "//source/base:base_library",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_test(
+    name = "thread_pool_test",
+    srcs = [
+        "ThreadPoolTest.cpp",
+    ],
+    deps = [
+        "//include:rocketmq_interface",
+        "//source/base:base_library",
+        "@com_google_googletest//:gtest_main",        
+    ],
+)
+
+cc_test(
+    name = "invocation_context_test",
+    srcs = [
+        "InvocationContextTest.cpp",
+    ],
+    deps = [
+        "//source/client:client_library",
+        "@com_google_googletest//:gtest_main",
+    ],
 )
\ No newline at end of file
diff --git a/cpp/source/base/tests/InvocationContextTest.cpp 
b/cpp/source/base/tests/InvocationContextTest.cpp
new file mode 100644
index 0000000..91212e5
--- /dev/null
+++ b/cpp/source/base/tests/InvocationContextTest.cpp
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "InvocationContext.h"
+#include "RpcClient.h"
+#include "gtest/gtest.h"
+
+ROCKETMQ_NAMESPACE_BEGIN
+
+TEST(InvocationContextTest, testOnCompletion_OK) {
+  bool cb_invoked = false;
+  auto callback = [&](const InvocationContext<SendMessageResponse>* 
invocation_context) {
+    cb_invoked = true;
+    throw 1;
+  };
+
+  auto invocation_context = new InvocationContext<SendMessageResponse>();
+  invocation_context->status = 
grpc::Status(grpc::StatusCode::DEADLINE_EXCEEDED, "Mock deadline exceeded");
+  invocation_context->callback = callback;
+  invocation_context->onCompletion(true);
+  EXPECT_TRUE(cb_invoked);
+}
+
+ROCKETMQ_NAMESPACE_END
\ No newline at end of file
diff --git a/cpp/source/base/tests/ThreadPoolTest.cpp 
b/cpp/source/base/tests/ThreadPoolTest.cpp
new file mode 100644
index 0000000..9cbde6c
--- /dev/null
+++ b/cpp/source/base/tests/ThreadPoolTest.cpp
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "ThreadPoolImpl.h"
+#include "absl/memory/memory.h"
+#include "absl/synchronization/mutex.h"
+#include "rocketmq/RocketMQ.h"
+#include "gtest/gtest.h"
+#include <atomic>
+#include <functional>
+#include <thread>
+
+ROCKETMQ_NAMESPACE_BEGIN
+
+class ThreadPoolTest : public testing::Test {
+public:
+  ThreadPoolTest() = default;
+
+  void SetUp() override {
+    pool_ = absl::make_unique<ThreadPoolImpl>(2);
+    pool_->start();
+    completed = false;
+  }
+
+  void TearDown() override {
+    pool_->shutdown();
+  }
+
+protected:
+  std::unique_ptr<ThreadPool> pool_;
+  absl::Mutex mtx;
+  absl::CondVar cv;
+  bool completed{false};
+};
+
+TEST_F(ThreadPoolTest, testBasics) {
+
+  auto task = [this](int cnt) {
+    for (int i = 0; i < cnt; i++) {
+      std::cout << std::this_thread::get_id() << ": It works" << std::endl;
+    }
+    {
+      absl::MutexLock lk(&mtx);
+      if (!completed) {
+        completed = true;
+        cv.SignalAll();
+      }
+    }
+  };
+
+  for (int i = 0; i < 3; i++) {
+    pool_->submit(std::bind(task, 3));
+  }
+
+  {
+    absl::MutexLock lk(&mtx);
+    if (!completed) {
+      cv.Wait(&mtx);
+    }
+  }
+}
+
+ROCKETMQ_NAMESPACE_END
diff --git a/cpp/source/base/tests/UniqueIdGeneratorTest.cpp 
b/cpp/source/base/tests/UniqueIdGeneratorTest.cpp
new file mode 100644
index 0000000..d78e134
--- /dev/null
+++ b/cpp/source/base/tests/UniqueIdGeneratorTest.cpp
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "UniqueIdGenerator.h"
+#include "absl/container/flat_hash_set.h"
+#include "rocketmq/RocketMQ.h"
+#include "spdlog/spdlog.h"
+#include "gtest/gtest.h"
+
+#include <iostream>
+ROCKETMQ_NAMESPACE_BEGIN
+
+TEST(UniqueIdGeneratorTest, testOutputSampleId) {
+  std::cout << "A sample unique ID: " << UniqueIdGenerator::instance().next() 
<< std::endl;
+}
+
+TEST(UniqueIdGeneratorTest, testNext) {
+  absl::flat_hash_set<std::string> id_set;
+  uint32_t total = 500000;
+  uint32_t count = 0;
+  while (total--) {
+    std::string id = UniqueIdGenerator::instance().next();
+    if (id_set.contains(id)) {
+      SPDLOG_WARN("Yuck, found an duplicated ID: {}", id);
+    } else {
+      id_set.insert(id);
+    }
+    ++count;
+  }
+  EXPECT_EQ(count, id_set.size());
+}
+
+ROCKETMQ_NAMESPACE_END
\ No newline at end of file
diff --git a/cpp/source/scheduler/tests/BUILD.bazel 
b/cpp/source/scheduler/tests/BUILD.bazel
new file mode 100644
index 0000000..32d347b
--- /dev/null
+++ b/cpp/source/scheduler/tests/BUILD.bazel
@@ -0,0 +1,30 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+# 
+#     http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+load("@rules_cc//cc:defs.bzl", "cc_test")
+cc_test(
+    name = "scheduler_test",
+    srcs = [
+        "SchedulerTest.cpp",
+    ],
+    deps = [
+        "//include:rocketmq_interface",
+        "//source/base:base_library",
+        "//source/concurrent:countdown_latch_library",
+        "//source/scheduler:scheduler_library",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
\ No newline at end of file
diff --git a/cpp/source/scheduler/tests/SchedulerTest.cpp 
b/cpp/source/scheduler/tests/SchedulerTest.cpp
new file mode 100644
index 0000000..19a3dae
--- /dev/null
+++ b/cpp/source/scheduler/tests/SchedulerTest.cpp
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <chrono>
+#include <cstdint>
+#include <exception>
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <thread>
+
+#include "SchedulerImpl.h"
+#include "gtest/gtest.h"
+
+ROCKETMQ_NAMESPACE_BEGIN
+
+class SchedulerTest : public testing::Test {
+public:
+  SchedulerTest() : scheduler(std::make_shared<SchedulerImpl>()) {
+  }
+
+  void SetUp() override {
+    scheduler->start();
+  }
+
+  void TearDown() override {
+    scheduler->shutdown();
+  }
+
+protected:
+  SchedulerSharedPtr scheduler;
+};
+
+TEST_F(SchedulerTest, testSingleShot) {
+  absl::Mutex mtx;
+  absl::CondVar cv;
+  int callback_fire_count{0};
+  auto callback = [&]() {
+    absl::MutexLock lock(&mtx);
+    cv.Signal();
+    callback_fire_count++;
+  };
+
+  scheduler->schedule(callback, "single-shot", std::chrono::milliseconds(10), 
std::chrono::milliseconds(0));
+
+  // Wait till callback is executed.
+  {
+    absl::MutexLock lock(&mtx);
+    if (!callback_fire_count) {
+      cv.Wait(&mtx);
+    }
+  }
+}
+
+TEST_F(SchedulerTest, testCancel) {
+  absl::Mutex mtx;
+  absl::CondVar cv;
+  int callback_fire_count{0};
+  auto callback = [&]() {
+    absl::MutexLock lock(&mtx);
+    cv.Signal();
+    callback_fire_count++;
+  };
+
+  std::uint32_t task_id =
+      scheduler->schedule(callback, "test-cancel", 
std::chrono::milliseconds(100), std::chrono::milliseconds(100));
+  scheduler->cancel(task_id);
+  std::this_thread::sleep_for(std::chrono::milliseconds(200));
+  ASSERT_EQ(0, callback_fire_count);
+}
+
+TEST_F(SchedulerTest, testPeriodicShot) {
+  absl::Mutex mtx;
+  absl::CondVar cv;
+  int callback_fire_count{0};
+  auto callback = [&]() {
+    absl::MutexLock lock(&mtx);
+    cv.Signal();
+    callback_fire_count++;
+  };
+
+  std::uintptr_t task_id =
+      scheduler->schedule(callback, "periodic-task", 
std::chrono::milliseconds(10), std::chrono::milliseconds(100));
+  // Wait till callback is executed.
+  std::this_thread::sleep_for(std::chrono::milliseconds(600));
+  ASSERT_TRUE(callback_fire_count >= 4);
+  scheduler->cancel(task_id);
+}
+
+TEST_F(SchedulerTest, testSingleShotWithZeroDelay) {
+  absl::Mutex mtx;
+  absl::CondVar cv;
+  int callback_fire_count{0};
+  auto callback = [&]() {
+    absl::MutexLock lock(&mtx);
+    cv.Signal();
+    callback_fire_count++;
+  };
+
+  scheduler->schedule(callback, "single-shot-with-0-delay", 
std::chrono::milliseconds(0), std::chrono::milliseconds(0));
+
+  // Wait till callback is executed.
+  {
+    absl::MutexLock lock(&mtx);
+    if (!callback_fire_count) {
+      cv.Wait(&mtx);
+    }
+  }
+}
+
+TEST_F(SchedulerTest, testException) {
+  absl::Mutex mtx;
+  absl::CondVar cv;
+  int callback_fire_count{0};
+  auto callback = [&]() {
+    {
+      absl::MutexLock lock(&mtx);
+      cv.Signal();
+      callback_fire_count++;
+    }
+
+    std::exception e;
+    throw e;
+  };
+
+  scheduler->schedule(callback, "test-exception", 
std::chrono::milliseconds(100), std::chrono::milliseconds(100));
+
+  // Wait till callback is executed.
+  {
+    absl::MutexLock lock(&mtx);
+    if (callback_fire_count <= 5) {
+      cv.Wait(&mtx);
+    }
+  }
+}
+ROCKETMQ_NAMESPACE_END
\ No newline at end of file

Reply via email to