Search in sources :

Example 6 with Subscribe

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

the class LAONetworkManagerTest method subscribeSendsTheRightMessages.

@Test
public void subscribeSendsTheRightMessages() {
    TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
    TestScheduler testScheduler = schedulerProvider.getTestScheduler();
    LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
    Answer<?> answer = args -> {
        // Retrieve subscribe object
        Subscribe subscribe = args.getArgument(0);
        // Make sure the channel is correct
        assertEquals(CHANNEL, subscribe.getChannel());
        // Return a positive result
        messages.onNext(new Result(subscribe.getRequestId()));
        return null;
    };
    doAnswer(answer).when(connection).sendMessage(any(Subscribe.class));
    // Actual test
    Disposable disposable = networkManager.subscribe(CHANNEL).subscribe(() -> verify(connection, never()).sendMessage(any(Catchup.class)));
    testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
    disposable.dispose();
    networkManager.dispose();
    verify(connection).sendMessage(any(Subscribe.class));
    verify(connection).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) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) TestScheduler(io.reactivex.schedulers.TestScheduler) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) Result(com.github.dedis.popstellar.model.network.answer.Result) Test(org.junit.Test)

Example 7 with Subscribe

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

the class LAONetworkManager method subscribe.

@Override
public Completable subscribe(Channel channel) {
    Log.d(TAG, "sending a subscribe on the channel " + channel);
    Subscribe subscribe = new Subscribe(channel, requestCounter.incrementAndGet());
    return request(subscribe).doOnSuccess(answer -> subscribedChannels.add(channel)).doAfterSuccess(answer -> catchup(channel).subscribe()).ignoreElement();
}
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) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe)

Example 8 with Subscribe

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

the class ConnectionTest method sendMessageDelegatesToService.

@Test
public void sendMessageDelegatesToService() {
    LAOService service = mock(LAOService.class);
    BehaviorSubject<GenericMessage> messages = BehaviorSubject.create();
    when(service.observeMessage()).thenReturn(messages);
    BehaviorSubject<Lifecycle.State> manualState = BehaviorSubject.create();
    Connection connection = new Connection(service, manualState);
    Message msg = new Subscribe(Channel.ROOT, 12);
    connection.sendMessage(msg);
    verify(service).sendMessage(msg);
    verify(service).observeMessage();
    verifyNoMoreInteractions(service);
}
Also used : GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) Message(com.github.dedis.popstellar.model.network.method.Message) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) Test(org.junit.Test)

Aggregations

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