@Test(groups = "unit")
public void startBackgroundAddressRefresh() throws Exception {

    initializeConsistencyWriter(false);
    CyclicBarrier b = new CyclicBarrier(2);
    PublishSubject<Uri> subject = PublishSubject.create();
    CountDownLatch c = new CountDownLatch(1);
    Uri uri = Uri.create("https://localhost:5050");
    List<InvocationOnMock> invocationOnMocks = Collections.synchronizedList(new ArrayList<>());
    Mockito.doAnswer(new Answer() {
        @Override
        public Single<Uri> answer(InvocationOnMock invocationOnMock)  {
            invocationOnMocks.add(invocationOnMock);
            return subject.toSingle().doOnSuccess(x -> c.countDown()).doAfterTerminate(() -> {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            b.await();
                        } catch (Exception e) {
                        }
                    }
                }.start();
            });
        }
    }).when(addressSelector).resolvePrimaryUriAsync(Mockito.any(RxDocumentServiceRequest.class), Mockito.anyBoolean());
    RxDocumentServiceRequest request = Mockito.mock(RxDocumentServiceRequest.class);
    consistencyWriter.startBackgroundAddressRefresh(request);
    subject.onNext(uri);
    subject.onCompleted();
    TimeUnit.MILLISECONDS.sleep(1000);
    assertThat(c.getCount()).isEqualTo(0);
    assertThat(b.getNumberWaiting()).isEqualTo(1);
    b.await(1000, TimeUnit.MILLISECONDS);
    assertThat(invocationOnMocks).hasSize(1);
    assertThat(invocationOnMocks.get(0).getArgumentAt(1, Boolean.class)).isTrue();
}