-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/byte based messages #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,34 @@ | ||
| package com.ofek.queue; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.UUID; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class Message implements Serializable { | ||
| private final String id; | ||
| private final String payload; | ||
| private final byte[] payload; | ||
|
|
||
| // Constructor for new messages (generates new ID) | ||
| public Message(String payload) { | ||
| this.id = UUID.randomUUID().toString(); | ||
| this.payload = payload; | ||
| // Constructor for new messages | ||
| public Message(byte[] payload) { | ||
| this.payload = payload.clone(); | ||
| } | ||
|
|
||
| // Constructor for loading existing messages (preserves original ID) | ||
| public Message(String id, String payload) { | ||
| this.id = id; | ||
| this.payload = payload; | ||
| // Constructor for String payload | ||
| public Message(String payload) { | ||
| this.payload = payload.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| public byte[] getPayload() { | ||
| return payload.clone(); | ||
| } | ||
|
|
||
| public String getPayload() { | ||
| return payload; | ||
| // Get payload as String | ||
| public String getPayloadAsString() { | ||
| return new String(payload, StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Message{" + | ||
| "id='" + id + '\'' + | ||
| ", payload='" + payload + '\'' + | ||
| ", payload='" + getPayloadAsString() + '\'' + | ||
| '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||||
| import java.io.IOException; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | ||||||
| import java.util.Base64; | ||||||
| import java.util.List; | ||||||
| import java.util.Random; | ||||||
| import java.util.concurrent.CountDownLatch; | ||||||
|
|
@@ -63,9 +64,9 @@ void testBasicEnqueueDequeue() { | |||||
| assertNotNull(second); | ||||||
| assertNotNull(third); | ||||||
|
|
||||||
| assertEquals("First message", first.getPayload()); | ||||||
| assertEquals("Second message", second.getPayload()); | ||||||
| assertEquals("Third message", third.getPayload()); | ||||||
| assertEquals("First message", first.getPayloadAsString()); | ||||||
| assertEquals("Second message", second.getPayloadAsString()); | ||||||
| assertEquals("Third message", third.getPayloadAsString()); | ||||||
|
|
||||||
| assertEquals(0, messageQueue.size()); | ||||||
| } | ||||||
|
|
@@ -100,7 +101,7 @@ void testRandomNumberOfMessages() throws InterruptedException { | |||||
| Message message = consumer.poll(); | ||||||
| if (message != null) { | ||||||
| dequeuedCount++; | ||||||
| assertTrue(message.getPayload().startsWith("Random message")); | ||||||
| assertTrue(message.getPayloadAsString().startsWith("Random message")); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -131,9 +132,8 @@ void testBatchSizePersistence() throws InterruptedException, IOException { | |||||
|
|
||||||
| // Verify file format | ||||||
| for (int i = 0; i < batchSize; i++) { | ||||||
| String line = lines.get(i); | ||||||
| assertTrue(line.contains("Batch message " + i)); | ||||||
| assertTrue(line.contains("|")); // ID|payload format | ||||||
| String decodedMessage = new String(Base64.getDecoder().decode(lines.get(i))); | ||||||
|
||||||
| assertTrue(decodedMessage.contains("Batch message " + i)); | ||||||
| } | ||||||
|
|
||||||
| batchQueue.shutdown(); | ||||||
|
|
@@ -166,7 +166,8 @@ void testLessThanBatchSize() throws InterruptedException, IOException { | |||||
|
|
||||||
| for (int i = 0; i < messageCount; i++) { | ||||||
| String line = lines.get(i); | ||||||
| assertTrue(line.contains("Small batch message " + i)); | ||||||
| String decodedMessage = new String(Base64.getDecoder().decode(line)); | ||||||
|
||||||
| String decodedMessage = new String(Base64.getDecoder().decode(line)); | |
| String decodedMessage = new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8); |
Copilot
AI
Jul 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Base64 decoding should specify the character encoding explicitly. Use new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8) to ensure consistent encoding behavior across different platforms.
| String decodedMessage = new String(Base64.getDecoder().decode(line)); | |
| String decodedMessage = new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8); |
Copilot
AI
Jul 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Base64 decoding should specify the character encoding explicitly. Use new String(Base64.getDecoder().decode(lines.get(0)), StandardCharsets.UTF_8) to ensure consistent encoding behavior across different platforms.
| String decodedMessage = new String(Base64.getDecoder().decode(lines.get(0))); | |
| String decodedMessage = new String(Base64.getDecoder().decode(lines.get(0)), StandardCharsets.UTF_8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an unmatched closing brace. The if statement on line 189 that checks
parts.length == 2was removed, but its closing brace remains, creating a syntax error.