/
unit-test.xaml.cs
392 lines (354 loc) · 15.7 KB
/
unit-test.xaml.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.Threading;
namespace CSharp_WP7
{
public partial class unit_test : PhoneApplicationPage
{
public unit_test()
{
InitializeComponent();
}
private void btnUnitTest_Click(object sender, RoutedEventArgs e)
{
UnitTestAll(pubnub_high_security);
//UnitTestAll(pubnub_user_supplied_options);
//test_subscribe();
//test_uuid();
//test_time();
//test_publish();
//test_history();
}
// -----------------------------------------------------------------------
// 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, Int32> status = new Dictionary<string, int>(3);
static Dictionary<String, Dictionary<string, Int32>> channelStatus = new Dictionary<String, Dictionary<string, Int32>>(many_channels.Capacity);
static Dictionary<String, Object> threads = new Dictionary<String, Object>(4);
static Dictionary<string, bool> channelConnected = new Dictionary<string, bool>();
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();
many_channels.Clear();
if (runthroughs >= 2)
{
runthroughs = 0;
}
_pubnub = pubnub;
if (!status.ContainsKey("sent"))
status.Add("sent", 0);
if (!status.ContainsKey("received"))
status.Add("received", 0);
if (!status.ContainsKey("connections"))
status.Add("connections", 0);
for (int i = 0; i < many_channels.Capacity; i++)
{
many_channels.Add("channel_" + i);
channelConnected[many_channels[i]] = false;
channelStatus[many_channels[i]] = status;
}
subscribeChannels(_pubnub);
}
static void subscribeChannels(Pubnub pubnub)
{
foreach (string _channel in many_channels)
{
Thread t = new Thread(delegate()
{
Dictionary<String, Object> argsSub = new Dictionary<String, Object>(2);
argsSub.Add("channel", _channel);
argsSub.Add("callback", new Receiver()); // callback to get response
// Listen for Messages (Subscribe)
pubnub.Subscribe(argsSub);
});
t.Start();
if (threads.ContainsKey(_channel))
threads[_channel] = t;
else
threads.Add(_channel, t);
try
{
Thread.Sleep(1000);
}
catch (ThreadAbortException e)
{
Debug.WriteLine(e.Message);
}
}
}
class Receiver : Callback
{
public bool responseCallback(string channel, object message)
{
object[] results = (object[])message;
if (results.Length > 0)
{
int sent = Convert.ToInt32((channelStatus[channel])["sent"]);
int received = Convert.ToInt32((channelStatus[channel])["received"]);
test(received <= sent, "many sends");
received = received + 1;
(channelStatus[channel]).Remove("received");
(channelStatus[channel]).Add("received", received);
Dictionary<String, Object> args = new Dictionary<String, Object>(1);
args.Add("channel", channel);
if (channelConnected[channel])
{
_pubnub.Unsubscribe(args);
}
//int allReceived = 0;
//foreach(var status in channelStatus)
//{
// allReceived += Convert.ToInt32(status.Value["received"]);
//}
//if (allReceived == many_channels.Count)
//{
// runthroughs += 1;
// if (runthroughs < planned_tests)
// {
// //UnitTestAll(_pubnub);
// }
//}
return true;
}
else
return false;
}
public void errorCallback(string channel, object message)
{
//Debug.WriteLine("Channel:" + channel + "-" + message.ToString());
}
public void connectCallback(string channel)
{
if (!channelConnected[channel])
{
Pubnub.ResponseCallback histCallback = delegate(object response)
{
List<object> result = (List<object>)response;
test(result != null && result.Count > 0, "history");
};
Debug.WriteLine("Connected to channel :" + channel);
int connections = Convert.ToInt32(status["connections"]);
status.Remove("connections");
status.Add("connections", connections + 1);
publishAndSetStatus(channel, Message);
Dictionary<string, object> argshist = new Dictionary<string, object>();
argshist.Add("channel", channel);
argshist.Add("limit", 1.ToString());
argshist.Add("callback", histCallback);
_pubnub.History(argshist);
}
}
public void reconnectCallback(string channel)
{
Debug.WriteLine("Reconnected to channel :" + channel);
}
public void disconnectCallback(string channel)
{
Debug.WriteLine("Disconnected to channel :" + channel);
}
}
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";
Pubnub.ResponseCallback respCallback = delegate(object response)
{
List<object> messages = (List<object>)response;
if (messages != null && messages.Count > 0)
{
Debug.WriteLine("[" + messages[0] + " , " + messages[1] + "]");
}
};
Dictionary<string, object> strArgs = new Dictionary<string, object>();
string message = "Hello Silverlight";
strArgs.Add("channel", channel);
strArgs.Add("message", message);
strArgs.Add("callback", respCallback);
objPubnub.Publish(strArgs);
}
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";
Dictionary<string, object> args = new Dictionary<string, object>();
args = new Dictionary<string, object>();
args.Add("channel", channel);
args.Add("callback", new CSharp_WP7.Receiver()); // callback to get response
objPubnub.Subscribe(args);
}
static public void test_history()
{
Pubnub.ResponseCallback histCallback = delegate(object response)
{
List<object> result = (List<object>)response;
Debug.WriteLine("[History data]");
foreach (object data in result)
{
Debug.WriteLine(data);
}
};
// 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, object> argsHist = new Dictionary<string, object>();
argsHist.Add("channel", channel);
argsHist.Add("limit", 3.ToString());
argsHist.Add("callback", histCallback);
objPubnub.History(argsHist);
}
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();
Debug.WriteLine("Generated 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
objPubnub.Time(timedelegate);
}
public static void timedelegate(object response)
{
List<object> result = (List<object>)response;
Debug.WriteLine(" Time is : " + result[0].ToString());
}
public static void publishAndSetStatus(string channel, object message)
{
Pubnub.ResponseCallback publishCallback = delegate(object response)
{
List<object> messages = (List<object>)response;
if (messages != null && messages.Count > 2)
{
int sent = Convert.ToInt32((channelStatus[channel])["sent"]);
(channelStatus[channel]).Remove("sent");
(channelStatus[channel]).Add("sent", sent + 1);
test(messages != null, "publish complete");
test(messages != null && messages.Count > 2, "publish responce");
channelConnected[channel] = true;
}
if (messages == null || messages.Count <= 0)
{
delivery_retries += 1;
if (max_retries > delivery_retries)
{
publishAndSetStatus(channel, message);
}
}
};
// Publish message
Dictionary<string, object> args = new Dictionary<string, object>(2);
args.Add("channel", channel);
args.Add("message", message);
args.Add("callback", publishCallback);
// publish Response
_pubnub.Publish(args);
}
}
}