// ------------------------------------------------------------ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. // ------------------------------------------------------------ namespace Dapr.Client { using System; using System.Collections.Generic; using System.Net.Http; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Dapr.Client; using Grpc.Net.Client; using Autogenerated = Dapr.Client.Autogen.Grpc.v1; internal class StateTestClient : DaprClientGrpc { public Dictionary State { get; } = new Dictionary(); private static readonly GrpcChannel channel = GrpcChannel.ForAddress("http://localhost"); /// /// Initializes a new instance of the class. /// internal StateTestClient() : base(channel, new Autogenerated.Dapr.DaprClient(channel), new HttpClient(), new Uri("http://localhost"), null, default) { } public override Task GetStateAsync(string storeName, string key, ConsistencyMode? consistencyMode = default, IReadOnlyDictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); if (this.State.TryGetValue(key, out var obj)) { return Task.FromResult((TValue)obj); } else { return Task.FromResult(default(TValue)); } } public override Task> GetBulkStateAsync(string storeName, IReadOnlyList keys, int? parallelism, IReadOnlyDictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); var response = new List(); foreach (var key in keys) { if (this.State.TryGetValue(key, out var obj)) { response.Add(new BulkStateItem(key, obj.ToString(), "")); } else { response.Add(new BulkStateItem(key, "", "")); } } return Task.FromResult>(response); } public override Task<(TValue value, string etag)> GetStateAndETagAsync( string storeName, string key, ConsistencyMode? consistencyMode = default, IReadOnlyDictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); if (this.State.TryGetValue(key, out var obj)) { return Task.FromResult(((TValue)obj, "test_etag")); } else { return Task.FromResult((default(TValue), "test_etag")); } } public override Task SaveStateAsync( string storeName, string key, TValue value, StateOptions stateOptions = default, IReadOnlyDictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); this.State[key] = value; return Task.CompletedTask; } public override Task DeleteStateAsync( string storeName, string key, StateOptions stateOptions = default, IReadOnlyDictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); this.State.Remove(key); return Task.CompletedTask; } } }