public async Task ReleaseLockRequest_ThrowsOnInvalidInput(string lockId, string ownerId) { Mock <IEnvironment> _environment = new Mock <IEnvironment>(); var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); var httpClient = new HttpClient(handlerMock.Object); var lockManager = new KubernetesDistributedLockManager( new KubernetesClient(_environment.Object, httpClient), ScriptSettingsManager.Instance); var inputLock = new KubernetesLockHandle() { LockId = lockId, Owner = ownerId }; await Assert.ThrowsAsync <ArgumentNullException>(() => lockManager.ReleaseLockAsync(inputLock, new CancellationToken())); }
public async Task GetLockRequest_ReturnsCorrectLockHandle(HttpStatusCode status, string lockId, string ownerId) { Mock <IEnvironment> _environment = new Mock <IEnvironment>(); _environment.Setup(p => p.GetEnvironmentVariable(HttpLeaderEndpoint)).Returns("http://test-endpoint"); var expectedLockHandle = new KubernetesLockHandle() { Owner = ownerId }; var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.Is <HttpRequestMessage>(request => request.Method == HttpMethod.Get && request.RequestUri.AbsoluteUri.Equals($"{TestHttpLeaderEndpoint}/lock?name={lockId}")), ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage { StatusCode = status, Content = new StringContent(JsonConvert.SerializeObject(expectedLockHandle)) }); var httpClient = new HttpClient(handlerMock.Object); var lockClient = new KubernetesClient(_environment.Object, httpClient); var lockManager = new KubernetesDistributedLockManager(lockClient, ScriptSettingsManager.Instance); if (status != HttpStatusCode.OK) { await Assert.ThrowsAsync <HttpRequestException>(() => lockManager.GetLockOwnerAsync( "", lockId, new CancellationToken())); } else { var actualOwner = await lockManager.GetLockOwnerAsync("", lockId, new CancellationToken()); Assert.Equal(expectedLockHandle.Owner, actualOwner); } }