Skip to content
Browse files

Added stronger unit test for c#-IIS

  • Loading branch information...
1 parent f39d88b commit 41217aa08ae031ab5aa483144916fbf5a6e6f840 priyanka committed
View
15 csharp-iis/src/Pubnub.cs
@@ -239,6 +239,9 @@ public void Subscribe(Dictionary<string, object> args)
*/
private void _subscribe(Dictionary<string, object> args)
{
+ bool is_disconnect = false;
+ bool is_alreadyConnect = false;
+
Procedure callback = null, connect_cb, disconnect_cb, reconnect_cb, error_cb;
clsPubnubCrypto pc = new clsPubnubCrypto(this.CIPHER_KEY);
@@ -283,6 +286,12 @@ private void _subscribe(Dictionary<string, object> args)
if (cs.channel == channel)
{
channel_exist = true;
+ if (!cs.connected)
+ {
+ cs.connected = true;
+ }
+ else
+ is_alreadyConnect = true;
break;
}
}
@@ -293,7 +302,7 @@ private void _subscribe(Dictionary<string, object> args)
cs.connected = true;
subscriptions.Add(cs);
}
- else
+ else if (is_alreadyConnect)
{
error_cb("Already Connected");
return;
@@ -326,7 +335,7 @@ private void _subscribe(Dictionary<string, object> args)
url.Add(timetoken.ToString());
// Stop Connection?
- bool is_disconnect = false;
+ is_disconnect = false;
foreach (Channel_status cs in subscriptions)
{
if (cs.channel == channel)
@@ -535,7 +544,7 @@ private List<object> _request(List<string> url_components)
string message = sb.ToString();
return JsonConvert.DeserializeObject<List<object>>(message);
}
- catch (Exception ex)
+ catch (Exception)
{
List<object> error = new List<object>();
if (url_components[0] == "time")
View
16 csharp-iis/test/unit-test.aspx
@@ -0,0 +1,16 @@
+<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="unit-test.aspx.cs" Inherits="csharp_webApp.unit_test" %>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head runat="server">
+ <title></title>
+</head>
+<body>
+ <form id="form1" runat="server">
+ <div>
+
+ </div>
+ </form>
+</body>
+</html>
View
390 csharp-iis/test/unit-test.aspx.cs
@@ -0,0 +1,390 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.UI;
+using System.Web.UI.WebControls;
+using System.Threading;
+using System.Diagnostics;
+
+namespace csharp_webApp
+{
+ public partial class unit_test : System.Web.UI.Page
+ {
+
+ static public void test_publish()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //define channel
+ string channel = "hello-world";
+
+ List<object> info = null;
+ Dictionary<string, object> args = new Dictionary<string, object>();
+ // Publish string message
+ args.Add("channel", channel);
+ args.Add("message", "Hello Csharp - IIS");
+ info = objPubnub.Publish(args);
+ // Print response
+ Debug.WriteLine("");
+ Debug.WriteLine("Published messages - >");
+ if (info != null)
+ {
+ if (info.Count == 3)
+ {
+ Debug.WriteLine("[" + info[0].ToString() + ", " + info[1] + ", " + info[2] + "]");
+ }
+ else if (info.Count == 2)
+ {
+ Debug.WriteLine("[" + info[0].ToString() + ", " + info[1] + "]");
+ }
+ }
+ else
+ {
+ Debug.WriteLine("Error in network connection");
+ }
+ }
+ static public void test_subscribe()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //define channel
+ string channel = "hello-world";
+
+ //Subscribe messages
+ pubnub.Procedure Receiver = delegate(object message)
+ {
+ Debug.WriteLine("Message - " + message);
+ return true;
+ };
+ pubnub.Procedure ConnectCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure DisconnectCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ReconnectCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ErrorCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+
+ Dictionary<string, object> args = new Dictionary<string, object>();
+ args = new Dictionary<string, object>();
+ args.Add("channel", channel);
+ args.Add("callback", Receiver); // callback to get response
+ args.Add("connect_cb", ConnectCallback); // callback to get connect event
+ args.Add("disconnect_cb", DisconnectCallback); // callback to get disconnect event
+ args.Add("reconnect_cb", ReconnectCallback); // callback to get reconnect event
+ args.Add("error_cb", ErrorCallback); // callback to get error event
+
+ objPubnub.Subscribe(args);
+ }
+ static public void test_history()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //define channel
+ string channel = "hello-world";
+
+ // History
+ Dictionary<string, string> args = new Dictionary<string, string>();
+ args.Add("channel", channel);
+ args.Add("limit", 3.ToString());
+ List<object> history = objPubnub.History(args);
+ Debug.WriteLine("");
+ Debug.WriteLine("History messages - > ");
+ foreach (object history_message in history)
+ {
+ Debug.WriteLine(history_message);
+ }
+ }
+ static public void test_uuid()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //Get UUID
+ Debug.WriteLine("");
+ Debug.WriteLine("Generated UUID - > " + objPubnub.UUID());
+
+ }
+ static public void test_time()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ // Get PubNub Server Time
+ Debug.WriteLine("");
+ Debug.WriteLine("Server Time - > " + objPubnub.Time());
+ }
+ protected void Page_Load(object sender, EventArgs e)
+ {
+ //test_publish();
+ //test_subscribe();
+ //test_history();
+ //test_time();
+ //test_uuid();
+ UnitTestAll(pubnub_high_security);
+ UnitTestAll(pubnub_user_supplied_options);
+ }
+
+ // -----------------------------------------------------------------------
+ // unit-test-all
+ // -----------------------------------------------------------------------
+
+ static String publish_key = "demo", subscribe_key = "demo";
+ static String secret_key = "demo", cipher_key = "";
+ static Boolean ssl_on = false;
+
+ // -----------------------------------------------------------------------
+ // Command Line Options Supplied PubNub
+ // -----------------------------------------------------------------------
+
+ static pubnub pubnub_user_supplied_options = new pubnub(publish_key, // OPTIONAL (supply None to disable)
+ subscribe_key, // REQUIRED
+ secret_key, // OPTIONAL (supply None to disable)
+ cipher_key, // OPTIONAL (supply None to disable)
+ ssl_on // OPTIONAL (supply None to disable)
+ );
+
+ // -----------------------------------------------------------------------
+ // High Security PubNub
+ // -----------------------------------------------------------------------
+ static pubnub pubnub_high_security = new pubnub(
+ // Publish Key
+ "pub-c-a30c030e-9f9c-408d-be89-d70b336ca7a0",
+
+ // Subscribe Key
+ "sub-c-387c90f3-c018-11e1-98c9-a5220e0555fd",
+
+ // Secret Key
+ "sec-c-MTliNDE0NTAtYjY4Ni00MDRkLTllYTItNDhiZGE0N2JlYzBl",
+
+ // Cipher Key
+ "YWxzamRmbVjFaa05HVnGFqZHM3NXRBS73jxmhVMkjiwVVXV1d5UrXR1JLSkZFRr"
+ + "WVd4emFtUm1iR0TFpUZvbiBoYXMgYmVlbxWkhNaF3uUi8kM0YkJTEVlZYVFjBYi"
+ + "jFkWFIxSkxTa1pGUjd874hjklaTFpUwRVuIFNob3VsZCB5UwRkxUR1J6YVhlQWa"
+ + "V1ZkNGVH32mDkdho3pqtRnRVbTFpUjBaeGUgYXNrZWQtZFoKjda40ZWlyYWl1eX"
+ + "U4RkNtdmNub2l1dHE2TTA1jd84jkdJTbFJXYkZwWlZtRnKkWVrSRhhWbFpZVmFz"
+ + "c2RkZmTFpUpGa1dGSXhTa3hUYTFwR1Vpkm9yIGluZm9ybWFNfdsWQdSiiYXNWVX"
+ + "RSblJWYlRGcFVqQmFlRmRyYUU0MFpXbHlZV2wxZVhVNFJrTnR51YjJsMWRIRTJU"
+ + "W91ciBpbmZvcm1hdGliBzdWJtaXR0ZWQb3UZSBhIHJlc3BvbnNlLCB3ZWxsIHJl"
+ + "VEExWdHVybiB0am0aW9uIb24gYXMgd2UgcG9zc2libHkgY2FuLuhcFe24ldWVns"
+ + "dSaTFpU3hVUjFKNllWaFdhRmxZUWpCaQo34gcmVxdWlGFzIHNveqQl83snBfVl3",
+
+ // 2048bit SSL ON - ENABLED TRUE
+ false);
+
+ // -----------------------------------------------------------------------
+ // Channel | Message Test Data (UTF-8)
+ // -----------------------------------------------------------------------
+ static string Message = " ~`â¦â§!@#$%^&*(???)+=[]\\{}|;\':,./<>?abcd";
+ static pubnub _pubnub;
+ static List<String> many_channels = new List<String>(10);
+
+ static Dictionary<String, Object> status = new Dictionary<String, Object>(3);
+
+ static Dictionary<String, Object> threads = new Dictionary<String, Object>(4);
+
+ static int max_retries = 10;
+ static int planned_tests = 2;
+ static int runthroughs = 0;
+ static int delivery_retries = 0;
+
+ // -----------------------------------------------------------------------
+ // Unit Test Function
+ // -----------------------------------------------------------------------
+
+ static void test(Boolean trial, String name)
+ {
+ if (trial)
+ Debug.WriteLine("PASS " + name);
+ else
+ Debug.WriteLine("- FAIL - " + name);
+ }
+ static void UnitTestAll(pubnub pubnub)
+ {
+ status.Clear();
+ threads.Clear();
+ if (runthroughs >= 2)
+ {
+ runthroughs = 0;
+ }
+
+ _pubnub = pubnub;
+ if (many_channels.Count <= 0)
+ {
+ for (int i = 0; i < many_channels.Capacity; i++)
+ {
+ many_channels.Add("channel_" + i);
+ }
+ }
+ if (status.ContainsKey("sent"))
+ status["sent"] = 0;
+ else
+ status.Add("sent", 0);
+ if (status.ContainsKey("received"))
+ status["received"] = 0;
+ else
+ status.Add("received", 0);
+ if (status.ContainsKey("connections"))
+ status["connections"] = 0;
+ else
+ status.Add("connections", 0);
+
+
+ foreach (string _channel in many_channels)
+ {
+ ParameterizedThreadStart pts = new ParameterizedThreadStart(run);
+ Thread t = new Thread(pts);
+ t.Start(_channel);
+ if (threads.ContainsKey(_channel))
+ threads[_channel] = t;
+ else
+ threads.Add(_channel, t);
+ try
+ {
+ Thread.Sleep(1000);
+ }
+ catch (ThreadInterruptedException e)
+ {
+ Debug.WriteLine(e.Message);
+ }
+ }
+ }
+ public static void run(object _channel)
+ {
+ //Subscribe messages
+ pubnub.Procedure Receiver = delegate(object message)
+ {
+ int sent = Convert.ToInt32(status["sent"]);
+ int received = Convert.ToInt32(status["received"]);
+ test(received <= sent, "many sends");
+ status.Remove("received");
+ status.Add("received", received + 1);
+ Dictionary<String, Object> args = new Dictionary<String, Object>(1);
+ args.Add("channel", _channel);
+ _pubnub.Unsubscribe(args);
+ Dictionary<String, string> argsHistory = new Dictionary<String, string>(2);
+ argsHistory.Add("channel", _channel.ToString());
+ argsHistory.Add("limit", "2");
+
+ List<object> response = _pubnub.History(argsHistory);
+ test(response != null && response.Count > 0, "history");
+ Debug.WriteLine("received count " + Convert.ToInt32(status["received"]));
+ if (Convert.ToInt32(status["received"]) == many_channels.Count)
+ {
+ runthroughs += 1;
+ if (runthroughs < planned_tests)
+ {
+ UnitTestAll(_pubnub);
+ }
+ }
+ return true;
+ };
+
+ pubnub.Procedure ConnectCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ int connections = Convert.ToInt32(status["connections"]);
+ status.Remove("connections");
+ status.Add("connections", connections + 1);
+ publishAndSetStatus(_channel.ToString(), Message);
+ return true;
+ };
+ pubnub.Procedure DisconnectCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ReconnectCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ErrorCallback = delegate(object message)
+ {
+ Debug.WriteLine(message);
+ return true;
+ };
+ Dictionary<String, Object> argsSub = new Dictionary<String, Object>(6);
+ argsSub.Add("channel", _channel.ToString());
+ argsSub.Add("callback", Receiver); // callback to get response
+ argsSub.Add("connect_cb", ConnectCallback); // callback to get connect event
+ argsSub.Add("disconnect_cb", DisconnectCallback); // callback to get disconnect event
+ argsSub.Add("reconnect_cb", ReconnectCallback); // callback to get reconnect event
+ argsSub.Add("error_cb", ErrorCallback); // callback to get error event
+
+ // Listen for Messages (Subscribe)
+ _pubnub.Subscribe(argsSub);
+ }
+ public static void publishAndSetStatus(string channel, object message)
+ {
+ // Publish message in array format
+ Dictionary<string, object> args = new Dictionary<string, object>(2);
+
+ args.Add("channel", channel);
+ args.Add("message", message);
+
+ // publish Response
+ List<object> info = _pubnub.Publish(args);
+ int sent = Convert.ToInt32(status["sent"]);
+ status.Remove("sent");
+ status.Add("sent", sent + 1);
+ test(info != null, "publish complete");
+ test(info != null && info.Count > 2, "publish responce");
+
+ if (info == null || info.Count <= 0)
+ {
+ delivery_retries += 1;
+ if (max_retries > delivery_retries)
+ {
+ publishAndSetStatus(channel, message);
+ }
+ }
+ }
+ }
+}
View
26 csharp-iis/test/unit-test.aspx.designer.cs
@@ -0,0 +1,26 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+// This code was generated by a tool.
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace csharp_webApp
+{
+
+
+ public partial class unit_test
+ {
+
+ /// <summary>
+ /// form1 control.
+ /// </summary>
+ /// <remarks>
+ /// Auto-generated field.
+ /// To modify move field declaration from designer file to code-behind file.
+ /// </remarks>
+ protected global::System.Web.UI.HtmlControls.HtmlForm form1;
+ }
+}
View
15 csharp-mono/src/Pubnub.cs
@@ -248,6 +248,8 @@ public void Subscribe(Dictionary<string, object> args)
*/
private void _subscribe(Dictionary<string, object> args)
{
+ bool is_disconnect = false;
+ bool is_alreadyConnect = false;
Procedure callback=null, connect_cb, disconnect_cb, reconnect_cb, error_cb;
clsPubnubCrypto pc = new clsPubnubCrypto(this.CIPHER_KEY);
@@ -292,6 +294,12 @@ private void _subscribe(Dictionary<string, object> args)
if (cs.channel == channel)
{
channel_exist = true;
+ if (!cs.connected)
+ {
+ cs.connected = true;
+ }
+ else
+ is_alreadyConnect = true;
break;
}
}
@@ -302,11 +310,12 @@ private void _subscribe(Dictionary<string, object> args)
cs.connected = true;
subscriptions.Add(cs);
}
- else
+ else if (is_alreadyConnect)
{
error_cb("Already Connected");
return;
}
+
}
else
{
@@ -332,8 +341,8 @@ private void _subscribe(Dictionary<string, object> args)
url.Add("0");
url.Add(timetoken.ToString());
- // Stop Connection?
- bool is_disconnect = false;
+ // Stop Connection?
+ is_disconnect = false;
foreach (Channel_status cs in subscriptions)
{
if (cs.channel == channel)
View
372 csharp-mono/unit-test.cs
@@ -0,0 +1,372 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using Pubnub;
+
+namespace csharp_demo
+{
+ class unit_test
+ {
+ static public void Main()
+ {
+ //test_publish();
+ //test_history();
+ //test_uuid();
+ //test_time();
+ //test_subscribe();
+ UnitTestAll(pubnub_high_security);
+ //UnitTestAll(pubnub_user_supplied_options);
+ }
+ static public void test_publish()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //define channel
+ string channel = "hello-world";
+
+ // Publish string message
+ Dictionary<string, object> args = new Dictionary<string, object>();
+ args.Add("channel", channel);
+ args.Add("message", "Hello Csharp - mono");
+ List<object> info = null;
+
+ info = objPubnub.Publish(args);
+ if (info != null)
+ {
+ if (info.Count == 3) //success
+ {
+ Console.WriteLine("[ " + info[0].ToString() + ", " + info[1] + ", " + info[2] + "]");
+ }
+ else if (info.Count == 2) //error
+ {
+ Console.WriteLine("[" + info[0].ToString() + ", " + info[1] + "]");
+ }
+ }
+ else
+ {
+ Console.WriteLine("Error in network connection");
+ }
+ }
+ static public void test_subscribe()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //define channel
+ string channel = "hello-world";
+
+ //Subscribe messages
+ pubnub.Procedure Receiver = delegate(object message)
+ {
+ Console.WriteLine("Message - " + message);
+ return true;
+ };
+ pubnub.Procedure ConnectCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure DisconnectCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ReconnectCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ErrorCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+
+ Dictionary<string, object> args = new Dictionary<string, object>();
+ args = new Dictionary<string, object>();
+ args.Add("channel", channel);
+ args.Add("callback", Receiver); // callback to get response
+ args.Add("connect_cb", ConnectCallback); // callback to get connect event
+ args.Add("disconnect_cb", DisconnectCallback); // callback to get disconnect event
+ args.Add("reconnect_cb", ReconnectCallback); // callback to get reconnect event
+ args.Add("error_cb", ErrorCallback); // callback to get error event
+
+ objPubnub.Subscribe(args);
+
+ Console.ReadKey();
+ }
+ static public void test_history()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //define channel
+ string channel = "hello-world";
+
+ // History
+ Dictionary<string, string> argsHist = new Dictionary<string, string>();
+ argsHist.Add("channel", channel);
+ argsHist.Add("limit", 3.ToString());
+ List<object> history = objPubnub.History(argsHist);
+ Console.Write("History Messages: ");
+ foreach (object history_message in history)
+ {
+ Console.WriteLine(history_message);
+ }
+ }
+ static public void test_uuid()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ //Get UUID
+ string uuid = objPubnub.UUID();
+ Console.WriteLine("UUID - " + uuid);
+
+ }
+ static public void test_time()
+ {
+ // Initialize pubnub state
+ pubnub objPubnub = new pubnub(
+ "demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ false // SSL_ON?
+ );
+
+ // Get PubNub Server Time
+ object timestamp = objPubnub.Time();
+ Console.WriteLine("\nServer Time: " + timestamp.ToString());
+ }
+
+ // -----------------------------------------------------------------------
+ // unit-test-all
+ // -----------------------------------------------------------------------
+
+ static String publish_key = "demo", subscribe_key = "demo";
+ static String secret_key = "demo", cipher_key = "";
+ static Boolean ssl_on = false;
+
+ // -----------------------------------------------------------------------
+ // Command Line Options Supplied PubNub
+ // -----------------------------------------------------------------------
+
+ static pubnub pubnub_user_supplied_options = new pubnub(publish_key, // OPTIONAL (supply None to disable)
+ subscribe_key, // REQUIRED
+ secret_key, // OPTIONAL (supply None to disable)
+ cipher_key, // OPTIONAL (supply None to disable)
+ ssl_on // OPTIONAL (supply None to disable)
+ );
+
+ // -----------------------------------------------------------------------
+ // High Security PubNub
+ // -----------------------------------------------------------------------
+ static pubnub pubnub_high_security = new pubnub(
+ // Publish Key
+ "pub-c-a30c030e-9f9c-408d-be89-d70b336ca7a0",
+
+ // Subscribe Key
+ "sub-c-387c90f3-c018-11e1-98c9-a5220e0555fd",
+
+ // Secret Key
+ "sec-c-MTliNDE0NTAtYjY4Ni00MDRkLTllYTItNDhiZGE0N2JlYzBl",
+
+ // Cipher Key
+ "YWxzamRmbVjFaa05HVnGFqZHM3NXRBS73jxmhVMkjiwVVXV1d5UrXR1JLSkZFRr"
+ + "WVd4emFtUm1iR0TFpUZvbiBoYXMgYmVlbxWkhNaF3uUi8kM0YkJTEVlZYVFjBYi"
+ + "jFkWFIxSkxTa1pGUjd874hjklaTFpUwRVuIFNob3VsZCB5UwRkxUR1J6YVhlQWa"
+ + "V1ZkNGVH32mDkdho3pqtRnRVbTFpUjBaeGUgYXNrZWQtZFoKjda40ZWlyYWl1eX"
+ + "U4RkNtdmNub2l1dHE2TTA1jd84jkdJTbFJXYkZwWlZtRnKkWVrSRhhWbFpZVmFz"
+ + "c2RkZmTFpUpGa1dGSXhTa3hUYTFwR1Vpkm9yIGluZm9ybWFNfdsWQdSiiYXNWVX"
+ + "RSblJWYlRGcFVqQmFlRmRyYUU0MFpXbHlZV2wxZVhVNFJrTnR51YjJsMWRIRTJU"
+ + "W91ciBpbmZvcm1hdGliBzdWJtaXR0ZWQb3UZSBhIHJlc3BvbnNlLCB3ZWxsIHJl"
+ + "VEExWdHVybiB0am0aW9uIb24gYXMgd2UgcG9zc2libHkgY2FuLuhcFe24ldWVns"
+ + "dSaTFpU3hVUjFKNllWaFdhRmxZUWpCaQo34gcmVxdWlGFzIHNveqQl83snBfVl3",
+
+ // 2048bit SSL ON - ENABLED TRUE
+ false);
+
+ // -----------------------------------------------------------------------
+ // Channel | Message Test Data (UTF-8)
+ // -----------------------------------------------------------------------
+ static string Message = " ~`â¦â§!@#$%^&*(???)+=[]\\{}|;\':,./<>?abcd";
+ static pubnub _pubnub;
+ static List<String> many_channels = new List<String>(10);
+
+ static Dictionary<String, Object> status = new Dictionary<String, Object>(3);
+
+ static Dictionary<String, Object> threads = new Dictionary<String, Object>(4);
+
+ static int max_retries = 10;
+ static int planned_tests = 2;
+ static int runthroughs = 0;
+ static int delivery_retries = 0;
+ // -----------------------------------------------------------------------
+ // Unit Test Function
+ // -----------------------------------------------------------------------
+
+ static void test(Boolean trial, String name)
+ {
+ if (trial)
+ Console.WriteLine("PASS " + name);
+ else
+ Console.WriteLine("- FAIL - " + name);
+ }
+ static void UnitTestAll(pubnub pubnub)
+ {
+ if (runthroughs >= 2)
+ {
+ runthroughs = 0;
+ }
+ status.Clear();
+ threads.Clear();
+
+ _pubnub = pubnub;
+ if (many_channels.Count <= 0)
+ {
+ for (int i = 0; i < many_channels.Capacity; i++)
+ {
+ many_channels.Add("channel_" + i);
+ }
+ }
+ status.Add("sent", 0);
+ status.Add("received", 0);
+ status.Add("connections", 0);
+
+ foreach (string _channel in many_channels)
+ {
+ ParameterizedThreadStart pts = new ParameterizedThreadStart(run);
+ Thread t = new Thread(pts);
+ t.Start(_channel);
+ threads.Add(_channel, t);
+ try
+ {
+ Thread.Sleep(500);
+ }
+ catch (ThreadInterruptedException e)
+ {
+ Console.WriteLine(e.Message);
+ }
+ }
+ Console.ReadKey();
+ }
+ public static void run(object _channel)
+ {
+ //Subscribe messages
+ pubnub.Procedure Receiver = delegate(object message)
+ {
+ int sent = Convert.ToInt32(status["sent"]);
+ int received = Convert.ToInt32(status["received"]);
+ test(received <= sent, "many sends");
+ status.Remove("received");
+ status.Add("received", received + 1);
+ Dictionary<String, Object> args = new Dictionary<String, Object>(1);
+ args.Add("channel", _channel);
+ _pubnub.Unsubscribe(args);
+
+ Dictionary<String, string> argsHistory = new Dictionary<String, string>(2);
+ argsHistory.Add("channel", _channel.ToString());
+ argsHistory.Add("limit", "2");
+
+ List<object> response = _pubnub.History(argsHistory);
+ test(response != null && response.Count > 0, "history");
+ if (Convert.ToInt32(status["received"]) == many_channels.Count)
+ {
+ runthroughs += 1;
+ if (runthroughs < planned_tests)
+ {
+ UnitTestAll(_pubnub);
+ }
+ }
+ return true;
+ };
+
+ pubnub.Procedure ConnectCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ int connections = Convert.ToInt32(status["connections"]);
+ status.Remove("connections");
+ status.Add("connections", connections + 1);
+ publishAndSetStatus(_channel.ToString(), Message);
+ return true;
+ };
+ pubnub.Procedure DisconnectCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ReconnectCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+ pubnub.Procedure ErrorCallback = delegate(object message)
+ {
+ Console.WriteLine(message);
+ return true;
+ };
+ Dictionary<String, Object> argsSub = new Dictionary<String, Object>(6);
+ argsSub.Add("channel", _channel.ToString());
+ argsSub.Add("callback", Receiver); // callback to get response
+ argsSub.Add("connect_cb", ConnectCallback); // callback to get connect event
+ argsSub.Add("disconnect_cb", DisconnectCallback); // callback to get disconnect event
+ argsSub.Add("reconnect_cb", ReconnectCallback); // callback to get reconnect event
+ argsSub.Add("error_cb", ErrorCallback); // callback to get error event
+
+ // Listen for Messages (Subscribe)
+ _pubnub.Subscribe(argsSub);
+ }
+ public static void publishAndSetStatus(string channel,object message)
+ {
+ // Publish message in array format
+ Dictionary<string, object> args = new Dictionary<string, object>(2);
+
+ args.Add("channel", channel);
+ args.Add("message", message);
+
+ // publish Response
+ List<object> info = _pubnub.Publish(args);
+ int sent = Convert.ToInt32(status["sent"]);
+ status.Remove("sent");
+ status.Add("sent", sent + 1);
+ test(info != null, "publish complete");
+ test(info != null && info.Count > 2, "publish responce ");
+ if (info == null || info.Count <= 0)
+ {
+ delivery_retries += 1;
+ if (max_retries > delivery_retries)
+ {
+ publishAndSetStatus(channel, message);
+ }
+ }
+ }
+ }
+}

0 comments on commit 41217aa

Please sign in to comment.
Something went wrong with that request. Please try again.