-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathRollbarLocatorFixture.cs
94 lines (77 loc) · 2.79 KB
/
RollbarLocatorFixture.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
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace UnitTest.Rollbar
{
using global::Rollbar;
using global::Rollbar.Infrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
[TestClass]
[TestCategory(nameof(RollbarLocatorFixture))]
public class RollbarLocatorFixture
{
[TestInitialize]
public void SetupFixture()
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
}
[TestCleanup]
public void TearDownFixture()
{
}
public static void ProducesValidInstance()
{
var rollbarLogger = RollbarLocator.RollbarInstance as RollbarLogger;
Assert.IsNotNull(rollbarLogger);
}
[TestMethod]
public void LocatesTheSameInstance()
{
Assert.AreSame(RollbarLocator.RollbarInstance, RollbarLocator.RollbarInstance);
}
[TestMethod]
[Timeout(10000)]
public void LocatesTheSameInstanceInMultithreadedEnvironment()
{
const int maxIterations = 100;
const int maxTasks = 100;
byte[] delays = new byte[maxTasks];
var randomGenerator = new Random();
int iteration = 0;
while (iteration++ < maxIterations)
{
randomGenerator.NextBytes(delays);
List<Task<IRollbar>> tasks = new List<Task<IRollbar>>(maxTasks);
int i = 0;
while (i < maxTasks)
{
Trace.WriteLine("Task #" + i);
tasks.Add(
Task.Factory.StartNew<IRollbar>((object state) =>
{
Trace.WriteLine("Starting thread: " + Thread.CurrentThread.ManagedThreadId);
byte delay = (byte)state;
Trace.WriteLine("Delay" + delay);
Thread.Sleep(TimeSpan.FromTicks(delay));
var rollbar = RollbarLocator.RollbarInstance;
Trace.WriteLine("Ending thread: " + Thread.CurrentThread.ManagedThreadId);
return rollbar;
}
, delays[i]
)
);
i++;
}
Task.WaitAll(tasks.ToArray());
foreach(var task in tasks)
{
Assert.AreEqual(tasks.First().Result, task.Result);
}
}
}
}
}