예제 #1
0
            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);
                    }
                }
            }
예제 #2
0
        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));
        }
예제 #3
0
        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);
        }
예제 #4
0
 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);
 }
예제 #5
0
 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);
 }
예제 #6
0
 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);
 }
예제 #7
0
 public void SetValue <T>(string key, T value)
 {
     Values[key] = FakeConfiguration.Serialize(value);
 }
예제 #8
0
 public T GetValue <T>(string key, Func <T> defaultValueFactory)
 {
     return(FakeConfiguration.Deserialize <T>(Values.GetOrAdd(key, _k => FakeConfiguration.Serialize(defaultValueFactory()))));
 }
예제 #9
0
 public T GetValue <T>(string key)
 {
     return(FakeConfiguration.Deserialize <T>(Values[key]));
 }
예제 #10
0
 public async Task AddAsync(T item, CancellationToken cancellationToken = default)
 {
     await _channelWriter.WriteAsync(FakeConfiguration.Serialize(item), cancellationToken);
 }