Skip to content

Commit b6b3c7f

Browse files
committed
Refactor tests. Remove direct environment check for Idempotency Disabled.
1 parent 2424868 commit b6b3c7f

File tree

5 files changed

+60
-128
lines changed

5 files changed

+60
-128
lines changed

libraries/src/AWS.Lambda.Powertools.Idempotency/IdempotentAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ protected internal sealed override T WrapSync<T>(Func<object[], T> target, objec
8383
protected internal sealed override async Task<T> WrapAsync<T>(
8484
Func<object[], Task<T>> target, object[] args, AspectEventArgs eventArgs)
8585
{
86-
var idempotencyDisabledEnv = Environment.GetEnvironmentVariable(Constants.IdempotencyDisabledEnv);
87-
if (idempotencyDisabledEnv is "true")
86+
if (PowertoolsConfigurations.Instance.IdempotencyDisabled)
8887
{
8988
return await base.WrapAsync(target, args, eventArgs);
9089
}

libraries/src/AWS.Lambda.Powertools.Idempotency/Output/ConsoleLog.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

libraries/src/AWS.Lambda.Powertools.Idempotency/Persistence/DynamoDBPersistenceStore.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ internal DynamoDBPersistenceStore(string tableName,
6565
}
6666
else
6767
{
68-
var isIdempotencyDisabled = bool.TryParse(Environment.GetEnvironmentVariable(Constants.IdempotencyDisabledEnv), out var result) && result;
69-
70-
if (isIdempotencyDisabled)
68+
if (PowertoolsConfigurations.Instance.IdempotencyDisabled)
7169
{
7270
// we do not want to create a DynamoDbClient if idempotency is disabled
7371
// null is ok as idempotency won't be called

libraries/tests/AWS.Lambda.Powertools.Idempotency.Tests/Internal/IdempotencyTests.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,5 @@ namespace AWS.Lambda.Powertools.Idempotency.Tests.Internal;
66

77
public class IdempotencyTests
88
{
9-
[Fact]
10-
public void Idempotency_Set_Execution_Environment_Context()
11-
{
12-
// Arrange
13-
var assemblyName = "AWS.Lambda.Powertools.Idempotency";
14-
var assemblyVersion = "1.0.0";
15-
16-
var env = new Mock<IPowertoolsEnvironment>();
17-
env.Setup(x => x.GetAssemblyName(It.IsAny<Idempotency>())).Returns(assemblyName);
18-
env.Setup(x => x.GetAssemblyVersion(It.IsAny<Idempotency>())).Returns(assemblyVersion);
19-
20-
var conf = new PowertoolsConfigurations(new SystemWrapper(env.Object));
21-
22-
// Act
23-
var xRayRecorder = new Idempotency(conf);
24-
25-
// Assert
26-
env.Verify(v =>
27-
v.SetEnvironmentVariable(
28-
"AWS_EXECUTION_ENV", $"{Constants.FeatureContextIdentifier}/Idempotency/{assemblyVersion}"
29-
), Times.Once);
30-
31-
env.Verify(v =>
32-
v.GetEnvironmentVariable(
33-
"AWS_EXECUTION_ENV"
34-
), Times.Once);
35-
36-
Assert.NotNull(xRayRecorder);
37-
}
9+
3810
}

libraries/tests/AWS.Lambda.Powertools.Idempotency.Tests/Internal/IdempotentAspectTests.cs

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
namespace AWS.Lambda.Powertools.Idempotency.Tests.Internal;
3232

3333
[Collection("Sequential")]
34-
public class IdempotentAspectTests
34+
public class IdempotentAspectTests : IDisposable
3535
{
3636
[Fact]
3737
public async Task Handle_WhenFirstCall_ShouldPutInStore()
@@ -157,33 +157,62 @@ public async Task Handle_WhenThrowException_ShouldDeleteRecord_AndThrowFunctionE
157157
[Fact]
158158
public async Task Handle_WhenIdempotencyDisabled_ShouldJustRunTheFunction()
159159
{
160-
try
161-
{
162-
// Arrange
163-
var store = new Mock<BasePersistenceStore>();
164-
165-
Environment.SetEnvironmentVariable(Constants.IdempotencyDisabledEnv, "true");
166-
167-
Idempotency.Configure(builder =>
168-
builder
169-
.WithPersistenceStore(store.Object)
170-
.WithOptions(optionsBuilder => optionsBuilder.WithEventKeyJmesPath("Id"))
171-
);
172-
173-
var function = new IdempotencyEnabledFunction();
174-
var product = new Product(42, "fake product", 12);
175-
176-
// Act
177-
var basket = await function.Handle(product, new TestLambdaContext());
160+
161+
// Arrange
162+
var store = new Mock<BasePersistenceStore>();
163+
164+
Environment.SetEnvironmentVariable(Constants.IdempotencyDisabledEnv, "true");
165+
166+
Idempotency.Configure(builder =>
167+
builder
168+
.WithPersistenceStore(store.Object)
169+
.WithOptions(optionsBuilder => optionsBuilder.WithEventKeyJmesPath("Id"))
170+
);
171+
172+
var function = new IdempotencyEnabledFunction();
173+
var product = new Product(42, "fake product", 12);
174+
175+
// Act
176+
var basket = await function.Handle(product, new TestLambdaContext());
177+
178+
// Assert
179+
store.Invocations.Count.Should().Be(0);
180+
basket.Products.Count.Should().Be(1);
181+
function.HandlerExecuted.Should().BeTrue();
182+
}
178183

179-
// Assert
180-
store.Invocations.Count.Should().Be(0);
181-
basket.Products.Count.Should().Be(1);
182-
function.HandlerExecuted.Should().BeTrue();
183-
}
184-
finally
185-
{
186-
Environment.SetEnvironmentVariable(Constants.IdempotencyDisabledEnv, "false");
187-
}
184+
[Fact]
185+
public void Idempotency_Set_Execution_Environment_Context()
186+
{
187+
// Arrange
188+
var assemblyName = "AWS.Lambda.Powertools.Idempotency";
189+
var assemblyVersion = "1.0.0";
190+
191+
var env = new Mock<IPowertoolsEnvironment>();
192+
env.Setup(x => x.GetAssemblyName(It.IsAny<Idempotency>())).Returns(assemblyName);
193+
env.Setup(x => x.GetAssemblyVersion(It.IsAny<Idempotency>())).Returns(assemblyVersion);
194+
195+
var conf = new PowertoolsConfigurations(new SystemWrapper(env.Object));
196+
197+
// Act
198+
var xRayRecorder = new Idempotency(conf);
199+
200+
// Assert
201+
env.Verify(v =>
202+
v.SetEnvironmentVariable(
203+
"AWS_EXECUTION_ENV", $"{Constants.FeatureContextIdentifier}/Idempotency/{assemblyVersion}"
204+
), Times.Once);
205+
206+
env.Verify(v =>
207+
v.GetEnvironmentVariable(
208+
"AWS_EXECUTION_ENV"
209+
), Times.Once);
210+
211+
Assert.NotNull(xRayRecorder);
212+
}
213+
214+
public void Dispose()
215+
{
216+
Environment.SetEnvironmentVariable(Constants.IdempotencyDisabledEnv, "false");
188217
}
189218
}

0 commit comments

Comments
 (0)