dotnet-sdk/test/Dapr.Client.Test/DistributedLockApiTest.cs

95 lines
3.5 KiB
C#

// ------------------------------------------------------------------------
// Copyright 2022 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
using System.Threading.Tasks;
using Autogenerated = Dapr.Client.Autogen.Grpc.v1;
using Xunit;
using Shouldly;
using System;
namespace Dapr.Client.Test;
[System.Obsolete]
public class DistributedLockApiTest
{
[Fact]
public async Task TryLockAsync_WithAllValues_ValidateRequest()
{
await using var client = TestClient.CreateForDaprClient();
string storeName = "redis";
string resourceId = "resourceId";
string lockOwner = "owner1";
Int32 expiryInSeconds = 1000;
var request = await client.CaptureGrpcRequestAsync(async daprClient =>
{
return await daprClient.Lock(storeName, resourceId, lockOwner, expiryInSeconds);
});
// Get Request and validate
var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.TryLockRequest>();
envelope.StoreName.ShouldBe("redis");
envelope.ResourceId.ShouldBe("resourceId");
envelope.LockOwner.ShouldBe("owner1");
envelope.ExpiryInSeconds.ShouldBe(1000);
// Get response and validate
var invokeResponse = new Autogenerated.TryLockResponse{
Success = true
};
var domainResponse = await request.CompleteWithMessageAsync(invokeResponse);
domainResponse.Success.ShouldBe(true);
}
[Fact]
public async Task TryLockAsync_WithAllValues_ArgumentVerifierException()
{
var client = new DaprClientBuilder().Build();
string storeName = "redis";
string resourceId = "resourceId";
string lockOwner = "owner1";
Int32 expiryInSeconds = 0;
// Get response and validate
await Assert.ThrowsAsync<ArgumentException>(async () => await client.Lock(storeName, resourceId, lockOwner, expiryInSeconds));
}
//Tests For Unlock API
[Fact]
public async Task UnLockAsync_WithAllValues_ValidateRequest()
{
await using var client = TestClient.CreateForDaprClient();
string storeName = "redis";
string resourceId = "resourceId";
string lockOwner = "owner1";
var request = await client.CaptureGrpcRequestAsync(async daprClient =>
{
return await daprClient.Unlock(storeName, resourceId, lockOwner);
});
// Get Request and validate
var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.UnlockRequest>();
envelope.StoreName.ShouldBe("redis");
envelope.ResourceId.ShouldBe("resourceId");
envelope.LockOwner.ShouldBe("owner1");
// Get response and validate
var invokeResponse = new Autogenerated.UnlockResponse{
Status = Autogenerated.UnlockResponse.Types.Status.LockDoesNotExist
};
var domainResponse = await request.CompleteWithMessageAsync(invokeResponse);
domainResponse.status.ShouldBe(LockStatus.LockDoesNotExist);
}
}