Projekt

Obecné

Profil

Stáhnout (4.3 KB) Statistiky
| Větev: | Tag: | Revize:
1 e979e18d Pultak
using System.Threading;
2
using System.Threading.Tasks;
3
using LDClient.detection;
4
using LDClient.network;
5
using LDClient.network.data;
6
using Moq;
7
using NUnit.Framework;
8
9
namespace LDClientTests.detection; 
10
11
public class ProcessDetectionTests {
12
13
    private ProcessDetection _processDetection;
14
15
16
    private Mock<IApiClient> _mockApiClient;
17
    private Mock<IInfoFetcher> _mockInfoFetcher;
18
    private Mock<IProcessUtils> _mockProcessUtils;
19
20
    private readonly string _defaultSerialNumber = "C12345678912";
21
        
22
23
    [SetUp]
24
    public void Setup() {
25
        _mockApiClient = new Mock<IApiClient>(MockBehavior.Strict);
26
        _mockApiClient.Setup(x => x.SendPayloadAsync(It.IsAny<Payload>())).Returns(Task.CompletedTask);
27
28
29
        _mockInfoFetcher = new Mock<IInfoFetcher>(MockBehavior.Strict);
30
        _mockInfoFetcher.Setup(x => x.BodySerialNumber).Returns(_defaultSerialNumber);
31
        _mockInfoFetcher.Setup(x => x.HeadSerialNumber).Returns(_defaultSerialNumber);
32
33
        _mockProcessUtils = new Mock<IProcessUtils>(MockBehavior.Strict);
34
            
35
36
        _processDetection = new ProcessDetection("process", 50, _mockInfoFetcher.Object, _mockApiClient.Object, _mockProcessUtils.Object);
37
    }
38
39
40
    private void StartAndStopDetection(int timeout) {
41
        var detectionThread = new Thread(_processDetection.RunPeriodicDetection);
42
43
        detectionThread.Start();
44
45
        Thread.Sleep(timeout);
46
47
        _processDetection.DetectionRunning = false;
48
        detectionThread.Join();
49
    }
50
51
    [Test]
52
    [Timeout(1000)]
53
    public void RunPeriodicDetection_ProcessStartFetchFailed_1Fetch0PayloadSent() {
54
        _mockInfoFetcher.Setup(x => x.FetchDataAsync()).Returns(Task.FromResult(false));
55
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(true);
56
57
58
        StartAndStopDetection(500);
59
60
        _mockApiClient.Verify(x => x.SendPayloadAsync(It.IsAny<Payload>()), Times.Never);
61
        _mockInfoFetcher.Verify(x => x.FetchDataAsync(), Times.Once);
62
    }
63
64
    [Test]
65
    [Timeout(1000)]
66
    public void RunPeriodicDetection_ProcessStartFetchSuccess_1Fetch1PayloadSent() {
67
        _mockInfoFetcher.Setup(x => x.FetchDataAsync()).Returns(Task.FromResult(true));
68
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(true);
69
70
71
        StartAndStopDetection(500);
72
73
        _mockApiClient.Verify(x => x.SendPayloadAsync(It.IsAny<Payload>()), Times.Once);
74
        _mockInfoFetcher.Verify(x => x.FetchDataAsync(), Times.Once);
75
    }
76
77
78
79
    [Test]
80
    [Timeout(1000)]
81
    public void RunPeriodicDetection_ProcessStartAndStopped_1Fetch2PayloadSent() {
82
        _mockInfoFetcher.Setup(x => x.FetchDataAsync()).Returns(Task.FromResult(true));
83
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(true);
84
85
        var detectionThread = new Thread(_processDetection.RunPeriodicDetection);
86
87
        detectionThread.Start();
88
89
        Thread.Sleep(250);
90
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(false);
91
92
        Thread.Sleep(250);
93
94
        _processDetection.DetectionRunning = false;
95
        detectionThread.Join();
96
97
        _mockApiClient.Verify(x => x.SendPayloadAsync(It.IsAny<Payload>()), Times.Exactly(2));
98
        _mockInfoFetcher.Verify(x => x.FetchDataAsync(), Times.Once);
99
    }
100
101
102
103
    [Test]
104
    [Timeout(2000)]
105
    public void RunPeriodicDetection_2xProcessStartAndStopped_2Fetch4PayloadSent() {
106
        _mockInfoFetcher.Setup(x => x.FetchDataAsync()).Returns(Task.FromResult(true));
107
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(true);
108
109
        var detectionThread = new Thread(_processDetection.RunPeriodicDetection);
110
111
        detectionThread.Start();
112
113
        Thread.Sleep(200);
114
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(false);
115
116
        Thread.Sleep(200);
117
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(true);
118
119
120
        Thread.Sleep(200);
121
        _mockProcessUtils.Setup(x => x.IsProcessRunning(It.IsAny<string>())).Returns(false);
122
        Thread.Sleep(200);
123
124
        _processDetection.DetectionRunning = false;
125
        detectionThread.Join();
126
127
        _mockApiClient.Verify(x => x.SendPayloadAsync(It.IsAny<Payload>()), Times.Exactly(4));
128
        _mockInfoFetcher.Verify(x => x.FetchDataAsync(), Times.Exactly(2));
129
    }
130
131
132
}