Permalink
Browse files

refactore core code to accomodate stubs, updated test project and add…

…ed connect callback feature
  • Loading branch information...
1 parent c2fc18f commit 8ddf9f642e2b670c1b48953dfd1bcf2c79547ac9 @budgetpreneur budgetpreneur committed Dec 20, 2012
@@ -3,6 +3,7 @@
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<appSettings>
<add key="OverrideTcpKeepAlive" value="false"/>
+ <add key="LogLevel" value="3"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
@@ -11,8 +12,5 @@
<remove name="Default"/>
</listeners>
</trace>
- <switches>
- <add name="PubnubTraceSwitch" value="1"/>
- </switches>
</system.diagnostics>
</configuration>
@@ -85,6 +85,7 @@
<ItemGroup>
<Compile Include="EncryptionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="PubnubUnitTest.cs" />
<Compile Include="WhenAClientIsPresented.cs" />
<Compile Include="WhenAMessageIsPublished.cs" />
<Compile Include="WhenDetailedHistoryIsRequested.cs" />

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -20,6 +20,7 @@ public class WhenAClientIsPresented
ManualResetEvent manualEvent3 = new ManualResetEvent(false);
ManualResetEvent manualEvent4 = new ManualResetEvent(false);
+ ManualResetEvent preUnsubEvent = new ManualResetEvent(false);
static bool receivedFlag1 = false;
static bool receivedFlag2 = false;
@@ -30,6 +31,12 @@ public void ThenPresenceShouldReturnReceivedMessage()
receivedFlag1 = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAClientIsPresented";
+ unitTest.TestCaseName = "ThenPresenceShouldReturnReceivedMessage";
+ pubnub.PubnubUnitTest = unitTest;
+
string channel = "my/channel";
pubnub.presence<string>(channel, ThenPresenceShouldReturnMessage);
@@ -42,6 +49,10 @@ public void ThenPresenceShouldReturnReceivedMessage()
manualEvent3.WaitOne(2000);
manualEvent2.WaitOne(310 * 1000);
+
+ pubnub.presence_unsubscribe<string>(channel, DummyMethodForPreUnSub);
+ preUnsubEvent.WaitOne();
+
Assert.IsTrue(receivedFlag1, "Presence message not received");
}
@@ -75,6 +86,10 @@ public void IfHereNowIsCalledThenItShouldReturnInfo()
receivedFlag2 = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAClientIsPresented";
+ unitTest.TestCaseName = "IfHereNowIsCalledThenItShouldReturnInfo";
+ pubnub.PubnubUnitTest = unitTest;
string channel = "my/channel";
pubnub.here_now<string>(channel, ThenHereNowShouldReturnMessage);
manualEvent4.WaitOne();
@@ -113,5 +128,10 @@ void DummyMethodForUnSubscribe(string receivedMessage)
manualEvent3.Set();
//Dummary callback method for unsubscribe to test presence
}
+
+ void DummyMethodForPreUnSub(string receivedMessage)
+ {
+ preUnsubEvent.Set();
+ }
}
}
@@ -59,6 +59,10 @@ public void ThenUnencryptPublishShouldReturnSuccessCodeAndInfo()
{
isUnencryptPublished = false;
Pubnub pubnub = new Pubnub("demo","demo","","",false);
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenUnencryptPublishShouldReturnSuccessCodeAndInfo";
+ pubnub.PubnubUnitTest = unitTest;
string channel = "my/channel";
string message = messageForUnencryptPublish;
@@ -82,6 +86,10 @@ public void ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo()
{
isUnencryptObjectPublished = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo";
+ pubnub.PubnubUnitTest = unitTest;
string channel = "my/channel";
object message = new CustomClass();
messageObjectForUnencryptPublish = JsonConvert.SerializeObject(message);
@@ -106,6 +114,13 @@ public void ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo()
{
isEncryptObjectPublished = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "enigma", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo";
+
+ pubnub.PubnubUnitTest = unitTest;
+
string channel = "my/channel";
object message = new SecretCustomClass();
messageObjectForEncryptPublish = JsonConvert.SerializeObject(message);
@@ -130,6 +145,13 @@ public void ThenEncryptPublishShouldReturnSuccessCodeAndInfo()
{
isEncryptPublished = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "enigma", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenEncryptPublishShouldReturnSuccessCodeAndInfo";
+
+ pubnub.PubnubUnitTest = unitTest;
+
string channel = "my/channel";
string message = messageForEncryptPublish;
@@ -153,6 +175,13 @@ public void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo()
{
isSecretEncryptPublished = false;
Pubnub pubnub = new Pubnub("demo", "demo", "key", "enigma", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo";
+
+ pubnub.PubnubUnitTest = unitTest;
+
string channel = "my/channel";
string message = messageForSecretEncryptPublish;
@@ -370,6 +399,13 @@ public void ThenPublishKeyShouldNotBeEmpty()
{
Pubnub pubnub = new Pubnub("", "demo", "", "", false);
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenPublishKeyShouldNotBeEmpty";
+
+ pubnub.PubnubUnitTest = unitTest;
+
+
string channel = "my/channel";
string message = "Pubnub API Usage Example";
@@ -382,6 +418,14 @@ public void ThenOptionalSecretKeyShouldBeProvidedInConstructor()
{
isPublished2 = false;
Pubnub pubnub = new Pubnub("demo","demo","key");
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "ThenOptionalSecretKeyShouldBeProvidedInConstructor";
+
+ pubnub.PubnubUnitTest = unitTest;
+
+
string channel = "my/channel";
string message = "Pubnub API Usage Example";
@@ -414,6 +458,14 @@ public void IfSSLNotProvidedThenDefaultShouldBeFalse()
{
isPublished3 = false;
Pubnub pubnub = new Pubnub("demo", "demo", "");
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenAMessageIsPublished";
+ unitTest.TestCaseName = "IfSSLNotProvidedThenDefaultShouldBeFalse";
+
+ pubnub.PubnubUnitTest = unitTest;
+
+
string channel = "my/channel";
string message = "Pubnub API Usage Example";
@@ -34,6 +34,13 @@ public void DetailHistoryCount10ReturnsRecords()
msg10Received = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenDetailedHistoryIsRequested";
+ unitTest.TestCaseName = "DetailHistoryCount10ReturnsRecords";
+
+ pubnub.PubnubUnitTest = unitTest;
+
string channel = "my/channel";
pubnub.detailedHistory<string>(channel, 10, DetailedHistoryCount10Callback);
@@ -68,6 +75,13 @@ public void DetailHistoryCount10ReverseTrueReturnsRecords()
msg10ReverseTrueReceived = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenDetailedHistoryIsRequested";
+ unitTest.TestCaseName = "DetailHistoryCount10ReverseTrueReturnsRecords";
+
+ pubnub.PubnubUnitTest = unitTest;
+
string channel = "my/channel";
pubnub.detailedHistory<string>(channel, -1, -1, 10, true, DetailedHistoryCount10ReverseTrueCallback);
@@ -102,17 +116,25 @@ public void DetailedHistoryStartWithReverseTrue()
expectedCountAtStartTimeWithReverseTrue = 0;
msgStartReverseTrue = false;
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenDetailedHistoryIsRequested";
+ unitTest.TestCaseName = "DetailedHistoryStartWithReverseTrue";
+
+ pubnub.PubnubUnitTest = unitTest;
+
+
string channel = "my/channel";
- startTimeWithReverseTrue = Pubnub.translateDateTimeToPubnubUnixNanoSeconds(DateTime.UtcNow);
+ startTimeWithReverseTrue = Pubnub.translateDateTimeToPubnubUnixNanoSeconds(new DateTime(2012,12,1));
for (int index = 0; index < 10; index++)
{
pubnub.publish<string>(channel,
- string.Format("DetailedHistoryStartTimeWithReverseTrue {0} {1}", startTimeWithReverseTrue, index),
+ string.Format("DetailedHistoryStartTimeWithReverseTrue {0}", index),
DetailedHistorySamplePublishCallback);
- mrePublishStartReverseTrue.WaitOne(5000);
+ mrePublishStartReverseTrue.WaitOne();
}
- Thread.Sleep(5000);
+ Thread.Sleep(2000);
pubnub.detailedHistory<string>(channel, startTimeWithReverseTrue, DetailedHistoryStartWithReverseTrueCallback, true);
mreMsgStartReverseTrue.WaitOne(310 * 1000);
@@ -130,12 +152,11 @@ private void DetailedHistoryStartWithReverseTrueCallback(string result)
JArray jArr = receivedObj[0] as JArray;
if (jArr != null)
{
- //object[] historyObj = (object[])receivedObj[0];
if (jArr.Count >= expectedCountAtStartTimeWithReverseTrue)
{
foreach (object item in jArr)
{
- if (item.ToString().Contains(string.Format("DetailedHistoryStartTimeWithReverseTrue {0}", startTimeWithReverseTrue)))
+ if (item.ToString().Contains("DetailedHistoryStartTimeWithReverseTrue"))
{
actualCountAtStartTimeWithReverseFalse++;
}
@@ -22,6 +22,13 @@ public class WhenGetRequestServerTime
public void ThenItShouldReturnTimeStamp()
{
Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenGetRequestServerTime";
+ unitTest.TestCaseName = "ThenItShouldReturnTimeStamp";
+
+ pubnub.PubnubUnitTest = unitTest;
+
pubnub.time<string>(ReturnTimeStampCallback);
manualEvent1.WaitOne(310 * 1000);
Assert.IsTrue(timeReceived, "time() Failed");
@@ -15,8 +15,11 @@ namespace PubNub_Messaging.Tests
[TestFixture]
public class WhenSubscribedToAChannel
{
- ManualResetEvent manualEvent1 = new ManualResetEvent(false);
- ManualResetEvent manualEvent2 = new ManualResetEvent(false);
+ ManualResetEvent meSubNoConnect = new ManualResetEvent(false);
+ ManualResetEvent meSubYesConnect = new ManualResetEvent(false);
+ ManualResetEvent mePublish = new ManualResetEvent(false);
+ ManualResetEvent meUnsub = new ManualResetEvent(false);
+
bool receivedMessage = false;
@@ -26,19 +29,53 @@ public void ThenSubscribeShouldReturnReceivedMessage()
receivedMessage = false;
Pubnub pubnub = new Pubnub("demo","demo","","",false);
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenSubscribedToAChannel";
+ unitTest.TestCaseName = "ThenSubscribeShouldReturnReceivedMessage";
+
+ pubnub.PubnubUnitTest = unitTest;
+
+
string channel = "my/channel";
- pubnub.subscribe<string>(channel, ReceivedMessageCallback);
- Thread.Sleep(5000);
+ pubnub.subscribe<string>(channel, ReceivedMessageCallbackNoConnect);
+ //Thread.Sleep(5000);
pubnub.publish<string>(channel, "Test for WhenSubscribedToAChannel ThenItShouldReturnReceivedMessage", dummyPublishCallback);
- manualEvent2.WaitOne(310 * 1000);
+ mePublish.WaitOne(310 * 1000);
- manualEvent1.WaitOne(310*1000);
+ meSubNoConnect.WaitOne(310 * 1000);
+ pubnub.unsubscribe<string>(channel, dummyUnsubCallback);
+
+ meUnsub.WaitOne(310 * 1000);
Assert.IsTrue(receivedMessage,"WhenSubscribedToAChannel --> ThenItShouldReturnReceivedMessage Failed");
}
- private void ReceivedMessageCallback(string result)
+ [Test]
+ public void ThenSubscribeShouldReturnConnectStatus()
+ {
+ receivedMessage = false;
+ Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
+
+ PubnubUnitTest unitTest = new PubnubUnitTest();
+ unitTest.TestClassName = "WhenSubscribedToAChannel";
+ unitTest.TestCaseName = "ThenSubscribeShouldReturnConnectStatus";
+
+ pubnub.PubnubUnitTest = unitTest;
+
+
+ string channel = "my/channel";
+
+ pubnub.subscribe<string>(channel, ReceivedMessageCallbackYesConnect, ConnectStatusCallback);
+ meSubYesConnect.WaitOne(310 * 1000);
+
+ pubnub.unsubscribe<string>(channel, dummyUnsubCallback);
+ meUnsub.WaitOne(310 * 1000);
+
+ Assert.IsTrue(receivedMessage, "WhenSubscribedToAChannel --> ThenSubscribeShouldReturnConnectStatus Failed");
+ }
+
+ private void ReceivedMessageCallbackNoConnect(string result)
{
if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
{
@@ -52,13 +89,40 @@ private void ReceivedMessageCallback(string result)
}
}
}
- manualEvent1.Set();
+ meSubNoConnect.Set();
+ }
+
+ private void ReceivedMessageCallbackYesConnect(string result)
+ {
+ //dummy method provided as part of subscribe connect status check.
+ }
+
+ private void ConnectStatusCallback(string result)
+ {
+ if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
+ {
+ object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
+ if (receivedObj is object[])
+ {
+ long statusCode = Int64.Parse(receivedObj[0].ToString());
+ string statusMsg = (string)receivedObj[1];
+ if (statusCode == 1 && statusMsg.ToLower() == "connected")
+ {
+ receivedMessage = true;
+ }
+ }
+ }
+ meSubYesConnect.Set();
}
private void dummyPublishCallback(string result)
{
- manualEvent2.Set();
+ mePublish.Set();
}
+ private void dummyUnsubCallback(string result)
+ {
+ meUnsub.Set();
+ }
}
}
Oops, something went wrong.

0 comments on commit 8ddf9f6

Please sign in to comment.