use of com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider in project popstellar by dedis.
the class GlobalNetworkManagerTest method connectingToANewConnectionClosesTheLast.
@Test
public void connectingToANewConnectionClosesTheLast() {
ConnectionFactory factory = mock(ConnectionFactory.class);
Connection firstConnection = mock(Connection.class);
when(firstConnection.observeMessage()).thenReturn(BehaviorSubject.create());
when(firstConnection.observeConnectionEvents()).thenReturn(BehaviorSubject.create());
when(factory.createConnection(anyString())).thenReturn(firstConnection);
GlobalNetworkManager networkManager = new GlobalNetworkManager(repository, handler, factory, gson, new TestSchedulerProvider());
verify(factory).createConnection(anyString());
Connection secondConnection = mock(Connection.class);
when(secondConnection.observeMessage()).thenReturn(BehaviorSubject.create());
when(secondConnection.observeConnectionEvents()).thenReturn(BehaviorSubject.create());
when(factory.createConnection(anyString())).thenReturn(secondConnection);
networkManager.connect("new url");
verify(firstConnection).close();
}
use of com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider in project popstellar by dedis.
the class GlobalNetworkManagerTest method initializationProducesAValidConnection.
@Test
public void initializationProducesAValidConnection() {
ConnectionFactory factory = mock(ConnectionFactory.class);
Connection firstConnection = mock(Connection.class);
when(firstConnection.observeMessage()).thenReturn(BehaviorSubject.create());
when(firstConnection.observeConnectionEvents()).thenReturn(BehaviorSubject.create());
when(factory.createConnection(anyString())).thenReturn(firstConnection);
GlobalNetworkManager networkManager = new GlobalNetworkManager(repository, handler, factory, gson, new TestSchedulerProvider());
verify(factory).createConnection(anyString());
networkManager.getMessageSender().unsubscribe(Channel.ROOT);
verify(firstConnection).sendMessage(any());
}
use of com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider in project popstellar by dedis.
the class LAONetworkManagerTest method unsubscribeSendsTheRightMessage.
@Test
public void unsubscribeSendsTheRightMessage() {
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
Unsubscribe 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(Unsubscribe.class));
// Actual test
Disposable disposable = networkManager.unsubscribe(CHANNEL).subscribe();
testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
disposable.dispose();
networkManager.dispose();
verify(connection).sendMessage(any(Unsubscribe.class));
verify(connection, atLeastOnce()).observeMessage();
verify(connection).observeConnectionEvents();
verify(connection).close();
verifyNoMoreInteractions(connection);
}
use of com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider 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.utility.scheduler.TestSchedulerProvider in project popstellar by dedis.
the class LAONetworkManagerTest method publishSendsRightMessage.
@Test
public void publishSendsRightMessage() {
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
Publish publish = args.getArgument(0);
// Make sure the channel is correct
assertEquals(CHANNEL, publish.getChannel());
MessageGeneral messageGeneral = publish.getMessage();
assertEquals(DATA, messageGeneral.getData());
// Return a positive result
messages.onNext(new Result(publish.getRequestId()));
return null;
};
doAnswer(answer).when(connection).sendMessage(any(Publish.class));
// Actual test
Disposable disposable = networkManager.publish(KEY_PAIR, CHANNEL, DATA).subscribe();
testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
disposable.dispose();
networkManager.dispose();
verify(connection).sendMessage(any(Publish.class));
verify(connection, atLeastOnce()).observeMessage();
verify(connection).observeConnectionEvents();
verify(connection).close();
verifyNoMoreInteractions(connection);
}
Aggregations