use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class MessageStreamsTest method testResource.
@Test
public void testResource() {
try {
MessageStreams.resource("/no_such_resource.json", new JsonSerializer(), CompactFields.kDescriptor);
fail("no exception");
} catch (IOException e) {
assertThat(e.getMessage(), is("No such resource /no_such_resource.json"));
}
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class IOMessageRWTest method testReadable.
@Test
public void testReadable() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (MessageWriter writer = new IOMessageWriter(baos, new JsonSerializer())) {
writer.write(m1);
writer.separator();
writer.write(m2);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try (MessageReader reader = new IOMessageReader(bais, new JsonSerializer())) {
assertThat(m1, is(equalTo(reader.read(CompactFields.kDescriptor))));
assertThat(m2, is(equalTo(reader.read(OptionalFields.kDescriptor))));
}
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class IOMessageRWTest method testService.
@Test
public void testService() throws IOException {
String content = "[\"calculate\",\"call\",44,{\"op\":{\"operator\":\"ADD\",\"operands\":[]}}]";
ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes(UTF_8));
PServiceCall<Operation, Operation._Field> call = null;
try (MessageReader reader = new IOMessageReader(in, new JsonSerializer())) {
call = reader.read(Calculator.kDescriptor);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (MessageWriter writer = new IOMessageWriter(out, new JsonSerializer().named())) {
writer.write(call);
}
assertThat(new String(out.toByteArray(), UTF_8), is(equalTo(content)));
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class QueuedFileMessageWriterTest method testClose_withQueue.
@Test
public void testClose_withQueue() throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOMessageWriter target = new IOMessageWriter(baos, new JsonSerializer().named());
ExecutorService executor = mock(ExecutorService.class);
when(executor.isShutdown()).thenReturn(false);
when(executor.awaitTermination(1000L, TimeUnit.MILLISECONDS)).thenReturn(true);
QueuedMessageWriter writer = new QueuedMessageWriter(target, executor);
writer.write(CompactFields.builder().setId(1).setName("Name").build());
writer.write(new PServiceCall<>("name", PServiceCallType.REPLY, 42, CompactFields.builder().setId(1).setName("Name").build()));
writer.close();
verify(executor).submit(any(Runnable.class));
verify(executor).isShutdown();
verify(executor).shutdown();
verify(executor).awaitTermination(1000L, TimeUnit.MILLISECONDS);
verifyNoMoreInteractions(executor);
assertThat(new String(baos.toByteArray()), is("[\"Name\",1]\n" + "[\"name\",\"reply\",42,[\"Name\",1]]\n" + ""));
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class RollingFileMessageWriterTest method testSizeBasedRolling.
@Test
public void testSizeBasedRolling() throws IOException {
CompactFields cf = new CompactFields(" * the message writer MUST be assigned a rolling policy", 1234567890, "Also note that");
RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
writer.write(cf);
for (int i = 0; i < 100; ++i) {
writer.write(cf);
}
String[] files = tmp.getRoot().list();
assertThat(files, is(notNullValue()));
assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-005.txt", "my-log-006.txt", "my-log-007.txt", "my-log-008.txt", "my-log-009.txt")));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-009.txt"));
writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
writer.write(cf);
for (int i = 0; i < 30; ++i) {
writer.write(cf);
}
files = tmp.getRoot().list();
assertThat(files, is(notNullValue()));
assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-001.txt", "my-log-002.txt", "my-log-003.txt", "my-log-008.txt", "my-log-009.txt")));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-003.txt"));
writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
writer.write(cf);
for (int i = 0; i < 30; ++i) {
writer.write(cf);
}
files = tmp.getRoot().list();
assertThat(files, is(notNullValue()));
assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-002.txt", "my-log-003.txt", "my-log-004.txt", "my-log-005.txt", "my-log-006.txt")));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-006.txt"));
}
Aggregations