Search in sources :

Example 1 with Query

use of com.github.dedis.popstellar.model.network.method.Query 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);
}
Also used : TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Publish(com.github.dedis.popstellar.model.network.method.Publish) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) BehaviorSubject(io.reactivex.subjects.BehaviorSubject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataRegistryModule(com.github.dedis.popstellar.di.DataRegistryModule) HashSet(java.util.HashSet) WebSocket(com.tinder.scarlet.WebSocket) Answer(org.mockito.stubbing.Answer) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Error(com.github.dedis.popstellar.model.network.answer.Error) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ErrorCode(com.github.dedis.popstellar.model.network.answer.ErrorCode) JsonModule(com.github.dedis.popstellar.di.JsonModule) Before(org.junit.Before) Channel(com.github.dedis.popstellar.model.objects.Channel) Result(com.github.dedis.popstellar.model.network.answer.Result) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Base64DataUtils(com.github.dedis.popstellar.testutils.Base64DataUtils) Query(com.github.dedis.popstellar.model.network.method.Query) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Disposable(io.reactivex.disposables.Disposable) TestScheduler(io.reactivex.schedulers.TestScheduler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Disposable(io.reactivex.disposables.Disposable) Query(com.github.dedis.popstellar.model.network.method.Query) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) Result(com.github.dedis.popstellar.model.network.answer.Result) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) TestScheduler(io.reactivex.schedulers.TestScheduler) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Query

use of com.github.dedis.popstellar.model.network.method.Query 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);
}
Also used : TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Publish(com.github.dedis.popstellar.model.network.method.Publish) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) BehaviorSubject(io.reactivex.subjects.BehaviorSubject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataRegistryModule(com.github.dedis.popstellar.di.DataRegistryModule) HashSet(java.util.HashSet) WebSocket(com.tinder.scarlet.WebSocket) Answer(org.mockito.stubbing.Answer) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Error(com.github.dedis.popstellar.model.network.answer.Error) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ErrorCode(com.github.dedis.popstellar.model.network.answer.ErrorCode) JsonModule(com.github.dedis.popstellar.di.JsonModule) Before(org.junit.Before) Channel(com.github.dedis.popstellar.model.objects.Channel) Result(com.github.dedis.popstellar.model.network.answer.Result) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Base64DataUtils(com.github.dedis.popstellar.testutils.Base64DataUtils) Query(com.github.dedis.popstellar.model.network.method.Query) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Disposable(io.reactivex.disposables.Disposable) TestScheduler(io.reactivex.schedulers.TestScheduler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Disposable(io.reactivex.disposables.Disposable) Query(com.github.dedis.popstellar.model.network.method.Query) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) Error(com.github.dedis.popstellar.model.network.answer.Error) ErrorCode(com.github.dedis.popstellar.model.network.answer.ErrorCode) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 3 with Query

use of com.github.dedis.popstellar.model.network.method.Query in project popstellar by dedis.

the class JsonMessageSerializer method serialize.

@Override
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject params = context.serialize(src).getAsJsonObject();
    JsonObject obj = context.serialize(new JSONRPCRequest(JsonUtils.JSON_RPC_VERSION, src.getMethod(), params)).getAsJsonObject();
    if (src instanceof Query) {
        obj.addProperty(JsonUtils.JSON_REQUEST_ID, ((Query) src).getRequestId());
    }
    return obj;
}
Also used : Query(com.github.dedis.popstellar.model.network.method.Query) JsonObject(com.google.gson.JsonObject)

Example 4 with Query

use of com.github.dedis.popstellar.model.network.method.Query 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;
}
Also used : Publish(com.github.dedis.popstellar.model.network.method.Publish) Answer(com.github.dedis.popstellar.model.network.answer.Answer) Completable(io.reactivex.Completable) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) Single(io.reactivex.Single) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) WebSocket(com.tinder.scarlet.WebSocket) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) Error(com.github.dedis.popstellar.model.network.answer.Error) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Gson(com.google.gson.Gson) Broadcast(com.github.dedis.popstellar.model.network.method.Broadcast) Observable(io.reactivex.Observable) LinkedList(java.util.LinkedList) Log(android.util.Log) Subject(io.reactivex.subjects.Subject) Channel(com.github.dedis.popstellar.model.objects.Channel) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Query(com.github.dedis.popstellar.model.network.method.Query) SchedulerProvider(com.github.dedis.popstellar.utility.scheduler.SchedulerProvider) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) PublishSubject(io.reactivex.subjects.PublishSubject) Answer(com.github.dedis.popstellar.model.network.answer.Answer) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) Error(com.github.dedis.popstellar.model.network.answer.Error)

Aggregations

Query (com.github.dedis.popstellar.model.network.method.Query)4 GenericMessage (com.github.dedis.popstellar.model.network.GenericMessage)3 Error (com.github.dedis.popstellar.model.network.answer.Error)3 ResultMessages (com.github.dedis.popstellar.model.network.answer.ResultMessages)3 Catchup (com.github.dedis.popstellar.model.network.method.Catchup)3 Publish (com.github.dedis.popstellar.model.network.method.Publish)3 Subscribe (com.github.dedis.popstellar.model.network.method.Subscribe)3 Unsubscribe (com.github.dedis.popstellar.model.network.method.Unsubscribe)3 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)3 Data (com.github.dedis.popstellar.model.network.method.message.data.Data)3 Channel (com.github.dedis.popstellar.model.objects.Channel)3 KeyPair (com.github.dedis.popstellar.model.objects.security.KeyPair)3 LAORepository (com.github.dedis.popstellar.repository.LAORepository)3 JsonRPCErrorException (com.github.dedis.popstellar.utility.error.JsonRPCErrorException)3 MessageHandler (com.github.dedis.popstellar.utility.handler.MessageHandler)3 WebSocket (com.tinder.scarlet.WebSocket)3 TimeUnit (java.util.concurrent.TimeUnit)3 DataRegistryModule (com.github.dedis.popstellar.di.DataRegistryModule)2 JsonModule (com.github.dedis.popstellar.di.JsonModule)2 ErrorCode (com.github.dedis.popstellar.model.network.answer.ErrorCode)2