use of com.github.dedis.popstellar.model.network.answer.ResultMessages in project popstellar by dedis.
the class LAONetworkManagerTest method multipleRequestsAtATimeShouldAllSucceed.
@Test
public void multipleRequestsAtATimeShouldAllSucceed() {
TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
TestScheduler testScheduler = schedulerProvider.getTestScheduler();
LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
// Set a response that stores requested ids
Set<Integer> requests = new HashSet<>();
Set<Integer> catchups = new HashSet<>();
// Setup mock answer
Answer<?> answer = args -> {
// Retrieve subscribe object
Query query = args.getArgument(0);
if (query instanceof Catchup)
catchups.add(query.getRequestId());
else
requests.add(query.getRequestId());
return null;
};
doAnswer(answer).when(connection).sendMessage(any());
// Actual test
// Create
AtomicBoolean sub1Called = new AtomicBoolean(false);
AtomicBoolean sub2Called = new AtomicBoolean(false);
Disposable disposable1 = networkManager.subscribe(CHANNEL).subscribe(() -> sub1Called.set(true));
Disposable disposable2 = networkManager.subscribe(CHANNEL).subscribe(() -> sub2Called.set(true));
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
// Responses for subscribes
requests.forEach(id -> messages.onNext(new Result(id)));
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
// Expect catchups to be sent now
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
// Responses to catchups
catchups.forEach(id -> messages.onNext(new ResultMessages(id, Collections.emptyList())));
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
// Make sure the subscription succeed
assertTrue(sub1Called.get());
assertTrue(sub2Called.get());
disposable1.dispose();
disposable2.dispose();
networkManager.dispose();
verify(connection, times(2)).sendMessage(any(Subscribe.class));
verify(connection, times(2)).sendMessage(any(Catchup.class));
verify(connection, atLeastOnce()).observeMessage();
verify(connection).observeConnectionEvents();
verify(connection).close();
verifyNoMoreInteractions(connection);
}
use of com.github.dedis.popstellar.model.network.answer.ResultMessages in project popstellar by dedis.
the class JsonResultSerializer method deserialize.
@Override
public Result deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject root = json.getAsJsonObject();
int id = root.get("id").getAsInt();
JsonElement resultElement = root.get(RESULT);
if (resultElement.isJsonPrimitive()) {
return new Result(id);
} else {
Type listType = new TypeToken<ArrayList<MessageGeneral>>() {
}.getType();
List<MessageGeneral> messages = context.deserialize(resultElement.getAsJsonArray(), listType);
return new ResultMessages(id, messages);
}
}
use of com.github.dedis.popstellar.model.network.answer.ResultMessages in project popstellar by dedis.
the class JsonResultSerializer method serialize.
@Override
public JsonElement serialize(Result src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject output = new JsonObject();
output.addProperty(ID, src.getId());
if (src instanceof ResultMessages) {
ResultMessages resultMessages = (ResultMessages) src;
JsonElement messages = context.serialize(resultMessages.getMessages());
output.add(RESULT, messages);
} else {
output.addProperty(RESULT, 0);
}
return output;
}
use of com.github.dedis.popstellar.model.network.answer.ResultMessages in project popstellar by dedis.
the class LAONetworkManager method catchup.
@Override
public Completable catchup(Channel channel) {
Log.d(TAG, "sending a catchup to the channel " + channel);
Catchup catchup = new Catchup(channel, requestCounter.incrementAndGet());
return request(catchup).map(ResultMessages.class::cast).map(ResultMessages::getMessages).doOnSuccess(messages -> Log.d(TAG, "Catchup on " + channel + " retrieved : " + messages)).doOnSuccess(messages -> handleMessages(messages, channel)).ignoreElement();
}
Aggregations