Skip to content

Commit b230801

Browse files
committed
add tests
1 parent 0b72c06 commit b230801

File tree

5 files changed

+121
-6
lines changed

5 files changed

+121
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.OpenTelemetry.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
using Microsoft.Azure.Functions.Worker.Middleware;
7+
using Moq;
8+
using OpenTelemetry;
9+
using Xunit;
10+
11+
namespace Microsoft.Azure.Functions.Worker.OpenTelemetry.Tests
12+
{
13+
public class BaggageMiddlewareTests
14+
{
15+
[Fact]
16+
public async Task Invoke_SetsAndClearsBaggage_WhenBaggagePresent()
17+
{
18+
var baggageKey = "TestKey";
19+
var baggageValue = "TestValue";
20+
var baggageDict = new List<KeyValuePair<string, string>>
21+
{
22+
new KeyValuePair<string, string>(baggageKey, baggageValue)
23+
};
24+
25+
var contextMock = new Mock<FunctionContext>();
26+
var items = new Dictionary<object, object>
27+
{
28+
{ TraceConstants.BaggageKeyName, baggageDict }
29+
};
30+
contextMock.Setup(c => c.Items).Returns(items);
31+
32+
bool nextCalled = false;
33+
var middleware = new BaggageMiddleware();
34+
35+
// baggage is set before next is called
36+
FunctionExecutionDelegate next = async ctx =>
37+
{
38+
nextCalled = true;
39+
Assert.Equal(baggageValue, Baggage.GetBaggage(baggageKey));
40+
await Task.CompletedTask;
41+
};
42+
43+
await middleware.Invoke(contextMock.Object, next);
44+
45+
// cleared after invoking
46+
Assert.True(nextCalled);
47+
Assert.Null(Baggage.GetBaggage(baggageKey));
48+
}
49+
50+
[Fact]
51+
public async Task Invoke_ClearsBaggage_OnException()
52+
{
53+
var baggageKey = "TestKey";
54+
var baggageValue = "TestValue";
55+
var baggageDict = new List<KeyValuePair<string, string>>
56+
{
57+
new KeyValuePair<string, string>(baggageKey, baggageValue)
58+
};
59+
60+
var contextMock = new Mock<FunctionContext>();
61+
var items = new Dictionary<object, object>
62+
{
63+
{ TraceConstants.BaggageKeyName, baggageDict }
64+
};
65+
contextMock.Setup(c => c.Items).Returns(items);
66+
67+
var middleware = new BaggageMiddleware();
68+
69+
FunctionExecutionDelegate next = ctx =>
70+
{
71+
Assert.Equal(baggageValue, Baggage.GetBaggage(baggageKey));
72+
throw new System.Exception("Test exception");
73+
};
74+
75+
await Assert.ThrowsAsync<System.Exception>(() => middleware.Invoke(contextMock.Object, next));
76+
Assert.Null(Baggage.GetBaggage(baggageKey));
77+
}
78+
}
79+
80+
}

test/DotNetWorker.OpenTelemetry.Tests/EndToEndTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
using OpenTelemetry.Trace;
2323
using Xunit;
2424

25+
using CoreTraceConstants = Microsoft.Azure.Functions.Worker.Diagnostics.TraceConstants;
26+
2527
namespace Microsoft.Azure.Functions.Worker.OpenTelemetry.Tests;
2628

2729
public class EndToEndTests
@@ -89,7 +91,7 @@ public async Task ContextPropagation()
8991
Assert.Equal(activity.TraceId, activityContext.TraceId);
9092
Assert.Equal(activity.TraceStateString, activityContext.TraceState);
9193
Assert.Equal(ActivityKind.Internal, activity.Kind);
92-
Assert.Contains(activity.Tags, t => t.Key == TraceConstants.OTelAttributes_1_37_0.InvocationId && t.Value == context.InvocationId);
94+
Assert.Contains(activity.Tags, t => t.Key == CoreTraceConstants.OTelAttributes_1_37_0.InvocationId && t.Value == context.InvocationId);
9395
}
9496
else
9597
{
@@ -112,7 +114,7 @@ public async Task ContextPropagationV17()
112114
Assert.Equal(activity.TraceId, activityContext.TraceId);
113115
Assert.Equal(activity.TraceStateString, activityContext.TraceState);
114116
Assert.Equal(ActivityKind.Server, activity.Kind);
115-
Assert.Contains(activity.Tags, t => t.Key == TraceConstants.OTelAttributes_1_17_0.InvocationId && t.Value == context.InvocationId);
117+
Assert.Contains(activity.Tags, t => t.Key == CoreTraceConstants.OTelAttributes_1_17_0.InvocationId && t.Value == context.InvocationId);
116118
}
117119
else
118120
{

test/DotNetWorker.Tests/Handlers/InvocationHandlerTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
@@ -15,6 +15,8 @@
1515
using Moq;
1616
using Xunit;
1717

18+
using CoreTraceConstants = Microsoft.Azure.Functions.Worker.Diagnostics.TraceConstants;
19+
1820
namespace Microsoft.Azure.Functions.Worker.Tests
1921
{
2022
public class InvocationHandlerTests
@@ -261,6 +263,32 @@ public async Task SetRetryContextToNull()
261263
Assert.Null(_context.RetryContext);
262264
}
263265

266+
[Fact]
267+
public async Task InvokeAsync_SetsBaggage_WhenPresentInInvocationRequest()
268+
{
269+
var invocationId = Guid.NewGuid().ToString();
270+
var request = TestUtility.CreateInvocationRequest(invocationId);
271+
272+
var testKey = "testKey";
273+
var testValue = "testValue";
274+
request.TraceContext.Baggage[testKey] = testValue;
275+
276+
// Setup to verify baggage is set in context.Items during invocation
277+
_mockApplication
278+
.Setup(m => m.InvokeFunctionAsync(It.IsAny<FunctionContext>()))
279+
.Returns(Task.CompletedTask)
280+
.Callback<FunctionContext>(ctx =>
281+
{
282+
Assert.True(ctx.Items.ContainsKey(CoreTraceConstants.InternalKeys.BaggageKeyName));
283+
var actualBaggage = ctx.Items[CoreTraceConstants.InternalKeys.BaggageKeyName] as System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>;
284+
Assert.NotNull(actualBaggage);
285+
Assert.Contains(actualBaggage, kv => kv.Key == testKey && kv.Value == testValue);
286+
});
287+
288+
var handler = CreateInvocationHandler();
289+
await handler.InvokeAsync(request);
290+
}
291+
264292
private InvocationHandler CreateInvocationHandler(IFunctionsApplication application = null,
265293
IOptions<WorkerOptions> workerOptions = null)
266294
{
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System.Runtime.CompilerServices;
@@ -7,5 +7,4 @@
77
[assembly: CollectionBehavior(DisableTestParallelization = true)]
88
[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.Extensions.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]
99
[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]
10-
11-
10+
[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.OpenTelemetry.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]

0 commit comments

Comments
 (0)