leekeiabstraction commented on code in PR #352:
URL: https://github.com/apache/fluss-rust/pull/352#discussion_r2830400401


##########
bindings/cpp/test/test_utils.h:
##########
@@ -0,0 +1,342 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <chrono>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <thread>
+#include <vector>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#pragma comment(lib, "ws2_32.lib")
+#else
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#endif
+
+#include "fluss.hpp"
+
+// Macro to assert Result is OK and print error message on failure
+#define ASSERT_OK(result) ASSERT_TRUE((result).Ok()) << (result).error_message
+#define EXPECT_OK(result) EXPECT_TRUE((result).Ok()) << (result).error_message
+
+namespace fluss_test {
+
+static constexpr const char* kFlussVersion = "0.7.0";
+static constexpr const char* kNetworkName = "fluss-cpp-test-network";
+static constexpr const char* kZookeeperName = "zookeeper-cpp-test";
+static constexpr const char* kCoordinatorName = "coordinator-server-cpp-test";
+static constexpr const char* kTabletServerName = "tablet-server-cpp-test";
+static constexpr int kCoordinatorPort = 9123;
+static constexpr int kTabletServerPort = 9124;
+
+/// Execute a shell command and return its exit code.
+inline int RunCommand(const std::string& cmd) {
+    return system(cmd.c_str());
+}
+
+/// Wait until a TCP port is accepting connections, or timeout.
+inline bool WaitForPort(const std::string& host, int port, int timeout_seconds 
= 60) {
+    auto deadline =
+        std::chrono::steady_clock::now() + 
std::chrono::seconds(timeout_seconds);
+
+    while (std::chrono::steady_clock::now() < deadline) {
+        int sock = socket(AF_INET, SOCK_STREAM, 0);
+        if (sock < 0) {
+            std::this_thread::sleep_for(std::chrono::milliseconds(500));
+            continue;
+        }
+
+        struct sockaddr_in addr {};
+        addr.sin_family = AF_INET;
+        addr.sin_port = htons(static_cast<uint16_t>(port));
+        inet_pton(AF_INET, host.c_str(), &addr.sin_addr);
+
+        int result = connect(sock, reinterpret_cast<struct sockaddr*>(&addr), 
sizeof(addr));
+#ifdef _WIN32
+        closesocket(sock);
+#else
+        close(sock);
+#endif
+        if (result == 0) {
+            return true;
+        }
+
+        std::this_thread::sleep_for(std::chrono::milliseconds(500));
+    }
+    return false;
+}
+
+/// Manages a Docker-based Fluss cluster for integration testing.
+class FlussTestCluster {
+   public:
+    FlussTestCluster() = default;
+
+    bool Start() {
+        const char* env_servers = std::getenv("FLUSS_BOOTSTRAP_SERVERS");
+        if (env_servers && std::strlen(env_servers) > 0) {
+            bootstrap_servers_ = env_servers;
+            external_cluster_ = true;
+            std::cout << "Using external cluster: " << bootstrap_servers_ << 
std::endl;
+            return true;
+        }
+
+        std::cout << "Starting Fluss cluster via Docker..." << std::endl;
+
+        // Create network
+        RunCommand(std::string("docker network create ") + kNetworkName + " 
2>/dev/null || true");
+
+        // Start ZooKeeper
+        std::string zk_cmd = std::string("docker run -d --rm") +
+                              " --name " + kZookeeperName +
+                              " --network " + kNetworkName +
+                              " zookeeper:3.9.2";
+        if (RunCommand(zk_cmd) != 0) {
+            std::cerr << "Failed to start ZooKeeper" << std::endl;
+            return false;
+        }
+
+        // Wait for ZooKeeper to be ready before starting Fluss servers
+        std::this_thread::sleep_for(std::chrono::seconds(5));

Review Comment:
   I'd skip polling for readiness for Zookeeper actually, don't want to 
overcomplicate by having to pull in ZK client here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to