Skip to content

Commit

Permalink
AWS Lambda unit tests refactoring: renaming, removing redundant tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
rypdal committed Jul 25, 2022
1 parent 3cc4629 commit 274e6ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public AWSLambdaWrapperTests()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TestLambdaHandler(bool setCustomParent)
public void TraceSyncWithInputAndReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

Expand All @@ -59,7 +59,7 @@ public void TestLambdaHandler(bool setCustomParent)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncReturn, "TestStream", this.sampleLambdaContext, parentContext);
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand All @@ -75,7 +75,7 @@ public void TestLambdaHandler(bool setCustomParent)
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TestLambdaHandlerNoReturn(bool setCustomParent)
public void TraceSyncWithInputAndNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

Expand All @@ -85,7 +85,7 @@ public void TestLambdaHandlerNoReturn(bool setCustomParent)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncNoReturn, "TestStream", this.sampleLambdaContext, parentContext);
AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndNoReturn, "TestStream", this.sampleLambdaContext, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand All @@ -101,7 +101,7 @@ public void TestLambdaHandlerNoReturn(bool setCustomParent)
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task TestLambdaHandlerAsync(bool setCustomParent)
public void TraceSyncWithNoInputAndNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

Expand All @@ -111,7 +111,7 @@ public async Task TestLambdaHandlerAsync(bool setCustomParent)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
var result = await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncReturn, "TestStream", this.sampleLambdaContext, parentContext);
AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncNoInputAndNoReturn, this.sampleLambdaContext, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand All @@ -127,7 +127,7 @@ public async Task TestLambdaHandlerAsync(bool setCustomParent)
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task TestLambdaHandlerAsyncNoReturn(bool setCustomParent)
public async Task TraceAsyncWithInputAndReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

Expand All @@ -137,7 +137,7 @@ public async Task TestLambdaHandlerAsyncNoReturn(bool setCustomParent)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncNoReturn, "TestStream", this.sampleLambdaContext, parentContext);
var result = await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncInputAndReturn, "TestStream", this.sampleLambdaContext, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand All @@ -153,7 +153,7 @@ public async Task TestLambdaHandlerAsyncNoReturn(bool setCustomParent)
[Theory]
[InlineData(false)]
[InlineData(true)]
public void TestLambdaHandlerNoContext(bool setCustomParent)
public async Task TraceAsyncWithInputAndNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

Expand All @@ -163,57 +163,7 @@ public void TestLambdaHandlerNoContext(bool setCustomParent)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncReturn, "TestStream", new Mock<ILambdaContext>().Object, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);

var activity = (Activity)processor.Invocations[1].Arguments[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void TestLambdaHandlerNoContextNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncNoReturn, "TestStream", new Mock<ILambdaContext>().Object, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}

// SetParentProvider -> OnStart -> OnEnd -> OnForceFlush -> OnShutdown -> Dispose
Assert.Equal(6, processor.Invocations.Count);

var activity = (Activity)processor.Invocations[1].Arguments[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task TestLambdaHandlerAsyncNoContext(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations()
.AddProcessor(processor.Object)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
var result = await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncReturn, "TestStream", new Mock<ILambdaContext>().Object, parentContext);
await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncInputAndNoReturn, "TestStream", this.sampleLambdaContext, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand All @@ -223,12 +173,13 @@ public async Task TestLambdaHandlerAsyncNoContext(bool setCustomParent)

var activity = (Activity)processor.Invocations[1].Arguments[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task TestLambdaHandlerAsyncNoContextNoReturn(bool setCustomParent)
public async Task TraceAsyncWithNoInputAndNoReturn(bool setCustomParent)
{
var processor = new Mock<BaseProcessor<Activity>>();

Expand All @@ -238,7 +189,7 @@ public async Task TestLambdaHandlerAsyncNoContextNoReturn(bool setCustomParent)
.Build())
{
var parentContext = setCustomParent ? CreateParentContext() : default;
await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncNoReturn, "TestStream", new Mock<ILambdaContext>().Object, parentContext);
await AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerAsyncNoInputAndNoReturn, this.sampleLambdaContext, parentContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand All @@ -248,6 +199,7 @@ public async Task TestLambdaHandlerAsyncNoContextNoReturn(bool setCustomParent)

var activity = (Activity)processor.Invocations[1].Arguments[0];
this.AssertSpanProperties(activity, setCustomParent ? CustomParentId : XRayParentId);
this.AssertSpanAttributes(activity);
}

[Theory]
Expand Down Expand Up @@ -295,7 +247,7 @@ public void TestLambdaHandlerNotSampled()
.AddProcessor(processor.Object)
.Build())
{
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncReturn, "TestStream", this.sampleLambdaContext);
var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext);
var resource = tracerProvider.GetResource();
this.AssertResourceAttributes(resource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,39 @@ namespace OpenTelemetry.Contrib.Instrumentation.AWSLambda.Tests
{
public class SampleHandlers
{
public void SampleHandlerSyncNoReturn(string str, ILambdaContext context)
// Action<TInput, ILambdaContext>
public void SampleHandlerSyncInputAndNoReturn(string str, ILambdaContext context)
{
}

public void SampleHandlerSyncNoReturn(string str)
// Action<ILambdaContext>
public void SampleHandlerSyncNoInputAndNoReturn(ILambdaContext context)
{
}

public string SampleHandlerSyncReturn(string str, ILambdaContext context)
// Func<TInput, ILambdaContext, TResult>
public string SampleHandlerSyncInputAndReturn(string str, ILambdaContext context)
{
return str;
}

public string SampleHandlerSyncReturn(string str)
{
return str;
}

public async Task SampleHandlerAsyncNoReturn(string str, ILambdaContext context)
// Func<TInput, ILambdaContext, Task>
public async Task SampleHandlerAsyncInputAndNoReturn(string str, ILambdaContext context)
{
await Task.Delay(10);
}

public async Task SampleHandlerAsyncNoReturn(string str)
{
await Task.Delay(10);
}

public async Task<string> SampleHandlerAsyncReturn(string str, ILambdaContext context)
// Func<TInput, ILambdaContext, Task<TResult>>
public async Task<string> SampleHandlerAsyncInputAndReturn(string str, ILambdaContext context)
{
await Task.Delay(10);
return str;
}

public async Task<string> SampleHandlerAsyncReturn(string str)
// Func<ILambdaContext, Task>
public async Task SampleHandlerAsyncNoInputAndNoReturn(ILambdaContext context)
{
await Task.Delay(10);
return str;
}

public void SampleHandlerSyncNoReturnException(string str, ILambdaContext context)
Expand Down

0 comments on commit 274e6ba

Please sign in to comment.