use of org.msgpack.core.MessagePacker in project fluency by komamitsu.
the class FluencyTest method testWithAckResponseWithProperToken.
@Theory
public void testWithAckResponseWithProperToken(final boolean sslEnabled) throws Throwable {
Exception exception = new ConfigurableTestServer(sslEnabled).run(new ConfigurableTestServer.WithClientSocket() {
@Override
public void run(Socket clientSocket) throws Exception {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(clientSocket.getInputStream());
assertEquals(3, unpacker.unpackArrayHeader());
assertEquals("foo.bar", unpacker.unpackString());
ImmutableRawValue rawValue = unpacker.unpackValue().asRawValue();
Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
assertEquals(2, map.size());
assertEquals(rawValue.asByteArray().length, map.get(KEY_OPTION_SIZE).asIntegerValue().asInt());
String ackResponseToken = map.get(KEY_OPTION_CHUNK).asRawValue().asString();
assertNotNull(ackResponseToken);
MessagePacker packer = MessagePack.newDefaultPacker(clientSocket.getOutputStream());
packer.packMapHeader(1).packString("ack").packString(ackResponseToken).close();
// Close the input stream after closing the output stream to avoid closing a socket too early
unpacker.close();
}
}, new ConfigurableTestServer.WithServerPort() {
@Override
public void run(int serverPort) throws Exception {
Fluency fluency = Fluency.defaultFluency(serverPort, new Fluency.Config().setSslEnabled(sslEnabled).setAckResponseMode(true));
fluency.emit("foo.bar", new HashMap<String, Object>());
fluency.close();
}
}, 5000);
assertNull(exception);
}
use of org.msgpack.core.MessagePacker in project fluency by komamitsu.
the class PackedForwardBuffer method flushInternal.
@Override
public void flushInternal(Sender sender, boolean force) throws IOException {
moveRetentionBuffersToFlushable(force);
TaggableBuffer flushableBuffer;
ByteArrayOutputStream header = new ByteArrayOutputStream();
MessagePacker messagePacker = MessagePack.newDefaultPacker(header);
while (!Thread.currentThread().isInterrupted() && (flushableBuffer = flushableBuffers.poll()) != null) {
boolean keepBuffer = false;
try {
LOG.trace("flushInternal(): bufferUsage={}, flushableBuffer={}", getBufferUsage(), flushableBuffer);
String tag = flushableBuffer.getTag();
ByteBuffer dataBuffer = flushableBuffer.getByteBuffer();
int dataLength = dataBuffer.limit();
messagePacker.packArrayHeader(3);
messagePacker.packString(tag);
messagePacker.packRawStringHeader(dataLength);
messagePacker.flush();
ByteBuffer headerBuffer = ByteBuffer.wrap(header.toByteArray());
try {
if (config.isAckResponseMode()) {
byte[] uuidBytes = UUID.randomUUID().toString().getBytes(CHARSET);
ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, uuidBytes)));
List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer);
synchronized (sender) {
sender.sendWithAck(buffers, uuidBytes);
}
} else {
ByteBuffer optionBuffer = ByteBuffer.wrap(objectMapper.writeValueAsBytes(new RequestOption(dataLength, null)));
List<ByteBuffer> buffers = Arrays.asList(headerBuffer, dataBuffer, optionBuffer);
synchronized (sender) {
sender.send(buffers);
}
}
} catch (IOException e) {
LOG.warn("Failed to send data. The data is going to be saved into the buffer again: data={}", flushableBuffer);
keepBuffer = true;
throw e;
}
} finally {
header.reset();
if (keepBuffer) {
try {
flushableBuffers.put(flushableBuffer);
} catch (InterruptedException e1) {
LOG.warn("Failed to save the data into the buffer. Trying to save it in extra buffer: chunk={}", flushableBuffer);
backupBuffers.add(flushableBuffer);
}
} else {
bufferPool.returnBuffer(flushableBuffer.getByteBuffer());
}
}
}
}
use of org.msgpack.core.MessagePacker in project zeebe by zeebe-io.
the class MsgPackConverterTest method createMsgPack.
protected static byte[] createMsgPack() {
byte[] msgPack = null;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
final MessagePacker payloadPacker = MessagePack.newDefaultPacker(outputStream);
payloadPacker.packMapHeader(2).packString("key1").packInt(1).packString("key2").packInt(2);
payloadPacker.flush();
msgPack = outputStream.toByteArray();
} catch (Exception e) {
LangUtil.rethrowUnchecked(e);
}
return msgPack;
}
use of org.msgpack.core.MessagePacker in project LogHub by fbacchella.
the class TestMsgpack method testmapsimple.
@Test
public void testmapsimple() throws IOException, DecodeException {
Decoder d = new Msgpack();
Map<Value, Value> destination = new HashMap<>();
destination.put(ValueFactory.newString("a"), ValueFactory.newString("0"));
destination.put(ValueFactory.newString("b"), ValueFactory.newInteger(1));
destination.put(ValueFactory.newString("c"), ValueFactory.newBoolean(false));
Value[] subdestination = new Value[4];
subdestination[0] = ValueFactory.newString("0");
subdestination[1] = ValueFactory.newInteger(1);
subdestination[2] = ValueFactory.newFloat(2.0);
subdestination[3] = ValueFactory.newNil();
destination.put(ValueFactory.newString("d"), ValueFactory.newArray(subdestination));
Value v = ValueFactory.newMap(destination);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = MessagePack.newDefaultPacker(out);
packer.packValue(v);
packer.close();
byte[] packed = out.toByteArray();
Map<String, Object> e = d.decode(ConnectionContext.EMPTY, packed);
testContent(e);
}
use of org.msgpack.core.MessagePacker in project fluency by komamitsu.
the class FluencyTest method testWithAckResponseButWrongReceiveToken.
@Theory
public void testWithAckResponseButWrongReceiveToken(final boolean sslEnabled) throws Throwable {
Exception exception = new ConfigurableTestServer(sslEnabled).run(new ConfigurableTestServer.WithClientSocket() {
@Override
public void run(Socket clientSocket) throws Exception {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(clientSocket.getInputStream());
assertEquals(3, unpacker.unpackArrayHeader());
assertEquals("foo.bar", unpacker.unpackString());
ImmutableRawValue rawValue = unpacker.unpackValue().asRawValue();
Map<Value, Value> map = unpacker.unpackValue().asMapValue().map();
assertEquals(2, map.size());
assertEquals(rawValue.asByteArray().length, map.get(KEY_OPTION_SIZE).asIntegerValue().asInt());
assertNotNull(map.get(KEY_OPTION_CHUNK).asRawValue().asString());
MessagePacker packer = MessagePack.newDefaultPacker(clientSocket.getOutputStream());
packer.packMapHeader(1).packString("ack").packString(UUID.randomUUID().toString()).close();
// Close the input stream after closing the output stream to avoid closing a socket too early
unpacker.close();
}
}, new ConfigurableTestServer.WithServerPort() {
@Override
public void run(int serverPort) throws Exception {
Fluency fluency = Fluency.defaultFluency(serverPort, new Fluency.Config().setSslEnabled(sslEnabled).setAckResponseMode(true));
fluency.emit("foo.bar", new HashMap<String, Object>());
fluency.close();
}
}, 5000);
assertEquals(exception.getClass(), TimeoutException.class);
}
Aggregations