5 changed files with 117 additions and 6 deletions
-
10src/main/java/com/corundumstudio/socketio/parser/Decoder.java
-
6src/main/java/com/corundumstudio/socketio/parser/Packet.java
-
35src/test/java/com/corundumstudio/socketio/parser/DecoderConnectionPacketTest.java
-
31src/test/java/com/corundumstudio/socketio/parser/DecoderMessagePacketTest.java
-
41src/test/java/com/corundumstudio/socketio/parser/PayloadTest.java
@ -0,0 +1,35 @@ |
|||||
|
package com.corundumstudio.socketio.parser; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
import org.codehaus.jackson.map.ObjectMapper; |
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Test; |
||||
|
|
||||
|
public class DecoderConnectionPacketTest { |
||||
|
|
||||
|
private final Decoder decoder = new Decoder(new ObjectMapper()); |
||||
|
|
||||
|
@Test |
||||
|
public void testDecodeHeartbeat() throws IOException { |
||||
|
Packet packet = decoder.decodePacket("2:::"); |
||||
|
Assert.assertEquals(PacketType.HEARTBEAT, packet.getType()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDecode() throws IOException { |
||||
|
Packet packet = decoder.decodePacket("1::/tobi"); |
||||
|
Assert.assertEquals(PacketType.CONNECT, packet.getType()); |
||||
|
Assert.assertEquals("/tobi", packet.getEndpoint()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDecodeWithQueryString() throws IOException { |
||||
|
Packet packet = decoder.decodePacket("1::/test:?test=1"); |
||||
|
Assert.assertEquals(PacketType.CONNECT, packet.getType()); |
||||
|
Assert.assertEquals("/test", packet.getEndpoint()); |
||||
|
Assert.assertEquals("?test=1", packet.getQs()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package com.corundumstudio.socketio.parser; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
import org.codehaus.jackson.map.ObjectMapper; |
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Test; |
||||
|
|
||||
|
public class DecoderMessagePacketTest { |
||||
|
|
||||
|
private final Decoder decoder = new Decoder(new ObjectMapper()); |
||||
|
|
||||
|
@Test |
||||
|
public void testDecode() throws IOException { |
||||
|
Packet packet = decoder.decodePacket("3:::woot"); |
||||
|
Assert.assertEquals(PacketType.MESSAGE, packet.getType()); |
||||
|
Assert.assertEquals("woot", packet.getData()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDecodeWithIdAndEndpoint() throws IOException { |
||||
|
Packet packet = decoder.decodePacket("3:5:/tobi"); |
||||
|
Assert.assertEquals(PacketType.MESSAGE, packet.getType()); |
||||
|
Assert.assertEquals("5", packet.getId()); |
||||
|
Assert.assertEquals(true, packet.getAck()); |
||||
|
Assert.assertEquals("/tobi", packet.getEndpoint()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
@ -0,0 +1,41 @@ |
|||||
|
package com.corundumstudio.socketio.parser; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.util.List; |
||||
|
import java.util.regex.Matcher; |
||||
|
import java.util.regex.Pattern; |
||||
|
|
||||
|
import org.codehaus.jackson.map.ObjectMapper; |
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Test; |
||||
|
|
||||
|
public class PayloadTest { |
||||
|
|
||||
|
private final Decoder decoder = new Decoder(new ObjectMapper()); |
||||
|
|
||||
|
//@Test |
||||
|
public void testPayloadDecodePerf() throws IOException { |
||||
|
long start = System.currentTimeMillis(); |
||||
|
for (int i = 0; i < 50000; i++) { |
||||
|
decoder.decodePayload("\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d\ufffd3\ufffd0::\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d\ufffd3\ufffd0::\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d\ufffd3\ufffd0::\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d\ufffd3\ufffd0::\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d\ufffd3\ufffd0::"); |
||||
|
} |
||||
|
long end = System.currentTimeMillis() - start; |
||||
|
System.out.println(end + "ms"); |
||||
|
// 1143ms |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testPayloadDecode() throws IOException { |
||||
|
List<Packet> payload = decoder.decodePayload("\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d\ufffd3\ufffd0::"); |
||||
|
Assert.assertEquals(3, payload.size()); |
||||
|
Packet msg1 = payload.get(0); |
||||
|
Assert.assertEquals(PacketType.MESSAGE, msg1.getType()); |
||||
|
Assert.assertEquals("5", msg1.getData()); |
||||
|
Packet msg2 = payload.get(1); |
||||
|
Assert.assertEquals(PacketType.MESSAGE, msg2.getType()); |
||||
|
Assert.assertEquals("53d", msg2.getData()); |
||||
|
Packet msg3 = payload.get(2); |
||||
|
Assert.assertEquals(PacketType.DISCONNECT, msg3.getType()); |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue