forked from segmentio/Analytics.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlushTests.cs
More file actions
113 lines (88 loc) · 2.78 KB
/
FlushTests.cs
File metadata and controls
113 lines (88 loc) · 2.78 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Segment.Model;
namespace Segment.Test
{
[TestFixture()]
public class FlushTests
{
[SetUp]
public void Init()
{
Analytics.Dispose();
Logger.Handlers += LoggingHandler;
}
[Test()]
public void SynchronousFlushTestNet35()
{
Analytics.Initialize(Constants.WRITE_KEY, new Config().SetAsync(false));
Analytics.Client.Succeeded += Client_Succeeded;
Analytics.Client.Failed += Client_Failed;
int trials = 10;
RunTests(Analytics.Client, trials);
Assert.AreEqual(trials, Analytics.Client.Statistics.Submitted);
Assert.AreEqual(trials, Analytics.Client.Statistics.Succeeded);
Assert.AreEqual(0, Analytics.Client.Statistics.Failed);
}
[Test()]
public void AsynchronousFlushTestNet35()
{
Analytics.Initialize(Constants.WRITE_KEY, new Config().SetAsync(true));
Analytics.Client.Succeeded += Client_Succeeded;
Analytics.Client.Failed += Client_Failed;
int trials = 10;
RunTests(Analytics.Client, trials);
Thread.Sleep(1000); // cant use flush to wait during asynchronous flushing
Assert.AreEqual(trials, Analytics.Client.Statistics.Submitted);
Assert.AreEqual(trials, Analytics.Client.Statistics.Succeeded);
Assert.AreEqual(0, Analytics.Client.Statistics.Failed);
}
[Test()]
public void PerformanceTestNet35()
{
Analytics.Initialize(Constants.WRITE_KEY);
Analytics.Client.Succeeded += Client_Succeeded;
Analytics.Client.Failed += Client_Failed;
int trials = 100;
DateTime start = DateTime.Now;
RunTests(Analytics.Client, trials);
Analytics.Client.Flush();
TimeSpan duration = DateTime.Now.Subtract(start);
Assert.AreEqual(trials, Analytics.Client.Statistics.Submitted);
Assert.AreEqual(trials, Analytics.Client.Statistics.Succeeded);
Assert.AreEqual(0, Analytics.Client.Statistics.Failed);
Assert.IsTrue(duration.CompareTo(TimeSpan.FromSeconds(20)) < 0);
}
private void RunTests(Client client, int trials)
{
for (int i = 0; i < trials; i += 1)
{
Actions.Random(client);
}
}
void Client_Failed(BaseAction action, System.Exception e)
{
Console.WriteLine(String.Format("Action [{0}] {1} failed : {2}",
action.MessageId, action.Type, e.Message));
}
void Client_Succeeded(BaseAction action)
{
Console.WriteLine(String.Format("Action [{0}] {1} succeeded.",
action.MessageId, action.Type));
}
static void LoggingHandler(Logger.Level level, string message, IDictionary<string, object> args)
{
if (args != null)
{
foreach (string key in args.Keys)
{
message += String.Format(" {0}: {1},", "" + key, "" + args[key]);
}
}
Console.WriteLine(String.Format("[FlushTests] [{0}] {1}", level, message));
}
}
}