-
Notifications
You must be signed in to change notification settings - Fork 32
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
Issue 4 #5
Issue 4 #5
Changes from 1 commit
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,9 +1,6 @@ | ||
package ly.bit.nsq; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.*; | ||
import java.util.Arrays; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
@@ -61,7 +58,7 @@ public void messageReceivedCallback(Message message){ | |
public abstract void readForever() throws NSQException; | ||
public abstract void close(); | ||
|
||
public Message decodeMesage(byte[] data) throws NSQException { | ||
public Message decodeMessage(byte[] data) throws NSQException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha! Nice catch |
||
DataInputStream ds = new DataInputStream(new ByteArrayInputStream(data)); | ||
try { | ||
long timestamp = ds.readLong(); // 8 bytes | ||
|
@@ -76,6 +73,27 @@ public Message decodeMesage(byte[] data) throws NSQException { | |
} | ||
} | ||
|
||
/** | ||
* Reverse of decodeMessage, helpful in testing so far. | ||
* @param msg | ||
* @return | ||
* @throws NSQException | ||
*/ | ||
public byte[] encodeMessage(Message msg) throws NSQException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this doesn't depend on any fields of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe a MessageCodec class |
||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
DataOutputStream ds = new DataOutputStream(bytes); | ||
try { | ||
ds.writeLong(msg.getTimestamp()); | ||
ds.writeShort(msg.getAttempts()); | ||
ds.write(msg.getId()); | ||
ds.write(msg.getBody()); | ||
ds.close(); | ||
} catch (IOException e) { | ||
throw new NSQException(e); | ||
} | ||
return bytes.toByteArray(); | ||
} | ||
|
||
public void handleResponse(byte[] response) throws NSQException { | ||
DataInputStream ds = new DataInputStream(new ByteArrayInputStream(response)); | ||
try { | ||
|
@@ -86,7 +104,7 @@ public void handleResponse(byte[] response) throws NSQException { | |
break; | ||
case FRAMETYPEMESSAGE: | ||
byte[] messageBytes = Arrays.copyOfRange(response, 4, response.length); | ||
Message msg = this.decodeMesage(messageBytes); | ||
Message msg = this.decodeMessage(messageBytes); | ||
this.messageReceivedCallback(msg); | ||
break; | ||
case FRAMETYPEERROR: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,5 +54,4 @@ public Connection getConn() { | |
return conn; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ly.bit.nsq; | ||
|
||
import ly.bit.nsq.exceptions.NSQException; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* User: oneill | ||
* Date: 4/8/13 | ||
*/ | ||
public class ConnectionTest { | ||
Logger log = LoggerFactory.getLogger(ConnectionTest.class); | ||
|
||
|
||
/** | ||
* Test that we can encode and decode a message properly. | ||
* @throws NSQException | ||
*/ | ||
@Test | ||
public void testCodec() throws NSQException { | ||
|
||
byte[] id = MockConnection.randomId(); | ||
|
||
|
||
Connection conn = new MockConnection(); | ||
String body = "kjehfliuANDY.WAS.HEREe;flijwe,jfhwqliuehfj"; | ||
Message msg = new Message(id, body.getBytes(), System.currentTimeMillis()*1000, | ||
new Integer(0).shortValue(), conn); | ||
byte[] encoded = conn.encodeMessage(msg); | ||
log.debug("Encoded message: {}", encoded); | ||
|
||
Message decoded = conn.decodeMessage(encoded); | ||
assertEquals(msg.getAttempts(), decoded.getAttempts()); | ||
for (int i=0; i<id.length; i++) { | ||
assertEquals(id[i], decoded.getId()[i]); | ||
} | ||
assertEquals(new String(msg.getBody()), new String(decoded.getBody())); | ||
assertEquals(msg.getTimestamp(), decoded.getTimestamp()); | ||
|
||
byte[] reenecoded = conn.encodeMessage(decoded); | ||
assertEquals(encoded.length, reenecoded.length); | ||
for (int i=0; i<reenecoded.length; i++) { | ||
assertEquals(encoded[i], reenecoded[i]); | ||
} | ||
|
||
} | ||
} |
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.
Can we not import * here? We're pretty strict here at Bitly about explicit imports
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.
That must be an IDE setting. I will find it and kill it.