Repository navigation
Expand file tree
/
Copy pathFunctionLifecycleTest.cs
More file actions
256 lines (235 loc) · 11.1 KB
/
Copy pathFunctionLifecycleTest.cs
File metadata and controls
256 lines (235 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using UnitTestEx.IsolatedFunction;
namespace UnitTestEx.NUnit.Test
{
[TestFixture]
[NonParallelizable]
public class FunctionLifecycleTest
{
[TestCase("generic")]
[TestCase("http")]
[TestCase("servicebus")]
public void TriggerScopes_AreOwnedUntilResetOrDisposal(string kind)
{
var resources = new List<ScopedResource>();
using var test = FunctionTester.Create<HttpFunction>()
.UseWorkerMiddleware(false)
.ConfigureServices(sc => sc.AddScoped(_ =>
{
var resource = new ScopedResource();
resources.Add(resource);
return resource;
}));
void Run()
{
if (kind == "generic")
test.Trigger<ScopedFunction>().Run(f => f.Generic(null!)).AssertValue("alive");
else if (kind == "http")
test.HttpTrigger<ScopedFunction>().Run(f => f.Http(test.CreateHttpRequest(HttpMethod.Get, "scope"))).AssertOK();
else
test.ServiceBusTrigger<ScopedFunction>().Run(f => f.ServiceBus(null!)).AssertSuccess();
}
Run();
Run();
Assert.That(resources, Has.Count.EqualTo(2));
Assert.That(resources.TrueForAll(r => r.DisposeCount == 0), Is.True);
test.ResetHost();
Assert.That(resources.TrueForAll(r => r.DisposeCount == 1), Is.True);
Run();
Assert.That(resources, Has.Count.EqualTo(3));
Assert.That(resources[2].DisposeCount, Is.Zero);
test.Dispose();
test.Dispose();
Assert.That(resources.TrueForAll(r => r.DisposeCount == 1), Is.True);
Assert.Throws<ObjectDisposedException>(() => test.Trigger<ScopedFunction>());
}
[Test]
public void TriggerScope_RemainsUsableBetweenInvocations()
{
using var test = FunctionTester.Create<HttpFunction>().UseWorkerMiddleware(false)
.ConfigureServices(sc => sc.AddScoped<ScopedResource>());
var trigger = test.Trigger<ScopedFunction>();
trigger.Run(f => f.Generic(null!)).AssertValue("alive");
trigger.Run(f => f.Generic(null!)).AssertValue("alive");
Assert.That(trigger.Services.GetRequiredService<ScopedResource>().DisposeCount, Is.Zero);
test.ResetHost();
Assert.Throws<ObjectDisposedException>(() => trigger.Services.GetRequiredService<ScopedResource>());
}
[TestCase(false)]
[TestCase(true)]
public void Cleanup_AttemptsOtherScopesAndHostWhenScopeThrows(bool reset)
{
var disposed = new List<string>();
var test = FunctionTester.Create<HttpFunction>()
.ConfigureServices(sc =>
{
sc.AddScoped(_ => new ThrowingResource(disposed));
sc.AddKeyedScoped("scope", (_, _) => new RecordingResource(disposed, "scope"));
sc.AddSingleton(_ => new RecordingResource(disposed, "host"));
});
test.Services.GetRequiredService<RecordingResource>();
test.Trigger<ScopedFunction>().Services.GetRequiredKeyedService<RecordingResource>("scope");
test.Trigger<ScopedFunction>().Services.GetRequiredService<ThrowingResource>();
var exception = Assert.Throws<InvalidOperationException>(() =>
{
if (reset)
test.ResetHost();
else
test.Dispose();
});
Assert.That(exception!.Message, Is.EqualTo("scope disposal failed"));
Assert.That(disposed, Is.EqualTo(new[] { "throwing", "scope", "host" }));
test.Dispose();
Assert.That(disposed, Has.Count.EqualTo(3));
}
[Test]
public void Cleanup_SupportsAsyncOnlyScopedResources()
{
using var test = FunctionTester.Create<HttpFunction>().ConfigureServices(sc => sc.AddScoped<AsyncResource>());
var resource = test.Trigger<ScopedFunction>().Services.GetRequiredService<AsyncResource>();
test.Dispose();
Assert.That(resource.DisposeCount, Is.EqualTo(1));
}
[Test]
public void Cleanup_ReportsAllScopeFailures()
{
var disposed = new List<string>();
var test = FunctionTester.Create<HttpFunction>()
.ConfigureServices(sc => sc.AddScoped(_ => new ThrowingResource(disposed)));
test.Trigger<ScopedFunction>().Services.GetRequiredService<ThrowingResource>();
test.Trigger<ScopedFunction>().Services.GetRequiredService<ThrowingResource>();
var exception = Assert.Throws<AggregateException>(() => test.Dispose());
Assert.That(exception!.InnerExceptions, Has.Count.EqualTo(2));
Assert.That(disposed, Has.Count.EqualTo(2));
test.Dispose();
}
[TestCase(null)]
[TestCase("original-environment")]
public void HostConstruction_RestoresEnvironmentImmediatelyAndAfterReset(string previous)
{
var original = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");
try
{
Environment.SetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT", previous);
using var test = FunctionTester.Create<HttpFunction>();
Assert.That(test.Services.GetRequiredService<IHostEnvironment>().EnvironmentName, Is.EqualTo(TestSetUp.Environment));
Assert.That(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), Is.EqualTo(previous));
test.ResetHost();
Assert.That(test.Services.GetRequiredService<IHostEnvironment>().EnvironmentName, Is.EqualTo(TestSetUp.Environment));
Assert.That(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), Is.EqualTo(previous));
test.Dispose();
Assert.That(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), Is.EqualTo(previous));
}
finally
{
Environment.SetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT", original);
}
}
[Test]
public void CreateHttpRequest_UsesUtf8ContentLength()
{
using var test = FunctionTester.Create<HttpFunction>();
var request = test.CreateHttpRequest(HttpMethod.Post, "unicode", "café");
Assert.That(request.ContentLength, Is.EqualTo(System.Text.Encoding.UTF8.GetByteCount("café")));
}
[Test]
public void FailedConstruction_RestoresEnvironment()
{
var original = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");
try
{
Environment.SetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT", "original-environment");
using var test = FunctionTester.Create<HttpFunction>()
.ConfigureServices(_ => throw new InvalidOperationException("construction failed"));
Assert.Throws<InvalidOperationException>(() => _ = test.Services);
Assert.That(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), Is.EqualTo("original-environment"));
}
finally
{
Environment.SetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT", original);
}
}
[Test]
public async Task ConcurrentGenericTypes_SerializeHostConstruction()
{
var active = 0;
var maximum = 0;
void Configure(IServiceCollection services)
{
var count = Interlocked.Increment(ref active);
maximum = Math.Max(maximum, count);
try
{
Assert.That(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), Is.EqualTo(TestSetUp.Environment));
Thread.Sleep(100);
}
finally
{
Interlocked.Decrement(ref active);
}
}
using var first = FunctionTester.Create<HttpFunction>().ConfigureServices(Configure);
using var second = FunctionTester.Create<HttpVariantFunctions>().ConfigureServices(Configure);
using var start = new ManualResetEventSlim();
var one = Task.Run(() => { start.Wait(); return first.Services.GetRequiredService<IHostEnvironment>().EnvironmentName; });
var two = Task.Run(() => { start.Wait(); return second.Services.GetRequiredService<IHostEnvironment>().EnvironmentName; });
var original = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");
start.Set();
Assert.That(await Task.WhenAll(one, two), Is.All.EqualTo(TestSetUp.Environment));
Assert.That(maximum, Is.EqualTo(1));
Assert.That(Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT"), Is.EqualTo(original));
}
public sealed class ScopedResource : IDisposable
{
public int DisposeCount { get; private set; }
public void Dispose() => DisposeCount++;
}
public sealed class AsyncResource : IAsyncDisposable
{
public int DisposeCount { get; private set; }
public async ValueTask DisposeAsync()
{
await Task.Yield();
DisposeCount++;
}
}
public sealed class RecordingResource(List<string> disposed, string name) : IDisposable
{
public void Dispose() => disposed.Add(name);
}
public sealed class ThrowingResource(List<string> disposed) : IDisposable
{
public void Dispose()
{
disposed.Add("throwing");
throw new InvalidOperationException("scope disposal failed");
}
}
public sealed class ScopedFunction(ScopedResource resource)
{
[Function("ScopedGeneric")]
public string Generic([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest request)
=> resource.DisposeCount == 0 ? "alive" : throw new ObjectDisposedException(nameof(resource));
[Function("ScopedHttp")]
public IActionResult Http([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "scope")] HttpRequest request)
=> new OkObjectResult(Generic(request));
[Function("ScopedServiceBus")]
public Task ServiceBus([ServiceBusTrigger("scope")] string message)
{
Generic(null!);
return Task.CompletedTask;
}
}
}
}