Skip to content

Commit b8dc00e

Browse files
msohailhussainMichael Ng
authored andcommitted
feat(eventprocess): Integrate Event Processor with Optimizely (#191)
1 parent 5fefbe8 commit b8dc00e

17 files changed

+419
-247
lines changed

OptimizelySDK.Net35/OptimizelySDK.Net35.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
</Compile>
133133
<Compile Include="..\OptimizelySDK\Event\LogEvent.cs">
134134
<Link>Event\LogEvent.cs</Link>
135+
</Compile>
136+
<Compile Include="..\OptimizelySDK\Event\ForwardingEventProcessor.cs">
137+
<Link>Event\ForwardingEventProcessor.cs</Link>
135138
</Compile>
136139
<Compile Include="..\OptimizelySDK\Exceptions\OptimizelyException.cs">
137140
<Link>Exceptions\OptimizelyException.cs</Link>

OptimizelySDK.Net40/OptimizelySDK.Net40.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@
282282
<Compile Include="..\OptimizelySDK\Event\EventProcessor.cs">
283283
<Link>Event\EventProcessor.cs</Link>
284284
</Compile>
285+
<Compile Include="..\OptimizelySDK\Event\ForwardingEventProcessor.cs">
286+
<Link>Event\ForwardingEventProcessor.cs</Link>
287+
</Compile>
285288
</ItemGroup>
286289
<ItemGroup>
287290
<None Include="..\OptimizelySDK\Utils\schema.json">

OptimizelySDK.NetStandard16/OptimizelySDK.NetStandard16.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="..\OptimizelySDK\Event\Dispatcher\HttpClientEventDispatcher45.cs" />
4949
<Compile Include="..\OptimizelySDK\Event\Dispatcher\IEventDispatcher.cs" />
5050
<Compile Include="..\OptimizelySDK\Event\LogEvent.cs" />
51+
<Compile Include="..\OptimizelySDK\Event\ForwardingEventProcessor.cs" />
5152
<Compile Include="..\OptimizelySDK\Exceptions\OptimizelyException.cs" />
5253
<Compile Include="..\OptimizelySDK\IOptimizely.cs" />
5354
<Compile Include="..\OptimizelySDK\Logger\DefaultLogger.cs" />

OptimizelySDK.NetStandard20/OptimizelySDK.NetStandard20.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@
238238
<Compile Include="..\OptimizelySDK\Utils\Validator.cs">
239239
<Link>Utils\Validator.cs</Link>
240240
</Compile>
241+
<Compile Include="..\OptimizelySDK\Event\BatchEventProcessor.cs">
242+
<Link>Event\BatchEventProcessor.cs</Link>
243+
</Compile>
244+
<Compile Include="..\OptimizelySDK\Event\ForwardingEventProcessor.cs">
245+
<Link>Event\ForwardingEventProcessor.cs</Link>
246+
</Compile>
247+
<Compile Include="..\OptimizelySDK\Event\EventProcessor.cs">
248+
<Link>Event\EventProcessor.cs</Link>
249+
</Compile>
241250
</ItemGroup>
242251
<ItemGroup>
243252
<None Include="..\OptimizelySDK\Event\.DS_Store">

OptimizelySDK.Tests/EventTests/BatchEventProcessorTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,52 @@ public void TestStopAndStart()
211211
Assert.True(countdownEvent.Wait(TimeSpan.FromMilliseconds(MAX_DURATION_MS * 3)), "Exceeded timeout waiting for notification.");
212212
}
213213

214+
[Test]
215+
public void TestDispose()
216+
{
217+
var countdownEvent = new CountdownEvent(2);
218+
var eventDispatcher = new TestEventDispatcher(countdownEvent);
219+
SetEventProcessor(eventDispatcher);
220+
221+
Assert.IsTrue(EventProcessor.IsStarted);
222+
223+
UserEvent userEvent = BuildConversionEvent(EventName);
224+
EventProcessor.Process(userEvent);
225+
eventDispatcher.ExpectConversion(EventName, TestUserId);
226+
227+
EventProcessor.Dispose();
228+
229+
Assert.True(eventDispatcher.CompareEvents());
230+
231+
// make sure, isStarted is false while dispose.
232+
Assert.False(EventProcessor.IsStarted);
233+
}
234+
235+
[Test]
236+
public void TestDisposeDontRaiseException()
237+
{
238+
var countdownEvent = new CountdownEvent(2);
239+
var eventDispatcher = new TestEventDispatcher(countdownEvent);
240+
SetEventProcessor(eventDispatcher);
241+
242+
Assert.IsTrue(EventProcessor.IsStarted);
243+
244+
UserEvent userEvent = BuildConversionEvent(EventName);
245+
EventProcessor.Process(userEvent);
246+
eventDispatcher.ExpectConversion(EventName, TestUserId);
247+
248+
EventProcessor.Dispose();
249+
250+
Assert.True(eventDispatcher.CompareEvents());
251+
252+
// make sure, isStarted is false while dispose.
253+
Assert.False(EventProcessor.IsStarted);
254+
255+
// Need to make sure, after dispose, process shouldn't raise exception
256+
EventProcessor.Process(userEvent);
257+
258+
}
259+
214260
[Test]
215261
public void TestNotificationCenter()
216262
{

OptimizelySDK.Tests/EventTests/CanonicalEvent.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class CanonicalEvent
1111
private string VariationId;
1212
private string EventName;
1313
private string VisitorId;
14-
1514
private UserAttributes Attributes;
1615
private EventTags Tags;
1716

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using Moq;
3+
using NUnit.Framework;
4+
using OptimizelySDK.Config;
5+
using OptimizelySDK.Entity;
6+
using OptimizelySDK.ErrorHandler;
7+
using OptimizelySDK.Event;
8+
using OptimizelySDK.Event.Entity;
9+
using OptimizelySDK.Logger;
10+
using OptimizelySDK.Notifications;
11+
12+
namespace OptimizelySDK.Tests.EventTests
13+
{
14+
[TestFixture]
15+
class ForwardingEventProcessorTest
16+
{
17+
private const string UserId = "userId";
18+
private const string EventName = "purchase";
19+
20+
private ForwardingEventProcessor EventProcessor;
21+
private TestForwardingEventDispatcher EventDispatcher;
22+
private NotificationCenter NotificationCenter = new NotificationCenter();
23+
24+
Mock<ILogger> LoggerMock;
25+
Mock<IErrorHandler> ErrorHandlerMock;
26+
27+
ProjectConfig ProjectConfig;
28+
29+
[SetUp]
30+
public void Setup()
31+
{
32+
LoggerMock = new Mock<ILogger>();
33+
LoggerMock.Setup(l => l.Log(It.IsAny<LogLevel>(), It.IsAny<string>()));
34+
35+
ErrorHandlerMock = new Mock<IErrorHandler>();
36+
ErrorHandlerMock.Setup(e => e.HandleError(It.IsAny<Exception>()));
37+
38+
ProjectConfig = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object, new NoOpErrorHandler());
39+
40+
EventDispatcher = new TestForwardingEventDispatcher { IsUpdated = false };
41+
EventProcessor = new ForwardingEventProcessor(EventDispatcher, NotificationCenter, LoggerMock.Object, ErrorHandlerMock.Object);
42+
}
43+
44+
[Test]
45+
public void TestEventHandlerWithConversionEvent()
46+
{
47+
var userEvent = CreateConversionEvent(EventName);
48+
EventProcessor.Process(userEvent);
49+
50+
Assert.True(EventDispatcher.IsUpdated);
51+
}
52+
53+
54+
[Test]
55+
public void TestExceptionWhileDispatching()
56+
{
57+
var eventProcessor = new ForwardingEventProcessor(new InvalidEventDispatcher(), NotificationCenter, LoggerMock.Object, ErrorHandlerMock.Object);
58+
var userEvent = CreateConversionEvent(EventName);
59+
60+
eventProcessor.Process(userEvent);
61+
62+
ErrorHandlerMock.Verify(errorHandler => errorHandler.HandleError(It.IsAny<Exception>()), Times.Once );
63+
}
64+
65+
[Test]
66+
public void TestNotifications()
67+
{
68+
bool notificationTriggered = false;
69+
NotificationCenter.AddNotification(NotificationCenter.NotificationType.LogEvent, logEvent => notificationTriggered = true);
70+
var userEvent = CreateConversionEvent(EventName);
71+
72+
EventProcessor.Process(userEvent);
73+
74+
Assert.True(notificationTriggered);
75+
}
76+
77+
78+
private ConversionEvent CreateConversionEvent(string eventName)
79+
{
80+
return UserEventFactory.CreateConversionEvent(ProjectConfig, eventName, UserId, new UserAttributes(), new EventTags());
81+
}
82+
}
83+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
using OptimizelySDK.Event;
3+
using OptimizelySDK.Event.Dispatcher;
4+
using OptimizelySDK.Logger;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace OptimizelySDK.Tests.EventTests
12+
{
13+
public class TestForwardingEventDispatcher : IEventDispatcher
14+
{
15+
public ILogger Logger { get; set; }
16+
public bool IsUpdated { get; set; } = false;
17+
18+
public void DispatchEvent(LogEvent logEvent)
19+
{
20+
Assert.AreEqual(logEvent.HttpVerb, "POST");
21+
Assert.AreEqual(logEvent.Url, EventFactory.EVENT_ENDPOINT);
22+
IsUpdated = true;
23+
}
24+
}
25+
}

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@
8080
<Compile Include="EventTests\BatchEventProcessorTest.cs" />
8181
<Compile Include="EventTests\DefaultEventDispatcherTest.cs" />
8282
<Compile Include="EventTests\EventBuilderTest.cs" />
83+
<Compile Include="EventTests\ForwardingEventProcessorTest.cs" />
8384
<Compile Include="EventTests\LogEventTest.cs" />
8485
<Compile Include="EventTests\TestEventDispatcher.cs" />
86+
<Compile Include="EventTests\TestForwardingEventDispatcher.cs" />
8587
<Compile Include="InvalidEventDispatcher.cs" />
8688
<Compile Include="NotificationTests\NotificationCenterTests.cs" />
8789
<Compile Include="OptimizelyTest.cs" />

0 commit comments

Comments
 (0)