public async Task GivenRequestWhenIdempotencyKeyAvaiableAndFailsSaveKeyAndHasResponseShouldReturnResponse() { var key = Guid.NewGuid().ToString(); _keyReaderMock.Setup(x => x.Read(It.IsAny <IRequest <IResult> >())) .Returns(key) .Verifiable(); _repositoryMock.Setup(x => x.TryAddAsync(key)) .ReturnsAsync(false) .Verifiable(); _repositoryMock.Setup(x => x.GetAsync <IdempotencyRegister>(key)) .ReturnsAsync(IdempotencyRegister.Of(key, "body")) .Verifiable(); _serializerMock.Setup(x => x.Deserialize <IResult>(It.IsAny <string>())) .Returns(Result <string> .Success("body")) .Verifiable(); var result = await _pipeline.Handle( _requestMock.Object, CancellationToken.None, _nextMock.Object); result.Should().NotBeNull(); result.Successful.Should().BeTrue(); result.As <string>().Should().Be("body"); _keyReaderMock.VerifyAll(); _repositoryMock.VerifyAll(); _serializerMock.VerifyAll(); }
public void GivenIdempotencySerializerShouldSerializeIdempotencyRegister() { var register = IdempotencyRegister.Of("key", "body"); var serializedObject = _serializer.Serialize(register); serializedObject.Should().NotBeNullOrWhiteSpace(); }
public void GivenIdempotencyRegisterWhenKeyIsEmptyShouldThrow(string key) { Func <IdempotencyRegister> action1 = () => IdempotencyRegister.Of(key, "body"); Func <IdempotencyRegister> action2 = () => IdempotencyRegister.Of(key); action1.Should().Throw <ArgumentNullException>(); action2.Should().Throw <ArgumentNullException>(); }
public void GivenIdempotencyRegisterShouldInstantiateWithKeyAndValue() { var register = IdempotencyRegister.Of("key", "body"); register.Should().NotBeNull(); register.Key.Should().Be("key"); register.Value.Should().Be("body"); register.IsCompleted.Should().BeTrue(); }
public void GivenIdempotencySerializerShouldDeserializeIdempotencyRegister() { var expectedRegister = IdempotencyRegister.Of("key", "body"); var serializedObject = _serializer.Serialize(expectedRegister); var register = _serializer.Deserialize <IdempotencyRegister>(serializedObject); register.Key.Should().Be(expectedRegister.Key); register.IsCompleted.Should().Be(expectedRegister.IsCompleted); register.Value.Should().Be(expectedRegister.Value); }
public async Task <bool> TryAddAsync(string key) { if (await _database.KeyExistsAsync((RedisKey)key)) { return(false); } using (var factory = RedLockFactory.Create(new[] { new RedLockMultiplexer(_database.Multiplexer) })) using (var redLock = factory.CreateLock(key, TimeSpan.FromMinutes(1))) { if (!redLock.IsAcquired || await _database.KeyExistsAsync((RedisKey)key)) { return(false); } var value = _serializer.Serialize(IdempotencyRegister.Of(key)); return(await _database.StringSetAsync(key, value, TimeSpan.FromMinutes(1), When.NotExists)); } }
GivenRequestWhenIdempotencyKeyAvaiableAndFailsSaveKeyAndDoesntHaveResponseShouldReturnStatusCode409() { var key = Guid.NewGuid().ToString(); _keyReaderMock.Setup(x => x.Read(It.IsAny <IRequest <IResult> >())) .Returns(key) .Verifiable(); _repositoryMock.Setup(x => x.TryAddAsync(key)) .ReturnsAsync(false) .Verifiable(); _repositoryMock.Setup(x => x.GetAsync <IdempotencyRegister>(key)) .ReturnsAsync(IdempotencyRegister.Of(key)) .Verifiable(); Func <Task <IResult> > action = () => _pipeline.Handle( _requestMock.Object, CancellationToken.None, _nextMock.Object); await action.Should().ThrowAsync <ConflictDetectedException>(); _keyReaderMock.VerifyAll(); _repositoryMock.VerifyAll(); }