public async Task ExecuteStateTransactionAsync_ThrowsForNonSuccess() { // Configure Client var httpClient = new TestHttpClient(); var daprClient = new DaprClientBuilder() .UseGrpcChannelOptions(new GrpcChannelOptions { HttpClient = httpClient }) .Build(); var widget1 = new Widget() { Size = "small", Color = "yellow", }; var state1 = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(widget1), StateOperationType.Upsert); var states = new List <StateTransactionRequest>(); states.Add(state1); var task = daprClient.ExecuteStateTransactionAsync("testStore", states); // Create Response & Respond httpClient.Requests.TryDequeue(out var entry).Should().BeTrue(); var response = GrpcUtils.CreateResponse(HttpStatusCode.NotAcceptable); entry.Completion.SetResult(response); await FluentActions.Awaiting(async() => await task).Should().ThrowAsync <RpcException>(); }
public async Task ExecuteStateTransactionAsync_ThrowsForNonSuccess() { await using var client = TestClient.CreateForDaprClient(); var widget1 = new Widget() { Size = "small", Color = "yellow", }; var state1 = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(widget1), StateOperationType.Upsert); var states = new List <StateTransactionRequest> { state1 }; var request = await client.CaptureGrpcRequestAsync(async daprClient => { await daprClient.ExecuteStateTransactionAsync("testStore", states); }); // Create Response & Respond var ex = await Assert.ThrowsAsync <DaprException>(async() => { await request.CompleteAsync(new HttpResponseMessage(HttpStatusCode.NotAcceptable)); }); Assert.IsType <RpcException>(ex.InnerException); }
public static async Task StateTransactionSliceAsync(this DaprClient client, string storeName, string streamHeadKey, StreamHead head, string headetag, Dictionary <string, string> meta, EventData[] versionedEvents, string sliceKey, string sliceetag) { var sliceReq = new StateTransactionRequest(sliceKey, JsonSerializer.SerializeToUtf8Bytes(versionedEvents), Client.StateOperationType.Upsert, string.IsNullOrWhiteSpace(sliceetag) ? null : sliceetag, metadata: meta); var headReq = new StateTransactionRequest(streamHeadKey, JsonSerializer.SerializeToUtf8Bytes(head), Client.StateOperationType.Upsert, etag: string.IsNullOrWhiteSpace(headetag) ? null : headetag, metadata: meta); var reqs = new List <StateTransactionRequest> { sliceReq, headReq }; await client.ExecuteStateTransactionAsync(storeName, reqs, meta); }
public static async Task StateTransactionAsync(this DaprClient client, string storeName, string streamName, string streamHeadKey, StreamHead head, string headetag, Dictionary <string, string> meta, EventData[] versionedEvents) { var eventsReq = versionedEvents.Select(x => new StateTransactionRequest(Naming.StreamKey(streamName, x.Version), JsonSerializer.SerializeToUtf8Bytes(x), StateOperationType.Upsert, metadata: meta)); var headReq = new StateTransactionRequest(streamHeadKey, JsonSerializer.SerializeToUtf8Bytes(head), Client.StateOperationType.Upsert, etag: string.IsNullOrWhiteSpace(headetag) ? null : headetag, metadata: meta); var reqs = new List <StateTransactionRequest>(); reqs.AddRange(eventsReq); reqs.Add(headReq); await client.ExecuteStateTransactionAsync(storeName, reqs, meta); }
internal static async Task ExecuteStateTransactionAsync(DaprClient client) { var value = new Widget() { Size = "small", Color = "yellow", }; var request1 = new StateTransactionRequest("mystate", JsonSerializer.SerializeToUtf8Bytes(value), StateOperationType.Upsert); var request2 = new StateTransactionRequest("mystate", null, StateOperationType.Delete); var requests = new List <StateTransactionRequest>(); requests.Add(request1); requests.Add(request2); Console.WriteLine("Executing transaction - save state and delete state"); await client.ExecuteStateTransactionAsync(storeName, requests); Console.WriteLine("Executed State Transaction!"); }
public async Task ExecuteStateTransactionAsync_WithCancelledToken() { await using var client = TestClient.CreateForDaprClient(); var operation = new StateTransactionRequest("test", null, StateOperationType.Delete); var operations = new List<StateTransactionRequest> { operation, }; var cts = new CancellationTokenSource(); cts.Cancel(); await Assert.ThrowsAsync<OperationCanceledException>(async () => { await client.InnerClient.ExecuteStateTransactionAsync("testStore", operations, new Dictionary<string, string>(), cancellationToken: cts.Token); }); }
public async Task ExecuteStateTransactionAsync_WithCancelledToken() { // Configure Client var httpClient = new TestHttpClient(); var daprClient = new DaprClientBuilder() .UseGrpcChannelOptions(new GrpcChannelOptions { HttpClient = httpClient, ThrowOperationCanceledOnCancellation = true }) .Build(); var ctSource = new CancellationTokenSource(); CancellationToken ct = ctSource.Token; ctSource.Cancel(); var operation = new StateTransactionRequest("test", null, StateOperationType.Delete); var operations = new List <StateTransactionRequest>(); operations.Add(operation); await FluentActions.Awaiting(async() => await daprClient.ExecuteStateTransactionAsync("testStore", operations, new Dictionary <string, string>(), cancellationToken: ct)) .Should().ThrowAsync <OperationCanceledException>(); }
public async Task ExecuteStateTransactionAsync_CanSaveState() { await using var client = TestClient.CreateForDaprClient(); var stateValue1 = new Widget() { Size = "small", Color = "yellow", }; var metadata1 = new Dictionary<string, string>() { {"a", "b" } }; var options1 = new StateOptions { Concurrency = ConcurrencyMode.LastWrite }; var state1 = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(stateValue1), StateOperationType.Upsert, "testEtag", metadata1, options1); var stateValue2 = 100; var state2 = new StateTransactionRequest("stateKey2", JsonSerializer.SerializeToUtf8Bytes(stateValue2), StateOperationType.Delete); var stateValue3 = "teststring"; var state3 = new StateTransactionRequest("stateKey3", JsonSerializer.SerializeToUtf8Bytes(stateValue3), StateOperationType.Upsert); var states = new List<StateTransactionRequest> { state1, state2, state3 }; var request = await client.CaptureGrpcRequestAsync(async daprClient => { await daprClient.ExecuteStateTransactionAsync("testStore", states); }); request.Dismiss(); // Get Request and validate var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.ExecuteStateTransactionRequest>(); envelope.StoreName.Should().Be("testStore"); envelope.Operations.Count.Should().Be(3); var req1 = envelope.Operations[0]; req1.Request.Key.Should().Be("stateKey1"); req1.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower()); var valueJson1 = req1.Request.Value.ToStringUtf8(); var value1 = JsonSerializer.Deserialize<Widget>(valueJson1, client.InnerClient.JsonSerializerOptions); value1.Size.Should().Be(stateValue1.Size); value1.Color.Should().Be(stateValue1.Color); req1.Request.Etag.Value.Should().Be("testEtag"); req1.Request.Metadata.Count.Should().Be(1); req1.Request.Metadata["a"].Should().Be("b"); req1.Request.Options.Concurrency.Should().Be(2); var req2 = envelope.Operations[1]; req2.Request.Key.Should().Be("stateKey2"); req2.OperationType.Should().Be(StateOperationType.Delete.ToString().ToLower()); var valueJson2 = req2.Request.Value.ToStringUtf8(); var value2 = JsonSerializer.Deserialize<int>(valueJson2, client.InnerClient.JsonSerializerOptions); value2.Should().Be(100); var req3 = envelope.Operations[2]; req3.Request.Key.Should().Be("stateKey3"); req3.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower()); var valueJson3 = req3.Request.Value.ToStringUtf8(); var value3 = JsonSerializer.Deserialize<string>(valueJson3, client.InnerClient.JsonSerializerOptions); value3.Should().Be("teststring"); }
public async Task ExecuteStateTransactionAsync_CanSaveState() { // Configure Client var httpClient = new TestHttpClient(); var daprClient = new DaprClientBuilder() .UseGrpcChannelOptions(new GrpcChannelOptions { HttpClient = httpClient }) .Build(); var stateValue1 = new Widget() { Size = "small", Color = "yellow", }; var metadata1 = new Dictionary <string, string>() { { "a", "b" } }; var options1 = new StateOptions(); options1.Concurrency = ConcurrencyMode.LastWrite; var state1 = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(stateValue1), StateOperationType.Upsert, "testEtag", metadata1, options1); var stateValue2 = 100; var state2 = new StateTransactionRequest("stateKey2", JsonSerializer.SerializeToUtf8Bytes(stateValue2), StateOperationType.Delete); var stateValue3 = "teststring"; var state3 = new StateTransactionRequest("stateKey3", JsonSerializer.SerializeToUtf8Bytes(stateValue3), StateOperationType.Upsert); var states = new List <StateTransactionRequest>(); states.Add(state1); states.Add(state2); states.Add(state3); var task = daprClient.ExecuteStateTransactionAsync("testStore", states); // Get Request and validate httpClient.Requests.TryDequeue(out var entry).Should().BeTrue(); var transactionRequest = await GrpcUtils.GetRequestFromRequestMessageAsync <Autogenerated.ExecuteStateTransactionRequest>(entry.Request); transactionRequest.StoreName.Should().Be("testStore"); transactionRequest.Operations.Count.Should().Be(3); var req1 = transactionRequest.Operations[0]; req1.Request.Key.Should().Be("stateKey1"); req1.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower()); var valueJson1 = req1.Request.Value.ToStringUtf8(); var value1 = JsonSerializer.Deserialize <Widget>(valueJson1); value1.Size.Should().Be(stateValue1.Size); value1.Color.Should().Be(stateValue1.Color); req1.Request.Etag.Should().Be("testEtag"); req1.Request.Metadata.Count.Should().Be(1); req1.Request.Metadata["a"].Should().Be("b"); req1.Request.Options.Concurrency.Should().Be(2); var req2 = transactionRequest.Operations[1]; req2.Request.Key.Should().Be("stateKey2"); req2.OperationType.Should().Be(StateOperationType.Delete.ToString().ToLower()); var valueJson2 = req2.Request.Value.ToStringUtf8(); var value2 = JsonSerializer.Deserialize <int>(valueJson2); value2.Should().Be(100); var req3 = transactionRequest.Operations[2]; req3.Request.Key.Should().Be("stateKey3"); req3.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower()); var valueJson3 = req3.Request.Value.ToStringUtf8(); var value3 = JsonSerializer.Deserialize <string>(valueJson3); value3.Should().Be("teststring"); }