ryerraguntla commented on code in PR #3519:
URL: https://github.com/apache/iggy/pull/3519#discussion_r3812634511


##########
gateways/kafka/tests/server_integration_tests.rs:
##########
@@ -0,0 +1,179 @@
+// 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.
+
+#[path = "common/codec.rs"]
+mod codec;
+
+use std::time::Duration;
+
+use bytes::{Buf, BufMut, BytesMut};
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
+use tokio::net::{TcpListener, TcpStream};
+
+use codec::Encoder;
+use iggy_gateway_kafka::server::read_frame;
+
+async fn tcp_pair() -> (TcpStream, TcpStream) {
+    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+    let addr = listener.local_addr().unwrap();
+    let client = tokio::spawn(async move { 
TcpStream::connect(addr).await.unwrap() });
+    let (server, _) = listener.accept().await.unwrap();
+    let client = client.await.unwrap();
+    (client, server)
+}
+
+/// Raw length-prefixed write (no Kafka response header) - mirrors 
`server::write_frame`.
+async fn write_length_prefixed(
+    stream: &mut TcpStream,
+    payload: &[u8],
+    write_timeout: Duration,
+) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
+    let len = payload.len();
+    assert!(i32::try_from(len).is_ok());
+    let mut frame = BytesMut::with_capacity(4 + len);
+    frame.put_i32(i32::try_from(len).expect("len fits i32"));
+    frame.extend_from_slice(payload);
+    tokio::time::timeout(write_timeout, stream.write_all(&frame))
+        .await
+        .map_err(|_| std::io::Error::new(std::io::ErrorKind::TimedOut, "write 
timeout"))??;
+    Ok(())
+}
+
+#[tokio::test]
+async fn read_frame_reads_valid_payload() {
+    let (mut client, mut server) = tcp_pair().await;
+
+    let mut enc = Encoder::with_capacity(64);
+    enc.write_i16(18);
+    enc.write_i16(3);
+    enc.write_i32(123);
+    enc.write_nullable_string(Some("test-client")).unwrap();
+    let payload = enc.freeze();
+
+    let mut frame = BytesMut::with_capacity(4 + payload.len());
+    frame.extend_from_slice(
+        &i32::try_from(payload.len())
+            .expect("test payload fits i32")
+            .to_be_bytes(),
+    );
+    frame.extend_from_slice(&payload);
+    client.write_all(&frame).await.unwrap();
+
+    let parsed = read_frame(
+        &mut server,
+        4096,
+        Duration::from_secs(5),
+        Duration::from_secs(1),
+    )
+    .await
+    .unwrap();
+    assert_eq!(parsed, payload);
+}
+
+#[tokio::test]
+async fn write_frame_writes_length_prefixed_payload() {

Review Comment:
   Both tests + the dead write_length_prefixed helper deleted from 
server_integration_tests.rs. Remaining tests are genuine read_frame coverage.



-- 
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