-
Notifications
You must be signed in to change notification settings - Fork 10
/
StubComponent.cs
84 lines (75 loc) · 2.53 KB
/
StubComponent.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
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Sixeyed.Heartbeat.Extensions;
using Sixeyed.Heartbeat.TestFramework;
namespace Sixeyed.Heartbeat.Tests.Scenarios.Stubs
{
public class StubComponent : IDisposable
{
private Heartbeat _heartbeat;
public void Process()
{
long countInterval = 7000;
long countTo = RandomValueGenerator.GetRandomInt(20000, 50000);
double timerInterval = 300; //0.3 seconds
_heartbeat = new Heartbeat(this, countInterval, timerInterval);
_heartbeat.Start("StubComponent.Process started, countInterval: {0}, countTo: {1}, timerInterval: {2}"
.FormatWith(countInterval, countTo, timerInterval));
var tasks = new Task[countTo];
long finalTaskIndex = countTo - 1;
for (long i = 0; i < countTo; i++)
{
var heartbeat = _heartbeat; //don't pass the instance directly
var taskIndex = i;
tasks[i] = Task.Factory.StartNew(() => DoWork(heartbeat, taskIndex, finalTaskIndex));
}
}
private void DoWork(Heartbeat heartbeat, long taskIndex, long finalTaskIndex)
{
if (heartbeat == null)
Trace.WriteLine("Heartbeat is null");
heartbeat.IncrementCount();
//fake work:
if (RandomValueGenerator.GetRandomBool())
{
Thread.Sleep(RandomValueGenerator.GetRandomInt(100));
}
if (taskIndex == finalTaskIndex)
{
heartbeat.SetComplete("StubComponent.Process finished");
}
}
#region IDisposable
private bool _disposed;
/// <summary>
/// Cleans up resources
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Cleans up resources
/// </summary>
/// <param name="disposing">Flag if being disposed</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_heartbeat != null)
{
_heartbeat.Dispose();
_heartbeat = null;
}
_disposed = true;
}
}
}
#endregion
}
}