private async IAsyncEnumerable <T> Impl([EnumeratorCancellation] CancellationToken cancellationToken = default) { if (Replay) { for (var i = 0; i < _stream.StoredData.Count; i++) // Not using foreach, as StoredData can change asynchronously { var value = FakeConfiguration.Deserialize <T>(_stream.StoredData[i]); if (Filter(value)) { // NOTE: We are not saving state entries here yield return(value); } } } // NOTE: Race condition: can miss elements while switching from replay to realtime await foreach (var i in _stream.GetChannel().ReadAllAsync()) { var value = FakeConfiguration.Deserialize <T>(_stream.StoredData[i]); if (Filter(value)) { // NOTE: We are not saving state entries here yield return(value); } } }
internal async Task <TResult> CallAsync <TParams, TResult>(string functionDelegate, TParams parameters) { if (!functions.TryGetValue(functionDelegate, out var function)) { throw new IndexOutOfRangeException("Function '" + functionDelegate + "' is not registered."); } var input = FakeConfiguration.Serialize(parameters); var result = await function(input); return(FakeConfiguration.Deserialize <TResult>(result)); }
public static (IStream <T>, string) CreateBlankStream <T>() { var channel = Channel.CreateUnbounded <object?>(); var stream = new FakeStream <T>(channel.Reader.ReadAllAsync().Select(x => FakeConfiguration.Deserialize <T>(x))); var name = $"-{Guid.NewGuid()}"; blankStreams[name] = channel.Writer; return(stream, name); }
public FakeAgent RegisterFunction <TParams>(string functionDelegate, Func <TParams, Task> function) { functions.Add(functionDelegate, async(input) => { await function(FakeConfiguration.Deserialize <TParams>(input)); return(null); }); return(this); }
public FakeAgent RegisterFunction <TParams>(string functionDelegate, Action <TParams> function) { functions.Add(functionDelegate, (input) => { function(FakeConfiguration.Deserialize <TParams>(input)); return(Task.FromResult <object?>(null)); }); return(this); }
public FakeAgent RegisterFunction <TParams, TResult>(string functionDelegate, Func <TParams, TResult> function) { functions.Add(functionDelegate, (input) => Task.FromResult(FakeConfiguration.Serialize(function(FakeConfiguration.Deserialize <TParams>(input))))); return(this); }
public void SetValue <T>(string key, T value) { Values[key] = FakeConfiguration.Serialize(value); }
public T GetValue <T>(string key, Func <T> defaultValueFactory) { return(FakeConfiguration.Deserialize <T>(Values.GetOrAdd(key, _k => FakeConfiguration.Serialize(defaultValueFactory())))); }
public T GetValue <T>(string key) { return(FakeConfiguration.Deserialize <T>(Values[key])); }
public async Task AddAsync(T item, CancellationToken cancellationToken = default) { await _channelWriter.WriteAsync(FakeConfiguration.Serialize(item), cancellationToken); }