public ConnectionFactory<String, String> newConnectionFactoryMock() { @SuppressWarnings("unchecked") final ConnectionFactory<String, String> cfMock = Mockito.mock(ConnectionFactory.class); List<Instance<SocketAddress>> instances = hosts.toList().toBlocking().single(); for (Instance<SocketAddress> instance : instances) { EmbeddedChannel channel = new EmbeddedChannel(); final TcpClientEventPublisher eventPublisher = new TcpClientEventPublisher(); final Connection<String, String> mockConnection = ConnectionImpl.create(channel, eventPublisher, eventPublisher); Mockito.when(cfMock.newConnection(instance.getValue())) .thenReturn( ConnectionObservable.createNew( new OnSubcribeFunc<String, String>() { @Override public Subscription subscribeForEvents(TcpClientEventListener eventListener) { return eventPublisher.subscribe(eventListener); } @Override public void call(Subscriber<? super Connection<String, String>> subscriber) { subscriber.onNext(mockConnection); subscriber.onCompleted(); } })); } return cfMock; }
public Connection<String, String> connect( ConnectionObservable<String, String> connectionObservable) { TestSubscriber<Connection<String, String>> testSub = new TestSubscriber<>(); connectionObservable.subscribe(testSub); testSub.awaitTerminalEvent(); testSub.assertNoErrors(); testSub.assertValueCount(1); return testSub.getOnNextEvents().get(0); }