@ -14,98 +14,50 @@ package tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.ArgumentMatchers.refEq ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.when ;
import static org.mockito.Mockito.when ;
import tech.pegasys.pantheon.ethereum.core.SyncStatus ;
import tech.pegasys.pantheon.ethereum.core.SyncStatus ;
import tech.pegasys.pantheon.ethereum.core.Synchronizer ;
import tech.pegasys.pantheon.ethereum.core.Synchronizer ;
import tech.pegasys.pantheon.ethereum.core.Synchronizer.SyncStatusListener ;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.SyncingResult ;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.SyncingResult ;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager ;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager ;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType ;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType ;
import java.util.Optional ;
import com.google.common.collect.Lists ;
import com.google.common.collect.Lists ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.junit.runner.RunWith ;
import org.mockito.ArgumentCaptor ;
import org.mockito.Mock ;
import org.mockito.Mock ;
import org.mockito.junit.MockitoJUnitRunner ;
import org.mockito.junit.MockitoJUnitRunner ;
@RunWith ( MockitoJUnitRunner . class )
@RunWith ( MockitoJUnitRunner . class )
public class SyncingSubscriptionServiceTest {
public class SyncingSubscriptionServiceTest {
private SyncingSubscriptionService syncingSubscriptionService ;
@Mock private SubscriptionManager subscriptionManager ;
@Mock private SubscriptionManager subscriptionManager ;
@Mock private Synchronizer synchronizer ;
@Mock private Synchronizer synchronizer ;
private SyncStatusListener syncStatusListener ;
@Before
@Before
public void before ( ) {
public void before ( ) {
syncingSubscriptionService = new SyncingSubscriptionService ( subscriptionManager , synchronizer ) ;
final ArgumentCaptor < SyncStatusListener > captor =
}
ArgumentCaptor . forClass ( SyncStatusListener . class ) ;
when ( synchronizer . observeSyncStatus ( captor . capture ( ) ) ) . thenReturn ( 1L ) ;
@Test
new SyncingSubscriptionService ( subscriptionManager , synchronizer ) ;
public void shouldSendSyncStatusWhenSyncing ( ) {
syncStatusListener = captor . getValue ( ) ;
final SyncingSubscription subscription = new SyncingSubscription ( 9L , SubscriptionType . SYNCING ) ;
when ( subscriptionManager . subscriptionsOfType ( any ( ) , any ( ) ) )
. thenReturn ( Lists . newArrayList ( subscription ) ) ;
final SyncStatus syncStatus = new SyncStatus ( 0L , 1L , 1L ) ;
final SyncingResult expectedSyncingResult = new SyncingResult ( syncStatus ) ;
when ( synchronizer . getSyncStatus ( ) ) . thenReturn ( Optional . of ( syncStatus ) ) ;
syncingSubscriptionService . sendSyncingToMatchingSubscriptions ( ) ;
verify ( subscriptionManager ) . sendMessage ( eq ( subscription . getId ( ) ) , refEq ( expectedSyncingResult ) ) ;
}
@Test
public void shouldSendFalseWhenNotSyncing ( ) {
final SyncingSubscription subscription = new SyncingSubscription ( 9L , SubscriptionType . SYNCING ) ;
when ( subscriptionManager . subscriptionsOfType ( any ( ) , any ( ) ) )
. thenReturn ( Lists . newArrayList ( subscription ) ) ;
when ( synchronizer . getSyncStatus ( ) ) . thenReturn ( Optional . empty ( ) ) ;
syncingSubscriptionService . sendSyncingToMatchingSubscriptions ( ) ;
verify ( subscriptionManager )
. sendMessage ( eq ( subscription . getId ( ) ) , refEq ( new NotSynchronisingResult ( ) ) ) ;
}
}
@Test
@Test
public void shouldSendNoMore SyncStatusWhenSyncing StatusHasNotChanged ( ) {
public void shouldSendSyncStatusWhenReceiveSyncStatus ( ) {
final SyncingSubscription subscription = new SyncingSubscription ( 9L , SubscriptionType . SYNCING ) ;
final SyncingSubscription subscription = new SyncingSubscription ( 9L , SubscriptionType . SYNCING ) ;
when ( subscriptionManager . subscriptionsOfType ( any ( ) , any ( ) ) )
when ( subscriptionManager . subscriptionsOfType ( any ( ) , any ( ) ) )
. thenReturn ( Lists . newArrayList ( subscription ) ) ;
. thenReturn ( Lists . newArrayList ( subscription ) ) ;
final SyncStatus syncStatus = new SyncStatus ( 0L , 1L , 1L ) ;
final SyncStatus syncStatus = new SyncStatus ( 0L , 1L , 1L ) ;
final SyncingResult expectedSyncingResult = new SyncingResult ( syncStatus ) ;
final SyncingResult expectedSyncingResult = new SyncingResult ( syncStatus ) ;
when ( synchronizer . getSyncStatus ( ) ) . thenReturn ( Optional . of ( syncStatus ) ) ;
syncingSubscriptionService . sendSyncingToMatchingSubscriptions ( ) ;
verify ( subscriptionManager ) . sendMessage ( eq ( subscription . getId ( ) ) , refEq ( expectedSyncingResult ) ) ;
syncingSubscriptionService . sendSyncingToMatchingSubscriptions ( ) ;
}
@Test
public void shouldSendDifferentSyncStatusWhenSyncingStatusHasChanged ( ) {
final SyncingSubscription subscription = new SyncingSubscription ( 9L , SubscriptionType . SYNCING ) ;
when ( subscriptionManager . subscriptionsOfType ( any ( ) , any ( ) ) )
. thenReturn ( Lists . newArrayList ( subscription ) ) ;
final SyncStatus syncStatus1 = new SyncStatus ( 0L , 1L , 9L ) ;
final SyncStatus syncStatus2 = new SyncStatus ( 0L , 5L , 9L ) ;
final SyncingResult expectedSyncingResult1 = new SyncingResult ( syncStatus1 ) ;
when ( synchronizer . getSyncStatus ( ) ) . thenReturn ( Optional . of ( syncStatus1 ) ) ;
syncingSubscriptionService . sendSyncingToMatchingSubscriptions ( ) ;
syncStatusListener . onSyncStatus ( syncStatus ) ;
verify ( subscriptionManager )
. sendMessage ( eq ( subscription . getId ( ) ) , refEq ( expectedSyncingResult1 ) ) ;
final SyncingResult expectedSyncingResult2 = new SyncingResult ( syncStatus2 ) ;
verify ( subscriptionManager ) . sendMessage ( eq ( subscription . getId ( ) ) , eq ( expectedSyncingResult ) ) ;
when ( synchronizer . getSyncStatus ( ) ) . thenReturn ( Optional . of ( syncStatus2 ) ) ;
syncingSubscriptionService . sendSyncingToMatchingSubscriptions ( ) ;
verify ( subscriptionManager )
. sendMessage ( eq ( subscription . getId ( ) ) , refEq ( expectedSyncingResult2 ) ) ;
}
}
}
}