forked from Azure/azure-functions-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PingWorkerTests.cs
99 lines (85 loc) · 4.05 KB
/
PingWorkerTests.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Moq;
using Xunit;
namespace Microsoft.Azure.WebJobs.Script.Scaling.Tests
{
public class PingWorkerTests
{
[Fact]
public async Task ValidWorkerTests()
{
var activityId = Guid.NewGuid().ToString();
var workerInfo = new MockWorkerInfo();
// Test
using (var scaleManager = new MockScaleManager(MockBehavior.Strict))
{
// Setup
scaleManager.MockScaleHandler.Setup(h => h.PingWorker(activityId, workerInfo))
.Returns(Task.FromResult(true));
scaleManager.MockWorkerTable.Setup(t => t.AddOrUpdate(workerInfo))
.Returns(Task.CompletedTask);
scaleManager.MockScaleTracer.Setup(t => t.TraceUpdateWorker(activityId, workerInfo, It.Is<string>(s => s.Contains("updated"))));
// test
await scaleManager.MockPingWorker(activityId, workerInfo);
// assert
scaleManager.VerifyAll();
}
}
[Fact]
public async Task InvalidWorkerTests()
{
var activityId = Guid.NewGuid().ToString();
var workerInfo = new MockWorkerInfo();
// Test
using (var scaleManager = new MockScaleManager(MockBehavior.Strict))
{
// Setup
scaleManager.MockScaleHandler.Setup(h => h.PingWorker(activityId, workerInfo))
.Returns(Task.FromResult(false));
scaleManager.MockWorkerTable.Setup(t => t.Delete(workerInfo))
.Returns(Task.CompletedTask);
scaleManager.MockScaleTracer.Setup(t => t.TraceWarning(activityId, workerInfo, It.Is<string>(s => s.Contains("not belong"))));
scaleManager.MockScaleTracer.Setup(t => t.TraceRemoveWorker(activityId, workerInfo, It.Is<string>(s => s.Contains("removed"))));
// test
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () => await scaleManager.MockPingWorker(activityId, workerInfo));
// assert
Assert.Contains("not belong", exception.Message);
scaleManager.VerifyAll();
}
}
[Fact]
public async Task PingWorkerIntervalTests()
{
var activityId = Guid.NewGuid().ToString();
var workerInfo = new MockWorkerInfo();
var settings = new ScaleSettings
{
WorkerPingInterval = TimeSpan.FromMilliseconds(500)
};
using (var scaleManager = new MockScaleManager(MockBehavior.Strict, settings))
{
// Setup
scaleManager.MockScaleHandler.Setup(h => h.PingWorker(activityId, workerInfo))
.Returns(Task.FromResult(true));
scaleManager.MockWorkerTable.Setup(t => t.AddOrUpdate(workerInfo))
.Returns(Task.CompletedTask);
scaleManager.MockScaleTracer.Setup(t => t.TraceUpdateWorker(activityId, workerInfo, It.Is<string>(s => s.Contains("updated"))));
var loop = 10;
for (int i = 0; i < loop; ++i)
{
// test
await scaleManager.MockPingWorker(activityId, workerInfo);
await Task.Delay(100);
}
// assert
scaleManager.VerifyAll();
scaleManager.MockScaleHandler.Verify(h => h.PingWorker(activityId, workerInfo), Times.Between(2, loop - 1, Range.Inclusive));
scaleManager.MockWorkerTable.Verify(t => t.AddOrUpdate(workerInfo), Times.Exactly(loop));
scaleManager.MockScaleTracer.Verify(t => t.TraceUpdateWorker(activityId, workerInfo, It.Is<string>(s => s.Contains("updated"))), Times.Exactly(loop));
}
}
}
}