### Question:
SyncState { public boolean isInSync() { return isInSync(Synchronizer.DEFAULT_IN_SYNC_TOLERANCE); } SyncState(final Blockchain blockchain, final EthPeers ethPeers); long subscribeInSync(final InSyncListener listener); long subscribeInSync(final InSyncListener listener, final long syncTolerance); boolean unsubscribeInSync(final long subscriberId); long subscribeSyncStatus(final SyncStatusListener listener); boolean unsubscribeSyncStatus(final long listenerId); Optional<SyncStatus> syncStatus(); Optional<SyncTarget> syncTarget(); void setSyncTarget(final EthPeer peer, final BlockHeader commonAncestor); void setWorldStateDownloadStatus(final WorldStateDownloadStatus worldStateDownloadStatus); boolean isInSync(); boolean isInSync(final long syncTolerance); void disconnectSyncTarget(final DisconnectReason reason); void clearSyncTarget(); long bestChainHeight(); long bestChainHeight(final long localChainHeight);  }### Answer:
@Test public void isInSync_outOfSyncWithTargetAndOutOfSyncWithBestPeer() { setupOutOfSyncState(); updateChainState(otherPeer.getEthPeer(), TARGET_CHAIN_HEIGHT, TARGET_DIFFICULTY); doReturn(Optional.of(otherPeer.getEthPeer())).when(ethPeers).bestPeerWithHeightEstimate(); assertThat(syncState.isInSync()).isFalse(); assertThat(syncState.isInSync(0)).isFalse(); assertThat(syncState.isInSync(TARGET_CHAIN_DELTA - 1)).isFalse(); assertThat(syncState.isInSync(TARGET_CHAIN_DELTA)).isTrue(); assertThat(syncState.isInSync(TARGET_CHAIN_DELTA + 1)).isTrue(); }
@Test public void isInSync_inSyncWithTargetOutOfSyncWithBestPeer() { setupOutOfSyncState(); advanceLocalChain(TARGET_CHAIN_HEIGHT); final long heightDifference = 20L; updateChainState( otherPeer.getEthPeer(), TARGET_CHAIN_HEIGHT + heightDifference, TARGET_DIFFICULTY.add(heightDifference)); doReturn(Optional.of(otherPeer.getEthPeer())).when(ethPeers).bestPeerWithHeightEstimate(); assertThat(syncState.isInSync()).isFalse(); assertThat(syncState.isInSync(0)).isFalse(); assertThat(syncState.isInSync(heightDifference - 1)).isFalse(); assertThat(syncState.isInSync(heightDifference)).isTrue(); assertThat(syncState.isInSync(heightDifference + 1)).isTrue(); }
@Test public void isInSync_inSyncWithTargetInSyncWithBestPeer() { setupOutOfSyncState(); advanceLocalChain(TARGET_CHAIN_HEIGHT); updateChainState(otherPeer.getEthPeer(), TARGET_CHAIN_HEIGHT, TARGET_DIFFICULTY); doReturn(Optional.of(otherPeer.getEthPeer())).when(ethPeers).bestPeerWithHeightEstimate(); assertThat(syncState.isInSync()).isTrue(); assertThat(syncState.isInSync(0)).isTrue(); }
@Test public void shouldBecomeInSyncWhenOurBlockchainCatchesUp() { setupOutOfSyncState(); advanceLocalChain(TARGET_CHAIN_HEIGHT - Synchronizer.DEFAULT_IN_SYNC_TOLERANCE); assertThat(syncState.isInSync()).isTrue(); assertThat(syncState.isInSync(0)).isFalse(); verify(inSyncListener).onInSyncStatusChange(true); verify(inSyncListenerExact, never()).onInSyncStatusChange(true); advanceLocalChain(TARGET_CHAIN_HEIGHT - Synchronizer.DEFAULT_IN_SYNC_TOLERANCE + 1); assertThat(syncState.isInSync()).isTrue(); assertThat(syncState.isInSync(0)).isFalse(); verifyNoMoreInteractions(inSyncListener); verify(inSyncListenerExact, never()).onInSyncStatusChange(true); advanceLocalChain(TARGET_CHAIN_HEIGHT); assertThat(syncState.isInSync()).isTrue(); assertThat(syncState.isInSync(0)).isTrue(); verifyNoMoreInteractions(inSyncListener); verify(inSyncListenerExact).onInSyncStatusChange(true); }