Files
StockSharp/Tests/HookedFileSystem.cs
T
mika@stocksharp.com 6e7a7dae22 Move instrument lookups and persistence to async
Instrument lookups, configuration, credentials, CSV entities, snapshots and
the alert processing service are read and written asynchronously; the
blocking synchronous forms are obsolete wrappers.

IAsyncPersistable replaces IPersistable: every type that saves and loads its
settings has LoadAsync and SaveAsync only, and nested objects are read with
GetValueAsync, LoadEntireAsync and LoadAllAsync. The synchronous Load and Save
are the obsolete Ecng extensions. The MultiConnect sample keeps IPersistable
for two settings classes, because the settings window of the published
StockSharp.Xaml package takes it.

Copies that were made by saving and loading are made asynchronously.
IMessageAdapter, IIndicator, the risk, commission, PnL, latency, slippage and
position managers, IStorageBuffer, Strategy, DiagramElement, ICompositionModel
and its nodes get CloneAsync and no longer implement ICloneable; Clone is an
obsolete blocking wrapper. A strategy copy keeps every security and portfolio
parameter the original holds. Messages, FieldMapping, BankDetails and
WithdrawInfo are copied in memory. A CommissionRuleMessage shares its rule and
the emulator takes a copy of it. Envelope gets a constructor that takes all
three lines and CreateAsync.

ToBruteForce and ToBruteForceRandom become ToBruteForceAsync and
ToBruteForceRandomAsync and return IAsyncEnumerable; BruteForceOptimizer runs
over it and takes the next strategy through an asynchronous delegate.

Diagram elements keep their own state in SaveState, OnLoading and LoadState.
Undo restores an in-memory snapshot; a chart element holds its series and
axes in it by reference. An alert rule keeps its value in the form a message
carries it. A diagram strategy applies composition settings that were loaded
before the composition was set in SetCompositionAsync or at the start, and
saves them while they wait.

Strategies get an OnStartedAsync hook; diagram strategies resolve securities,
underlyings and strikes before the start so value processing never looks up
instruments. Paths.Serialize, SerializeInvariant, AppStartSettings.TrySave,
ICredentialsProvider.Save, ISnapshotSerializer.Serialize and the composition
registry gain async forms.

ICredentialsProvider.Delete becomes DeleteAsync, so loading, saving and
deleting the credentials wait for one another. Paths.InstalledVersion and
AppNameWithVersion are obsolete in favour of GetInstalledVersionAsync and
GetAppNameWithVersionAsync.

A file is opened for writing only after the last cancellation check, because
opening it empties it; a cancelled read reports the cancellation instead of
an empty result. A snapshot flush that failed leaves what it took due for the
next one.

The indicator templates for C#, F# and Python override LoadAsync and
SaveAsync.
2026-10-04 11:28:17 +03:00

53 lines
2.6 KiB
C#

namespace StockSharp.Tests;
/// <summary>
/// A file system that tells the test about a file right before it is opened, so the test can fail
/// the call or change something at exactly that moment.
/// </summary>
internal sealed class HookedFileSystem(IFileSystem inner) : IFileSystem
{
private readonly IFileSystem _inner = inner ?? throw new ArgumentNullException(nameof(inner));
/// <summary>
/// Called with the path and the access asked for, before the file is opened.
/// </summary>
public Action<string, FileAccess> Opening { get; set; }
public Stream Open(string path, FileMode mode, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.None)
{
Opening?.Invoke(path, access);
return _inner.Open(path, mode, access, share);
}
public long MaxSize
{
get => _inner.MaxSize;
set => _inner.MaxSize = value;
}
public FileSystemOverflowBehavior OverflowBehavior
{
get => _inner.OverflowBehavior;
set => _inner.OverflowBehavior = value;
}
public long TotalSize => _inner.TotalSize;
public bool FileExists(string path) => _inner.FileExists(path);
public bool DirectoryExists(string path) => _inner.DirectoryExists(path);
public void CreateDirectory(string path) => _inner.CreateDirectory(path);
public void DeleteDirectory(string path, bool recursive = false) => _inner.DeleteDirectory(path, recursive);
public void DeleteFile(string path) => _inner.DeleteFile(path);
public void MoveFile(string sourceFileName, string destFileName, bool overwrite = false) => _inner.MoveFile(sourceFileName, destFileName, overwrite);
public void MoveDirectory(string sourceDirName, string destDirName) => _inner.MoveDirectory(sourceDirName, destDirName);
public void CopyFile(string sourceFileName, string destFileName, bool overwrite = false) => _inner.CopyFile(sourceFileName, destFileName, overwrite);
public IEnumerable<string> EnumerateFiles(string path, string searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly) => _inner.EnumerateFiles(path, searchPattern, searchOption);
public IEnumerable<string> EnumerateDirectories(string path, string searchPattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly) => _inner.EnumerateDirectories(path, searchPattern, searchOption);
public DateTime GetCreationTimeUtc(string path) => _inner.GetCreationTimeUtc(path);
public DateTime GetLastWriteTimeUtc(string path) => _inner.GetLastWriteTimeUtc(path);
public long GetFileLength(string path) => _inner.GetFileLength(path);
public void SetReadOnly(string path, bool isReadOnly) => _inner.SetReadOnly(path, isReadOnly);
public FileAttributes GetAttributes(string path) => _inner.GetAttributes(path);
}