-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathViewCountServiceTests.cs
More file actions
39 lines (32 loc) · 1.18 KB
/
ViewCountServiceTests.cs
File metadata and controls
39 lines (32 loc) · 1.18 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
using Microsoft.Extensions.Logging;
using Moq;
using OrchardCore.ContentManagement;
using OrchardCoreContrib.ViewCount.Models;
namespace OrchardCoreContrib.ViewCount.Services.Tests;
public class ViewCountServiceTests
{
[Fact]
public async Task ViewAsync_ConcurrentCalls_WithSemaphore_AllIncrementsAreApplied()
{
// Arrange
const int concurrentCalls = 20;
var contentItem = new ContentItem();
contentItem.Weld(new ViewCountPart { Count = 0 });
var contentManagerMock = new Mock<IContentManager>();
contentManagerMock
.Setup(m => m.UpdateAsync(It.IsAny<ContentItem>()))
.Returns(async () => await Task.Delay(10).ConfigureAwait(false));
var service = new ViewCountService(
contentManagerMock.Object,
[],
Mock.Of<ILogger<ViewCountService>>());
// Act
var tasks = Enumerable.Range(0, concurrentCalls)
.Select(_ => Task.Run(() => service.ViewAsync(contentItem)))
.ToArray();
await Task.WhenAll(tasks);
// Assert
var count = contentItem.As<ViewCountPart>().Count;
Assert.Equal(concurrentCalls, count);
}
}