Fixed another bug, caused by pushing messages with blanks at the end; Updated tests
This commit is contained in:
parent
8fe059d674
commit
3dfbc5c71f
@ -20,18 +20,20 @@ public class BufferedNetworkStackClient {
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
}
|
||||
|
||||
public void push(String s, String host, int port) throws IOException,
|
||||
public void push(String text, String host, int port) throws IOException,
|
||||
TimeoutException {
|
||||
if (channel == null) {
|
||||
channel = DatagramChannel.open();
|
||||
}
|
||||
|
||||
String trimmed = text.trim();
|
||||
|
||||
InetSocketAddress dest = new InetSocketAddress(host, port);
|
||||
|
||||
buf = ByteBuffer.allocate(BUF_SIZE);
|
||||
buf.put(MessageType.CMD_PUSH);
|
||||
|
||||
buf.putInt(s.length());
|
||||
buf.putInt(trimmed.length());
|
||||
|
||||
buf.flip();
|
||||
channel.send(buf, dest);
|
||||
@ -54,7 +56,7 @@ public class BufferedNetworkStackClient {
|
||||
int remoteBufSize = buf.getInt();
|
||||
int sessionId = buf.getInt();
|
||||
|
||||
sendChunks(s, dest, remoteBufSize, sessionId);
|
||||
sendChunks(trimmed, dest, remoteBufSize, sessionId);
|
||||
}
|
||||
|
||||
private void sendChunks(String s, InetSocketAddress dest, int bufSize,
|
||||
|
||||
@ -12,10 +12,8 @@ public class Util {
|
||||
* @return The number of chunks needed
|
||||
*/
|
||||
public static int numChunks(int bufSize, int length) {
|
||||
/*
|
||||
* We have to do bufSize - 9 here, because we have 9 bytes of meta data
|
||||
* in every packet.
|
||||
*/
|
||||
// We have to do bufSize - 9 here, because we have 9 bytes of meta data
|
||||
// in every packet
|
||||
return (int) Math.ceil(length * 1.0 / actualData(bufSize));
|
||||
}
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@ public class BufferedNetworkStackTests {
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String uuid = UUID.randomUUID().toString().trim();
|
||||
values.add(uuid);
|
||||
|
||||
try {
|
||||
@ -76,7 +76,37 @@ public class BufferedNetworkStackTests {
|
||||
for (int j = 0; j < numOfIds; j++) {
|
||||
uuidBuilder.append(UUID.randomUUID().toString());
|
||||
}
|
||||
String uuid = uuidBuilder.toString();
|
||||
String uuid = uuidBuilder.toString().trim();
|
||||
|
||||
values.add(uuid);
|
||||
try {
|
||||
client.push(uuid, HOST, PORT);
|
||||
} catch (IOException e) {
|
||||
fail("Couldn't send to server. " + e);
|
||||
} catch (TimeoutException e) {
|
||||
fail("Connection timed out. " + e);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = values.size() - 1; i >= 0; i--) {
|
||||
try {
|
||||
assertEquals(values.get(i), client.pop(HOST, PORT));
|
||||
} catch (IOException | TimeoutException e) {
|
||||
fail("Couldn't send to server" + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomLongLengthWithSpaces() {
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder uuidBuilder = new StringBuilder(4 * 128);
|
||||
for (int j = 0; j < 4; j++) {
|
||||
uuidBuilder.append(UUID.randomUUID().toString()).append(" ");
|
||||
}
|
||||
String uuid = uuidBuilder.toString().trim();
|
||||
|
||||
values.add(uuid);
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user