use of com.github.dedis.popstellar.model.network.answer.Error in project popstellar by dedis.
the class LAONetworkManagerTest method errorsAreDispatchedCorrectly.
@Test
public void errorsAreDispatchedCorrectly() {
TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
TestScheduler testScheduler = schedulerProvider.getTestScheduler();
LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
ErrorCode error = new ErrorCode(3, "error");
// Setup mock answer
Answer<?> answer = args -> {
// Retrieve subscribe object
Query query = args.getArgument(0);
// Return a negative result
messages.onNext(new Error(query.getRequestId(), error));
return null;
};
doAnswer(answer).when(connection).sendMessage(any());
Disposable disposable = networkManager.subscribe(CHANNEL).subscribe(() -> {
throw new IllegalAccessException("The subscription should have failed.");
}, err -> assertTrue(err instanceof JsonRPCErrorException));
testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
disposable.dispose();
networkManager.dispose();
verify(connection).sendMessage(any(Subscribe.class));
verify(connection, atLeastOnce()).observeMessage();
verify(connection).observeConnectionEvents();
verify(connection).close();
verifyNoMoreInteractions(connection);
}
use of com.github.dedis.popstellar.model.network.answer.Error in project popstellar by dedis.
the class JsonAnswerSerializer method deserialize.
@Override
public Answer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject();
JsonUtils.testRPCVersion(obj);
JsonUtils.verifyJson(JsonUtils.ROOT_SCHEMA, json.toString());
if (obj.has(RESULT)) {
return context.deserialize(json, Result.class);
} else if (obj.has(ERROR)) {
return context.deserialize(json, Error.class);
} else {
throw new JsonParseException("A result must contain one of the field result or error");
}
}
use of com.github.dedis.popstellar.model.network.answer.Error in project popstellar by dedis.
the class LAONetworkManager method request.
private Single<Answer> request(Query query) {
Single<Answer> answerSingle = connection.observeMessage().filter(// Filter the Answers
Answer.class::isInstance).map(Answer.class::cast).filter(answer -> answer.getId() == query.getRequestId()).doOnNext(answer -> Log.d(TAG, "request id: " + answer.getId())).firstOrError().flatMap(answer -> {
if (answer instanceof Error) {
return Single.error(new JsonRPCErrorException((Error) answer));
} else {
return Single.just(answer);
}
}).subscribeOn(schedulerProvider.io()).observeOn(schedulerProvider.mainThread()).timeout(5, TimeUnit.SECONDS).cache();
// Only send the message after the single is created to make sure it is already waiting
// before the answer is received
connection.sendMessage(query);
return answerSingle;
}
Aggregations