Skip to content

Commit 6e76a73

Browse files
Add parallel identical query tests.
1 parent 72c2cb6 commit 6e76a73

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

samples/ImageSharp.Web.Sample/wwwroot/index.html

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html>
33
<head>
44
<meta charset="utf-8" />
@@ -171,6 +171,35 @@ <h2>Background Color</h2>
171171
</p>
172172
</div>
173173
</section>
174+
<hr />
175+
<section>
176+
<h2>Identical Queries</h2>
177+
<p>Demonstrates that the middleware handles identical queries without file contention.</p>
178+
<div>
179+
<p>
180+
<code>imagesharp-logo.png?width=123</code>
181+
</p>
182+
<p>
183+
<img src="imagesharp-logo.png?width=123" />
184+
</p>
185+
</div>
186+
<div>
187+
<p>
188+
<code>imagesharp-logo.png?width=123</code>
189+
</p>
190+
<p>
191+
<img src="imagesharp-logo.png?width=123" />
192+
</p>
193+
</div>
194+
<div>
195+
<p>
196+
<code>imagesharp-logo.png?width=123</code>
197+
</p>
198+
<p>
199+
<img src="imagesharp-logo.png?width=123" />
200+
</p>
201+
</div>
202+
</section>
174203
</main>
175204
</body>
176-
</html>
205+
</html>

src/ImageSharp.Web/FormatUtilities.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Runtime.CompilerServices;
78
using Microsoft.AspNetCore.WebUtilities;
89
using Microsoft.Extensions.Options;
910
using Microsoft.Extensions.Primitives;
@@ -46,6 +47,7 @@ public FormatUtilities(IOptions<ImageSharpMiddlewareOptions> options)
4647
/// </summary>
4748
/// <param name="uri">The full request uri.</param>
4849
/// <returns>The <see cref="string"/>.</returns>
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4951
public string GetExtensionFromUri(string uri)
5052
{
5153
// TODO: Investigate using span to reduce allocations here.
@@ -81,6 +83,7 @@ public string GetExtensionFromUri(string uri)
8183
/// </summary>
8284
/// <param name="contentType">The content type (mime-type).</param>
8385
/// <returns>The <see cref="string"/>.</returns>
86+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8487
public string GetExtensionFromContentType(string contentType) => this.fileExtension[contentType];
8588
}
8689
}

tests/ImageSharp.Web.Tests/Processing/AzureBlobStorageCacheServerTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Net;
78
using System.Net.Http;
89
using System.Threading.Tasks;
@@ -16,6 +17,7 @@ public class AzureBlobStorageCacheServerTests : ServerTestBase<AzureBlobStorageC
1617
{
1718
private const int Width = 20;
1819
private static readonly string Command = "?width=" + Width + "&v=" + Guid.NewGuid().ToString();
20+
private static readonly string Command2 = "?width=" + (Width + 1) + "&v=" + Guid.NewGuid().ToString();
1921

2022
public AzureBlobStorageCacheServerTests(AzureBlobStorageCacheTestServerFixture fixture)
2123
: base(fixture)
@@ -25,7 +27,7 @@ public AzureBlobStorageCacheServerTests(AzureBlobStorageCacheTestServerFixture f
2527
[Theory]
2628
[InlineData(TestConstants.PhysicalTestImage)]
2729
[InlineData(TestConstants.AzureTestImage)]
28-
public async Task CanProcessAndResolveImage(string url)
30+
public async Task CanProcessAndResolveImageAsync(string url)
2931
{
3032
string ext = Path.GetExtension(url);
3133
IImageFormat format = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension(ext);
@@ -97,5 +99,21 @@ public async Task CanProcessAndResolveImage(string url)
9799
request.Dispose();
98100
response.Dispose();
99101
}
102+
103+
[Theory]
104+
[InlineData(TestConstants.PhysicalTestImage)]
105+
[InlineData(TestConstants.AzureTestImage)]
106+
public async Task CanProcessMultipleIdenticalQueriesAsync(string url)
107+
{
108+
Task[] tasks = Enumerable.Range(0, 5).Select(_ => Task.Run(async () =>
109+
{
110+
using HttpResponseMessage response = await this.HttpClient.GetAsync(url + Command2);
111+
Assert.NotNull(response);
112+
})).ToArray();
113+
114+
var all = Task.WhenAll(tasks);
115+
await all;
116+
Assert.True(all.IsCompletedSuccessfully);
117+
}
100118
}
101119
}

tests/ImageSharp.Web.Tests/Processing/PhysicalFileSystemCacheServerTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Net;
78
using System.Net.Http;
89
using System.Threading.Tasks;
@@ -16,6 +17,7 @@ public class PhysicalFileSystemCacheServerTests : ServerTestBase<PhysicalFileSys
1617
{
1718
private const int Width = 20;
1819
private static readonly string Command = "?width=" + Width + "&v=" + Guid.NewGuid().ToString();
20+
private static readonly string Command2 = "?width=" + (Width + 1) + "&v=" + Guid.NewGuid().ToString();
1921

2022
public PhysicalFileSystemCacheServerTests(PhysicalFileSystemCacheTestServerFixture fixture)
2123
: base(fixture)
@@ -25,7 +27,7 @@ public PhysicalFileSystemCacheServerTests(PhysicalFileSystemCacheTestServerFixtu
2527
[Theory]
2628
[InlineData(TestConstants.PhysicalTestImage)]
2729
[InlineData(TestConstants.AzureTestImage)]
28-
public async Task CanProcessAndResolveImage(string url)
30+
public async Task CanProcessAndResolveImageAsync(string url)
2931
{
3032
string ext = Path.GetExtension(url);
3133
IImageFormat format = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension(ext);
@@ -97,5 +99,21 @@ public async Task CanProcessAndResolveImage(string url)
9799
request.Dispose();
98100
response.Dispose();
99101
}
102+
103+
[Theory]
104+
[InlineData(TestConstants.PhysicalTestImage)]
105+
[InlineData(TestConstants.AzureTestImage)]
106+
public async Task CanProcessMultipleIdenticalQueriesAsync(string url)
107+
{
108+
Task[] tasks = Enumerable.Range(0, 5).Select(_ => Task.Run(async () =>
109+
{
110+
using HttpResponseMessage response = await this.HttpClient.GetAsync(url + Command2);
111+
Assert.NotNull(response);
112+
})).ToArray();
113+
114+
var all = Task.WhenAll(tasks);
115+
await all;
116+
Assert.True(all.IsCompletedSuccessfully);
117+
}
100118
}
101119
}

0 commit comments

Comments
 (0)