use of net.morimekta.providence.util_internal.MessageGenerator in project providence by morimekta.
the class SerializerTest method setUpData.
@BeforeClass
public static void setUpData() throws IOException, ExceptionFields {
MessageGenerator gen = new MessageGenerator().addFactory(f -> {
if (f.equals(Operand._Field.OPERATION)) {
return () -> Operation.builder().setOperator(Operator.ADD).addToOperands(Operand.withNumber(123)).addToOperands(Operand.withNumber(321)).build();
}
return null;
});
if (operation == null) {
operation = gen.generate(Operation.kDescriptor);
}
if (containers == null) {
containers = new ArrayList<>();
for (int i = 0; i < 1; ++i) {
containers.add(gen.generate(Containers.kDescriptor));
}
}
serviceCalls = new ArrayList<>();
/**
* Temporary setup needed to generate
*/
ContainerService.Iface impl = pC -> {
if (pC == null) {
throw new PApplicationException("", PApplicationExceptionType.INTERNAL_ERROR);
}
if (pC.mutate().presentFields().isEmpty()) {
throw gen.generate(ExceptionFields.kDescriptor);
}
return CompactFields.builder().setName("" + pC.hashCode()).setId(pC.hashCode()).build();
};
PProcessor processor = new ContainerService.Processor(impl);
PServiceCallHandler handler = new PServiceCallHandler() {
@Nullable
@Override
@SuppressWarnings("unchecked")
public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException {
serviceCalls.add(call);
try {
PServiceCall response = processor.handleCall(call, service);
serviceCalls.add(response);
return response;
} catch (PApplicationException e) {
PServiceCall ex = new PServiceCall(call.getMethod(), PServiceCallType.EXCEPTION, call.getSequence(), e);
serviceCalls.add(ex);
return ex;
}
}
};
ContainerService.Client client = new ContainerService.Client(handler);
client.load(gen.generate(Containers.kDescriptor));
try {
client.load(Containers.builder().build());
} catch (ExceptionFields e) {
// ignore.
}
try {
// NPE -> PApplicationException
client.load(null);
} catch (PApplicationException e) {
// ignore.
}
}
use of net.morimekta.providence.util_internal.MessageGenerator in project providence by morimekta.
the class QueuedFileMessageWriterTest method testAFewWrites_serviceCalls.
@Test
@SuppressWarnings("unchecked")
public void testAFewWrites_serviceCalls() throws IOException, InterruptedException {
setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
QueuedMessageWriter writer = new QueuedMessageWriter(target);
ExecutorService executorService = Executors.newFixedThreadPool(11);
for (int i = 0; i < 10; ++i) {
executorService.submit(() -> {
MessageGenerator generator = new MessageGenerator();
for (int j = 0; j < 10; ++j) {
try {
if (j > 0)
sleep(1L);
PServiceCall tmp = new PServiceCall("test", PServiceCallType.CALL, j, generator.generate(CompactFields.kDescriptor));
writer.write(tmp);
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
sleep(10L);
executorService.shutdown();
assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
sleep(1L);
writer.close();
}
use of net.morimekta.providence.util_internal.MessageGenerator in project providence by morimekta.
the class QueuedFileMessageWriterTest method testAFewWrites.
@Test
@SuppressWarnings("unchecked")
public void testAFewWrites() throws IOException, InterruptedException {
setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
QueuedMessageWriter writer = new QueuedMessageWriter(target);
ExecutorService executorService = Executors.newFixedThreadPool(11);
for (int i = 0; i < 10; ++i) {
executorService.submit(() -> {
MessageGenerator generator = new MessageGenerator();
for (int j = 0; j < 10; ++j) {
try {
if (j > 0)
sleep(1L);
writer.write(generator.generate(CompactFields.kDescriptor));
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
});
}
sleep(10L);
executorService.shutdown();
assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
sleep(1L);
writer.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
List<CompactFields> result = MessageStreams.stream(bais, new BinarySerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
assertThat(result, hasSize(100));
}
Aggregations