public IExpressStore GetCheckpointStore(string checkPointPath) { if (chain.ConsensusNodes.Count != 1) { throw new ArgumentException("Checkpoint restore is only supported on single node express instances", nameof(chain)); } var node = chain.ConsensusNodes[0]; if (IsRunning(node)) { throw new Exception($"node already running"); } checkPointPath = ResolveCheckpointFileName(checkPointPath); if (!fileSystem.File.Exists(checkPointPath)) { throw new Exception($"Checkpoint {checkPointPath} couldn't be found"); } var checkpointTempPath = fileSystem.GetTempFolder(); var folderCleanup = AnonymousDisposable.Create(() => { if (fileSystem.Directory.Exists(checkpointTempPath)) { fileSystem.Directory.Delete(checkpointTempPath, true); } }); var multiSigAccount = node.Wallet.Accounts.Single(a => a.IsMultiSigContract()); RocksDbStore.RestoreCheckpoint(checkPointPath, checkpointTempPath, chain.Magic, multiSigAccount.ScriptHash); return(new CheckpointStore(RocksDbStore.OpenReadOnly(checkpointTempPath), true, folderCleanup)); }
public async Task Add_AfterDisposeStarts_InvokesActionAfterDisposeCompletes() { bool action1Invoked = false; bool action2Invoked = false; var ready = new ManualResetEventSlim(); var signal = new ManualResetEventSlim(); var disposable = AnonymousDisposable.Create(() => { action1Invoked = true; ready.Set(); signal.Wait(); }); var disposeTask = Task.Run(() => disposable.Dispose()); ready.Wait(); var addTask = Task.Run(() => disposable.Add(() => { action2Invoked = true; })); Assert.False(addTask.Wait(100)); Assert.True(action1Invoked); Assert.False(action2Invoked); signal.Set(); await disposeTask; await addTask; Assert.True(action2Invoked); }
public void Dispose_InvokesAction() { bool actionInvoked = false; var disposable = AnonymousDisposable.Create(() => { actionInvoked = true; }); disposable.Dispose(); Assert.True(actionInvoked); }
public void Dispose_AfterAddingNull_DoesNotThrow() { bool action1Invoked = false; var disposable = AnonymousDisposable.Create(() => { action1Invoked = true; }); disposable.Add(null); disposable.Dispose(); Assert.True(action1Invoked); }
public void MultipleDispose_OnlyInvokesActionOnce() { var counter = 0; var disposable = AnonymousDisposable.Create(() => { ++counter; }); disposable.Dispose(); disposable.Dispose(); Assert.Equal(1, counter); }
public static IDisposable GetDeleteDirectoryDisposable(string path) { return(AnonymousDisposable.Create(() => { if (Directory.Exists(path)) { Directory.Delete(path, true); } })); }
public void Dispose_AfterAdd_InvokesBothActions() { bool action1Invoked = false; bool action2Invoked = false; var disposable = AnonymousDisposable.Create(() => { action1Invoked = true; }); disposable.Add(() => { action2Invoked = true; }); disposable.Dispose(); Assert.True(action1Invoked); Assert.True(action2Invoked); }
public async Task AllowsMixedChildren() { bool action1Invoked = false; bool action2Invoked = false; var disposable = CollectionAsyncDisposable.Create( new AnonymousAsyncDisposable(async() => { action1Invoked = true; }), AnonymousDisposable.Create(() => action2Invoked = true).ToAsyncDisposable()); await disposable.DisposeAsync(); Assert.True(action1Invoked); Assert.True(action2Invoked); }
public void RestoreCheckpoint(string checkPointArchive, bool force) { if (chain.ConsensusNodes.Count != 1) { throw new ArgumentException("Checkpoint restore is only supported on single node express instances", nameof(chain)); } checkPointArchive = ResolveCheckpointFileName(checkPointArchive); if (!fileSystem.File.Exists(checkPointArchive)) { throw new Exception($"Checkpoint {checkPointArchive} couldn't be found"); } var node = chain.ConsensusNodes[0]; if (IsNodeRunning(node)) { var scriptHash = node.Wallet.DefaultAccount?.ScriptHash ?? "<unknown>"; throw new InvalidOperationException($"node {scriptHash} currently running"); } var checkpointTempPath = fileSystem.GetTempFolder(); using var folderCleanup = AnonymousDisposable.Create(() => { if (fileSystem.Directory.Exists(checkpointTempPath)) { fileSystem.Directory.Delete(checkpointTempPath, true); } }); var nodePath = fileSystem.GetNodePath(node); if (fileSystem.Directory.Exists(nodePath)) { if (!force) { throw new Exception("You must specify force to restore a checkpoint to an existing blockchain."); } fileSystem.Directory.Delete(nodePath, true); } var wallet = DevWallet.FromExpressWallet(ProtocolSettings, node.Wallet); var multiSigAccount = wallet.GetMultiSigAccounts().Single(); RocksDbUtility.RestoreCheckpoint(checkPointArchive, checkpointTempPath, ProtocolSettings.Network, ProtocolSettings.AddressVersion, multiSigAccount.ScriptHash); fileSystem.Directory.Move(checkpointTempPath, nodePath); }
public void RestoreCheckpoint(string checkPointArchive, bool force) { if (chain.ConsensusNodes.Count != 1) { throw new ArgumentException("Checkpoint restore is only supported on single node express instances", nameof(chain)); } checkPointArchive = ResolveCheckpointFileName(checkPointArchive); if (!fileSystem.File.Exists(checkPointArchive)) { throw new Exception($"Checkpoint {checkPointArchive} couldn't be found"); } var node = chain.ConsensusNodes[0]; if (IsRunning(node)) { var scriptHash = node.Wallet.DefaultAccount?.ScriptHash ?? "<unknown>"; throw new InvalidOperationException($"node {scriptHash} currently running"); } var checkpointTempPath = fileSystem.GetTempFolder(); using var folderCleanup = AnonymousDisposable.Create(() => { if (fileSystem.Directory.Exists(checkpointTempPath)) { fileSystem.Directory.Delete(checkpointTempPath, true); } }); var multiSigAccount = node.Wallet.Accounts.Single(a => a.IsMultiSigContract()); var nodeFolder = fileSystem.GetNodePath(node); if (fileSystem.Directory.Exists(nodeFolder)) { if (force) { fileSystem.Directory.Delete(nodeFolder, true); } else { throw new Exception("You must specify force to restore a checkpoint to an existing blockchain."); } } RocksDbStore.RestoreCheckpoint(checkPointArchive, checkpointTempPath, chain.Magic, multiSigAccount.ScriptHash); fileSystem.Directory.Move(checkpointTempPath, nodeFolder); }
protected ReportViewerViewModel(ILog log, IDispatcherSchedulerProvider scheduler, IStandardDialog standardDialog, TReportViewerService service, IToolBarService toolBarService, HistoryViewModel historyViewModel, BindableCollection <IViewModel> itemsCollection) : base(log, scheduler, standardDialog) { Service = service; ToolBarService = toolBarService; _historyViewModel = historyViewModel; Items = itemsCollection; this.SetupHeader(scheduler, "Viewer"); _historyViewModel.Open += Open; Disposables.Add(AnonymousDisposable.Create(() => _historyViewModel.Open -= Open)); }
public void when_Dispose_is_called_then_added_IDisposable_is_disposed() { var disposables = new CompositeDisposable(); var eventWasFired = false; var disposable = AnonymousDisposable.Create(() => eventWasFired = true); disposables.Add(disposable); Assert.That(eventWasFired, Is.False); disposables.Dispose(); Assert.That(eventWasFired, Is.True); }
/// <summary> /// Synchronously waits on the semaphore, and returns a disposable that releases the semaphore when disposed, thus treating this semaphore as a "multi-lock". /// </summary> /// <param name="this">The semaphore to lock.</param> /// <param name="cancellationToken">The cancellation token used to cancel the wait.</param> public static IDisposable Lock(this SemaphoreSlim @this, CancellationToken cancellationToken) { @this.Wait(cancellationToken); return(AnonymousDisposable.Create(() => @this.Release())); }
public WorkspaceViewModel(ILog log, IDispatcherService dispatcherService) : base(log, dispatcherService) { Disposables.Add(AnonymousDisposable.Create(() => DisposablesWasDisposed.SafeInvoke(this))); }
public WorkspaceViewModel(ILog log, IDispatcherSchedulerProvider scheduler, IStandardDialog standardDialog) : base(log, scheduler, standardDialog) { Disposables.Add(AnonymousDisposable.Create(() => DisposablesWasDisposed.SafeInvoke(this))); }
private static async Task <IDisposable> DoLockAsync(SemaphoreSlim @this, CancellationToken cancellationToken) { await @this.WaitAsync(cancellationToken).ConfigureAwait(false); return(AnonymousDisposable.Create(() => @this.Release())); }
public WorkspaceViewModel(ILog log, ISchedulerProvider scheduler, IViewService viewService) : base(log, scheduler, viewService) { Disposables.Add(AnonymousDisposable.Create(() => DisposablesWasDisposed.SafeInvoke(this))); }
public void Dispose_NullAction_DoesNotThrow() { var disposable = AnonymousDisposable.Create(null); disposable.Dispose(); }