// ------------------------------------------------------------ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. // ------------------------------------------------------------ namespace Dapr { using System; using System.Collections.Generic; 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(); static GrpcChannel channel = GrpcChannel.ForAddress("http://localhost"); /// /// Initializes a new instance of the class. /// internal StateTestClient() : base(new Autogenerated.Dapr.DaprClient(channel), null) { } public override ValueTask GetStateAsync(string storeName, string key, ConsistencyMode? consistencyMode = default, Dictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); if (this.State.TryGetValue(key, out var obj)) { return new ValueTask((TValue)obj); } else { return new ValueTask(default(TValue)); } } public override ValueTask> GetBulkStateAsync(string storeName, IReadOnlyList keys, int? parallelism, 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 new ValueTask>(response); } public override ValueTask<(TValue value, string etag)> GetStateAndETagAsync( string storeName, string key, ConsistencyMode? consistencyMode = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); if (this.State.TryGetValue(key, out var obj)) { return new ValueTask<(TValue value, string etag)>(((TValue)obj, "test_etag")); } else { return new ValueTask<(TValue value, string etag)>((default(TValue), "test_etag")); } } public override Task SaveStateAsync( string storeName, string key, TValue value, StateOptions stateOptions = default, Dictionary 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, Dictionary metadata = default, CancellationToken cancellationToken = default) { ArgumentVerifier.ThrowIfNullOrEmpty(storeName, nameof(storeName)); ArgumentVerifier.ThrowIfNullOrEmpty(key, nameof(key)); this.State.Remove(key); return Task.CompletedTask; } } }