Skip to content

Commit c49e858

Browse files
added disposed and max wait time. (#231)
1 parent f180b62 commit c49e858

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ public void TestOnReadyPromiseResolvedImmediatelyWhenDatafileIsProvided()
182182
.WithStartByDefault()
183183
.Build();
184184

185-
httpManager.OnReady().Wait();
185+
// OnReady waits until is resolved, need to add time in case of deadlock.
186+
httpManager.OnReady().Wait(10000);
186187

187188
Assert.AreEqual("15", httpManager.GetConfig().Revision);
188189

@@ -208,7 +209,9 @@ public void TestOnReadyPromiseWaitsForProjectConfigRetrievalWhenDatafileIsNotPro
208209
.WithStartByDefault(true)
209210
.Build();
210211
t.Wait();
211-
httpManager.OnReady().Wait();
212+
213+
// OnReady waits until is resolved, need to add time in case of deadlock.
214+
httpManager.OnReady().Wait(10000);
212215
Assert.NotNull(httpManager.GetConfig());
213216
httpManager.Dispose();
214217
}
@@ -244,7 +247,7 @@ public void TestHttpConfigManagerDoesNotWaitForTheConfigWhenDeferIsTrue()
244247
#region Notification
245248
[Test]
246249
public void TestHttpConfigManagerSendConfigUpdateNotificationWhenProjectConfigGetsUpdated()
247-
{
250+
{
248251
var t = MockSendAsync(TestData.Datafile);
249252

250253
var httpManager = new HttpProjectConfigManager.Builder()
@@ -266,7 +269,7 @@ public void TestHttpConfigManagerSendConfigUpdateNotificationWhenProjectConfigGe
266269

267270
[Test]
268271
public void TestHttpConfigManagerDoesNotSendConfigUpdateNotificationWhenDatafileIsProvided()
269-
{
272+
{
270273
var t = MockSendAsync(TestData.Datafile, TimeSpan.FromMilliseconds(100));
271274

272275
var httpManager = new HttpProjectConfigManager.Builder()
@@ -460,7 +463,6 @@ public void TestAuthenticationHeaderWhenTokenProvided()
460463
[Test]
461464
public void TestFormatUrlHigherPriorityThanDefaultUrl()
462465
{
463-
464466
var t = MockSendAsync();
465467
var httpManager = new HttpProjectConfigManager.Builder()
466468
.WithSdkKey("QBw9gFM8oTn7ogY9ANCC1z")
@@ -483,7 +485,6 @@ public Task MockSendAsync(string datafile = null, TimeSpan? delay = null, HttpSt
483485
{
484486
return TestHttpProjectConfigManagerUtil.MockSendAsync(HttpClientMock, datafile, delay, statusCode);
485487
}
486-
487488
#endregion
488489
}
489490
}

OptimizelySDK.Tests/ConfigTest/PollingProjectConfigManagerTest.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public class PollingProjectConfigManagerTest
3434

3535
[SetUp]
3636
public void Setup()
37-
{
37+
{
3838
LoggerMock = new Mock<ILogger>();
3939
LoggerMock.Setup(l => l.Log(It.IsAny<LogLevel>(), It.IsAny<string>()));
4040
ProjectConfig = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object, null);
4141
}
42-
42+
4343
[Test]
4444
public void TestPollingConfigManagerDoesNotBlockWhenProjectConfigIsAlreadyProvided()
4545
{
@@ -53,6 +53,7 @@ public void TestPollingConfigManagerDoesNotBlockWhenProjectConfigIsAlreadyProvid
5353

5454
Assert.True(stopwatch.Elapsed.Seconds == 0);
5555
Assert.NotNull(config);
56+
configManager.Dispose();
5657
}
5758

5859
[Test]
@@ -66,6 +67,7 @@ public void TestPollingConfigManagerBlocksWhenProjectConfigIsNotProvided()
6667
stopwatch.Stop();
6768

6869
Assert.True(stopwatch.Elapsed.TotalMilliseconds >= 500);
70+
configManager.Dispose();
6971
}
7072

7173
[Test]
@@ -86,6 +88,7 @@ public void TestImmediatelyCalledScheduledRequestIfPreviousRequestDelayedInRespo
8688
// Should be called immediately after 1200 seconds. Here checking after 1300 secs.
8789
//Thread.Sleep(200);
8890
Assert.AreEqual(2, configManager.Counter);
91+
configManager.Dispose();
8992

9093
}
9194

@@ -99,7 +102,7 @@ public void TestTimedoutIfTakingMorethanBlockingTimeout()
99102
configManager.Start();
100103
var config = configManager.GetConfig();
101104
LoggerMock.Verify(l => l.Log(LogLevel.WARN, "Timeout exceeded waiting for ProjectConfig to be set, returning null."));
102-
105+
configManager.Dispose();
103106
}
104107

105108
[Test]
@@ -113,6 +116,7 @@ public void TestTimedoutOnlyIfSchedulerStarted()
113116
var config = configManager.GetConfig();
114117
sw.Stop();
115118
Assert.GreaterOrEqual(sw.Elapsed.TotalMilliseconds, 950);
119+
configManager.Dispose();
116120
}
117121

118122
[Test]
@@ -126,6 +130,7 @@ public void TestDontTimedoutIfSchedulerNotStarted()
126130
var config = configManager.GetConfig();
127131
sw.Stop();
128132
Assert.GreaterOrEqual(sw.Elapsed.TotalMilliseconds, 1000);
133+
configManager.Dispose();
129134
}
130135

131136
[Test]
@@ -142,6 +147,7 @@ public void TestReturnDatafileImmediatelyOnceGetValidDatafileRemotely()
142147
var config = configManager.GetConfig();
143148
Assert.NotNull(config);
144149
Assert.AreEqual(1, configManager.Counter);
150+
configManager.Dispose();
145151
}
146152

147153
[Test]
@@ -168,6 +174,7 @@ public void TestWaitUntilValidDatfileIsNotGiven()
168174
var config = configManager.GetConfig();
169175
//Assert.NotNull(config);
170176
Assert.AreEqual(3, configManager.Counter);
177+
configManager.Dispose();
171178
}
172179

173180

@@ -186,6 +193,7 @@ public void TestWaitUntilValidDatafileIsNotGivenOrTimedout()
186193
var config = configManager.GetConfig();
187194
Assert.Null(config);
188195
Assert.AreEqual(3, configManager.Counter);
196+
configManager.Dispose();
189197
}
190198
}
191199
}

OptimizelySDK.Tests/OptimizelyTest.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3365,8 +3365,10 @@ public void TestDFMNotificationWhenProjectConfigIsUpdated()
33653365
var optimizely = new Optimizely(httpManager, notificationCenter);
33663366
optimizely.NotificationCenter.AddNotification(NotificationCenter.NotificationType.OptimizelyConfigUpdate, NotificationCallbackMock.Object.TestConfigUpdateCallback);
33673367
httpManager.Start();
3368+
3369+
// wait till 10 seconds max, to avoid stale state in worst case.
3370+
httpManager.OnReady().Wait(10000);
33683371

3369-
httpManager.OnReady().Wait(-1);
33703372
t.Wait();
33713373
NotificationCallbackMock.Verify(nc => nc.TestConfigUpdateCallback(), Times.Once);
33723374
httpManager.Dispose();
@@ -3389,7 +3391,9 @@ public void TestDFMWhenDatafileProvidedDoesNotNotifyWithoutStart()
33893391

33903392
var optimizely = new Optimizely(httpManager);
33913393
optimizely.NotificationCenter.AddNotification(NotificationCenter.NotificationType.OptimizelyConfigUpdate, NotificationCallbackMock.Object.TestConfigUpdateCallback);
3392-
httpManager.OnReady().Wait(-1);
3394+
3395+
// added 10 secs max wait to avoid stale state.
3396+
httpManager.OnReady().Wait(10000);
33933397

33943398
NotificationCallbackMock.Verify(nc => nc.TestConfigUpdateCallback(), Times.Never);
33953399
httpManager.Dispose();
@@ -3833,6 +3837,7 @@ public void TestAfterDisposeAPIsNoLongerValid()
38333837
var activateAfterDispose = optimizely.Activate("test_experiment", TestUserId, new UserAttributes() {
38343838
{ "device_type", "iPhone" }, { "location", "San Francisco" } });
38353839
Assert.Null(activateAfterDispose);
3840+
httpManager.Dispose();
38363841
}
38373842

38383843
[Test]

0 commit comments

Comments
 (0)