You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by gk...@apache.org on 2025/10/24 06:34:13 UTC

(iggy) branch io_uring_tpc updated: feat (io_uring): WebSocket tests (#2289)

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

gkoszyk pushed a commit to branch io_uring_tpc
in repository https://gitbox.apache.org/repos/asf/iggy.git


The following commit(s) were added to refs/heads/io_uring_tpc by this push:
     new 4a3cbcbf3 feat (io_uring): WebSocket tests (#2289)
4a3cbcbf3 is described below

commit 4a3cbcbf3aa2ab3ce9ce12da01ce3d2c9c47ec8c
Author: Krishna Vishal <kr...@gmail.com>
AuthorDate: Fri Oct 24 12:04:08 2025 +0530

    feat (io_uring): WebSocket tests (#2289)
    
    Summary:
    
    - Enable WebSocket transport for consumre group test scenarios. All
    tests work fine.
    - Add websocket TLS scenario test.
    - Fix websocke throwing non-specific errors (Err). @numinnex I think
    this fixes the bug you've pointed out, where non-specific Err is being
    thrown. Though the tests still fail.
---
 .../websocket_client_config_builder.rs             | 24 ++++++
 core/integration/tests/server/cg.rs                |  4 +-
 core/integration/tests/server/scenarios/mod.rs     |  1 +
 .../server/scenarios/websocket_tls_scenario.rs     | 93 ++++++++++++++++++++++
 core/integration/tests/server/specific.rs          | 49 +++++++++++-
 core/sdk/src/clients/client_builder.rs             | 67 +++++++++++++++-
 core/sdk/src/websocket/websocket_client.rs         | 31 +++++---
 7 files changed, 256 insertions(+), 13 deletions(-)

diff --git a/core/common/src/types/configuration/websocket_config/websocket_client_config_builder.rs b/core/common/src/types/configuration/websocket_config/websocket_client_config_builder.rs
index 2f5d18d9f..27e296f6c 100644
--- a/core/common/src/types/configuration/websocket_config/websocket_client_config_builder.rs
+++ b/core/common/src/types/configuration/websocket_config/websocket_client_config_builder.rs
@@ -114,6 +114,30 @@ impl WebSocketClientConfigBuilder {
         self
     }
 
+    /// Sets whether to use TLS when connecting to the server.
+    pub fn with_tls_enabled(mut self, tls_enabled: bool) -> Self {
+        self.config.tls_enabled = tls_enabled;
+        self
+    }
+
+    /// Sets the domain to use for TLS when connecting to the server.
+    pub fn with_tls_domain(mut self, tls_domain: String) -> Self {
+        self.config.tls_domain = tls_domain;
+        self
+    }
+
+    /// Sets the path to the CA file for TLS.
+    pub fn with_tls_ca_file(mut self, tls_ca_file: String) -> Self {
+        self.config.tls_ca_file = Some(tls_ca_file);
+        self
+    }
+
+    /// Sets whether to validate the TLS certificate.
+    pub fn with_tls_validate_certificate(mut self, tls_validate_certificate: bool) -> Self {
+        self.config.tls_validate_certificate = tls_validate_certificate;
+        self
+    }
+
     /// Builds the WebSocket client configuration.
     pub fn build(self) -> Result<WebSocketClientConfig, IggyError> {
         let addr = self.config.server_address.trim();
diff --git a/core/integration/tests/server/cg.rs b/core/integration/tests/server/cg.rs
index 655aa41be..7588eb1d9 100644
--- a/core/integration/tests/server/cg.rs
+++ b/core/integration/tests/server/cg.rs
@@ -22,10 +22,10 @@ use iggy_common::TransportProtocol;
 use serial_test::parallel;
 use test_case::test_matrix;
 
-// TODO: Add `QUIC` and `WebSocket` transports.
+// TODO: Add `QUIC`.
 // Consumer group scenarios do not support HTTP
 #[test_matrix(
-    [TransportProtocol::Tcp],
+    [TransportProtocol::Tcp, TransportProtocol::WebSocket],
     [
         join_scenario(),
         single_client_scenario(),
diff --git a/core/integration/tests/server/scenarios/mod.rs b/core/integration/tests/server/scenarios/mod.rs
index c1e14af37..05f46901d 100644
--- a/core/integration/tests/server/scenarios/mod.rs
+++ b/core/integration/tests/server/scenarios/mod.rs
@@ -30,6 +30,7 @@ pub mod stream_size_validation_scenario;
 pub mod system_scenario;
 pub mod tcp_tls_scenario;
 pub mod user_scenario;
+pub mod websocket_tls_scenario;
 
 use iggy::prelude::*;
 use integration::test_server::{ClientFactory, delete_user};
diff --git a/core/integration/tests/server/scenarios/websocket_tls_scenario.rs b/core/integration/tests/server/scenarios/websocket_tls_scenario.rs
new file mode 100644
index 000000000..f8f2f2d09
--- /dev/null
+++ b/core/integration/tests/server/scenarios/websocket_tls_scenario.rs
@@ -0,0 +1,93 @@
+/* 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.
+ */
+
+use bytes::Bytes;
+use iggy::prelude::*;
+use integration::test_server::{assert_clean_system, login_root};
+
+pub async fn run(client: &IggyClient) {
+    login_root(client).await;
+
+    let stream_name = "test-websocket-tls-stream";
+    client.create_stream(stream_name).await.unwrap();
+
+    let stream = client
+        .get_stream(&Identifier::named(stream_name).unwrap())
+        .await
+        .unwrap()
+        .unwrap();
+    assert_eq!(stream.name, stream_name);
+
+    let topic_name = "test-websocket-tls-topic";
+    client
+        .create_topic(
+            &Identifier::named(stream_name).unwrap(),
+            topic_name,
+            1,
+            CompressionAlgorithm::default(),
+            None,
+            IggyExpiry::NeverExpire,
+            MaxTopicSize::ServerDefault,
+        )
+        .await
+        .unwrap();
+
+    let mut messages = vec![
+        IggyMessage::builder()
+            .id(1)
+            .payload(Bytes::from("Hello WebSocket TLS!"))
+            .build()
+            .unwrap(),
+    ];
+
+    client
+        .send_messages(
+            &Identifier::named(stream_name).unwrap(),
+            &Identifier::named(topic_name).unwrap(),
+            &Partitioning::partition_id(0),
+            &mut messages,
+        )
+        .await
+        .unwrap();
+
+    let polled_messages = client
+        .poll_messages(
+            &Identifier::named(stream_name).unwrap(),
+            &Identifier::named(topic_name).unwrap(),
+            Some(0),
+            &Consumer::default(),
+            &PollingStrategy::offset(0),
+            1,
+            true,
+        )
+        .await
+        .unwrap();
+
+    assert_eq!(polled_messages.messages.len(), 1);
+    assert_eq!(
+        polled_messages.messages[0].payload.as_ref(),
+        b"Hello WebSocket TLS!"
+    );
+
+    client
+        .delete_stream(&Identifier::named(stream_name).unwrap())
+        .await
+        .unwrap();
+
+    assert_clean_system(client).await;
+}
diff --git a/core/integration/tests/server/specific.rs b/core/integration/tests/server/specific.rs
index d48517681..205d6b5df 100644
--- a/core/integration/tests/server/specific.rs
+++ b/core/integration/tests/server/specific.rs
@@ -16,7 +16,9 @@
  * under the License.
  */
 
-use crate::server::scenarios::{delete_segments_scenario, message_size_scenario, tcp_tls_scenario};
+use crate::server::scenarios::{
+    delete_segments_scenario, message_size_scenario, tcp_tls_scenario, websocket_tls_scenario,
+};
 use iggy::prelude::*;
 use integration::{
     tcp_client::TcpClientFactory,
@@ -128,6 +130,51 @@ async fn tcp_tls_self_signed_scenario_should_be_valid() {
     tcp_tls_scenario::run(&client).await;
 }
 
+#[tokio::test]
+#[parallel]
+async fn websocket_tls_scenario_should_be_valid() {
+    let temp_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
+    let cert_dir = temp_dir.path();
+    let cert_dir_str = cert_dir.to_str().unwrap();
+
+    generate_test_certificates(cert_dir_str).expect("Failed to generate test certificates");
+
+    let mut extra_envs = HashMap::new();
+    extra_envs.insert("IGGY_WEBSOCKET_TLS_ENABLED".to_string(), "true".to_string());
+    extra_envs.insert(
+        "IGGY_WEBSOCKET_TLS_CERT_FILE".to_string(),
+        cert_dir.join("test_cert.pem").to_str().unwrap().to_string(),
+    );
+    extra_envs.insert(
+        "IGGY_WEBSOCKET_TLS_KEY_FILE".to_string(),
+        cert_dir.join("test_key.pem").to_str().unwrap().to_string(),
+    );
+
+    let mut test_server = TestServer::new(Some(extra_envs), true, None, IpAddrKind::V4);
+    test_server.start();
+
+    let server_addr = test_server.get_websocket_addr().unwrap();
+    let cert_path = cert_dir.join("test_cert.pem").to_str().unwrap().to_string();
+
+    let client = IggyClientBuilder::new()
+        .with_websocket()
+        .with_server_address(server_addr)
+        .with_tls_enabled(true)
+        .with_tls_domain("localhost".to_string())
+        .with_tls_ca_file(cert_path)
+        .build()
+        .expect("Failed to create WebSocket TLS client");
+
+    client
+        .connect()
+        .await
+        .expect("Failed to connect WebSocket TLS client");
+
+    let client = IggyClient::create(ClientWrapper::Iggy(client), None, None);
+
+    websocket_tls_scenario::run(&client).await;
+}
+
 // Message size scenario is specific to TCP transport to test the behavior around the maximum message size.
 // When run on other transports, it will fail because both QUIC and HTTP have different message size limits.
 #[tokio::test]
diff --git a/core/sdk/src/clients/client_builder.rs b/core/sdk/src/clients/client_builder.rs
index 08199ce3b..7aadfa61b 100644
--- a/core/sdk/src/clients/client_builder.rs
+++ b/core/sdk/src/clients/client_builder.rs
@@ -21,7 +21,7 @@ use crate::clients::client::IggyClient;
 use crate::http::http_client::HttpClient;
 use crate::prelude::{
     AutoLogin, EncryptorKind, HttpClientConfigBuilder, IggyDuration, IggyError, Partitioner,
-    QuicClientConfigBuilder, TcpClientConfigBuilder,
+    QuicClientConfigBuilder, TcpClientConfigBuilder, WebSocketClientConfigBuilder,
 };
 use crate::quic::quic_client::QuicClient;
 use crate::tcp::tcp_client::TcpClient;
@@ -123,6 +123,16 @@ impl IggyClientBuilder {
         }
     }
 
+    /// This method provides fluent API for the WebSocket client configuration.
+    /// It returns the `WebSocketClientBuilder` instance, which allows to configure the WebSocket client with custom settings or using defaults.
+    /// This should be called after the non-protocol specific methods, such as `with_partitioner`, `with_encryptor` or `with_message_handler`.
+    pub fn with_websocket(self) -> WebSocketClientBuilder {
+        WebSocketClientBuilder {
+            config: WebSocketClientConfigBuilder::default(),
+            parent_builder: self,
+        }
+    }
+
     /// Build the `IggyClient` instance.
     /// This method returns an error if the client is not provided.
     /// If the client is provided, it creates the `IggyClient` instance with the provided configuration.
@@ -297,6 +307,61 @@ impl HttpClientBuilder {
     }
 }
 
+pub struct WebSocketClientBuilder {
+    config: WebSocketClientConfigBuilder,
+    parent_builder: IggyClientBuilder,
+}
+
+impl WebSocketClientBuilder {
+    /// Sets the server address for the WebSocket client.
+    pub fn with_server_address(mut self, server_address: String) -> Self {
+        self.config = self.config.with_server_address(server_address);
+        self
+    }
+
+    /// Sets the auto sign in during connection.
+    pub fn with_auto_sign_in(mut self, auto_sign_in: AutoLogin) -> Self {
+        self.config = self.config.with_auto_sign_in(auto_sign_in);
+        self
+    }
+
+    /// Sets whether to use TLS when connecting to the server.
+    pub fn with_tls_enabled(mut self, tls_enabled: bool) -> Self {
+        self.config = self.config.with_tls_enabled(tls_enabled);
+        self
+    }
+
+    /// Sets the domain to use for TLS when connecting to the server.
+    pub fn with_tls_domain(mut self, tls_domain: String) -> Self {
+        self.config = self.config.with_tls_domain(tls_domain);
+        self
+    }
+
+    /// Sets the path to the CA file for TLS.
+    pub fn with_tls_ca_file(mut self, tls_ca_file: String) -> Self {
+        self.config = self.config.with_tls_ca_file(tls_ca_file);
+        self
+    }
+
+    /// Sets whether to validate the TLS certificate.
+    pub fn with_tls_validate_certificate(mut self, tls_validate_certificate: bool) -> Self {
+        self.config = self
+            .config
+            .with_tls_validate_certificate(tls_validate_certificate);
+        self
+    }
+
+    /// Builds the parent `IggyClient` with WebSocket configuration.
+    pub fn build(self) -> Result<IggyClient, IggyError> {
+        let client = WebSocketClient::create(Arc::new(self.config.build()?))?;
+        let client = self
+            .parent_builder
+            .with_client(ClientWrapper::WebSocket(client))
+            .build()?;
+        Ok(client)
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/core/sdk/src/websocket/websocket_client.rs b/core/sdk/src/websocket/websocket_client.rs
index 3f34351cc..5556752ee 100644
--- a/core/sdk/src/websocket/websocket_client.rs
+++ b/core/sdk/src/websocket/websocket_client.rs
@@ -28,7 +28,8 @@ use bytes::{BufMut, Bytes, BytesMut};
 use iggy_binary_protocol::{BinaryClient, BinaryTransport, PersonalAccessTokenClient, UserClient};
 use iggy_common::{
     AutoLogin, ClientState, Command, ConnectionString, Credentials, DiagnosticEvent, IggyDuration,
-    IggyError, IggyTimestamp, WebSocketClientConfig, WebSocketConnectionStringOptions,
+    IggyError, IggyErrorDiscriminants, IggyTimestamp, WebSocketClientConfig,
+    WebSocketConnectionStringOptions,
 };
 use std::net::SocketAddr;
 use std::sync::Arc;
@@ -543,15 +544,27 @@ impl WebSocketClient {
         );
 
         if status != 0 {
-            if length > 0 {
-                let mut error_buffer = vec![0u8; length];
-                stream.read(&mut error_buffer).await?;
-                let _error_message = String::from_utf8(error_buffer).unwrap_or_default();
-                // FIXME: add error message
-                return Err(IggyError::Error);
+            // TEMP: See https://github.com/apache/iggy/pull/604 for context.
+            if status == IggyErrorDiscriminants::TopicNameAlreadyExists as u32
+                || status == IggyErrorDiscriminants::StreamNameAlreadyExists as u32
+                || status == IggyErrorDiscriminants::UserAlreadyExists as u32
+                || status == IggyErrorDiscriminants::PersonalAccessTokenAlreadyExists as u32
+                || status == IggyErrorDiscriminants::ConsumerGroupNameAlreadyExists as u32
+            {
+                debug!(
+                    "Received a server resource already exists response: {} ({})",
+                    status,
+                    IggyError::from_code_as_string(status)
+                )
+            } else {
+                error!(
+                    "Received an invalid response with status: {} ({}).",
+                    status,
+                    IggyError::from_code_as_string(status),
+                );
             }
-            // FIXME: add error message
-            return Err(IggyError::Error);
+
+            return Err(IggyError::from_code(status));
         }
 
         if length == 0 {