public async Task StartAsync(CancellationToken cancellationToken = default) { if (State.IsRunning) { _userOutput.WriteLine("Sync is already running."); return; } State.IsRunning = true; _previousHash = _deltaIndexService.LatestDeltaIndex().Cid; _userOutput.WriteLine("Starting Sync..."); _scoredDeltaIndexRangeDisposable = _peerSyncManager.ScoredDeltaIndexRange.Subscribe(ProcessDeltaIndexRange); _deltaHeightWatcher.Start(); await _peerSyncManager.WaitForPeersAsync(cancellationToken).ConfigureAwait(false); var highestDeltaIndex = await _deltaHeightWatcher.GetHighestDeltaIndexAsync(); if (highestDeltaIndex == null || highestDeltaIndex.Height <= CurrentHighestDeltaIndexStored) { await Completed().ConfigureAwait(false); return; } State.CurrentBlock = State.StartingBlock = CurrentHighestDeltaIndexStored; State.HighestBlock = highestDeltaIndex.Height; _peerSyncManager.Start(); Progress(CurrentHighestDeltaIndexStored, _rangeSize); }
public async Task StartAsync_Should_Start_Sync() { _deltaHeightWatcher = Substitute.For <IDeltaHeightWatcher>(); _deltaHeightWatcher.GetHighestDeltaIndexAsync().Returns(new DeltaIndex { Cid = ByteString.Empty, Height = 10000 }); var sync = new Synchroniser(new SyncState(), _peerSyncManager, _deltaCache, _deltaHeightWatcher, _deltaHashProvider, _deltaDfsReader, _deltaIndexService, _mapperProvider, _userOutput, Substitute.For <ILogger>()); await sync.StartAsync(CancellationToken.None); sync.State.IsRunning.Should().BeTrue(); }
public async Task StopAsync_Should_Log_If_Not_Running_Sync() { _deltaHeightWatcher = Substitute.For <IDeltaHeightWatcher>(); _deltaHeightWatcher.GetHighestDeltaIndexAsync().Returns(new DeltaIndex { Cid = ByteString.Empty, Height = 10000 }); var sync = new Synchroniser(new SyncState(), _peerSyncManager, _deltaCache, _deltaHeightWatcher, _deltaHashProvider, _deltaDfsReader, _deltaIndexService, _mapperProvider, _userOutput, Substitute.For <ILogger>()); await sync.StopAsync(CancellationToken.None); _userOutput.Received(1).WriteLine("Sync is not currently running."); sync.State.IsRunning.Should().BeFalse(); }