PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs

https://github.com/mip1983/SignalR
C# | 941 lines | 714 code | 220 blank | 7 comment | 13 complexity | 05e5bf7a2e561272b3f5fae98d8b5c73 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json.Linq;
  7. using Moq;
  8. using Microsoft.AspNet.SignalR.Client.Hubs;
  9. using Microsoft.AspNet.SignalR.Hosting.Memory;
  10. using Microsoft.AspNet.SignalR.Hubs;
  11. using Microsoft.AspNet.SignalR.Tests.Infrastructure;
  12. using Xunit;
  13. namespace Microsoft.AspNet.SignalR.Tests
  14. {
  15. public class HubFacts : IDisposable
  16. {
  17. [Fact]
  18. public void ReadingState()
  19. {
  20. var host = new MemoryHost();
  21. host.MapHubs();
  22. var connection = new Client.Hubs.HubConnection("http://foo/");
  23. var hub = connection.CreateHubProxy("demo");
  24. hub["name"] = "test";
  25. connection.Start(host).Wait();
  26. var result = hub.Invoke<string>("ReadStateValue").Result;
  27. Assert.Equal("test", result);
  28. connection.Stop();
  29. }
  30. [Fact]
  31. public void SettingState()
  32. {
  33. var host = new MemoryHost();
  34. host.MapHubs();
  35. var connection = new Client.Hubs.HubConnection("http://foo/");
  36. var hub = connection.CreateHubProxy("demo");
  37. connection.Start(host).Wait();
  38. var result = hub.Invoke<string>("SetStateValue", "test").Result;
  39. Assert.Equal("test", result);
  40. Assert.Equal("test", hub["Company"]);
  41. connection.Stop();
  42. }
  43. [Fact]
  44. public void GetValueFromServer()
  45. {
  46. var host = new MemoryHost();
  47. host.MapHubs();
  48. var connection = new Client.Hubs.HubConnection("http://foo/");
  49. var hub = connection.CreateHubProxy("demo");
  50. connection.Start(host).Wait();
  51. var result = hub.Invoke<int>("GetValue").Result;
  52. Assert.Equal(10, result);
  53. connection.Stop();
  54. }
  55. [Fact]
  56. public void TaskWithException()
  57. {
  58. var host = new MemoryHost();
  59. host.MapHubs();
  60. var connection = new Client.Hubs.HubConnection("http://foo/");
  61. var hub = connection.CreateHubProxy("demo");
  62. connection.Start(host).Wait();
  63. var ex = Assert.Throws<AggregateException>(() => hub.Invoke("TaskWithException").Wait());
  64. Assert.IsType<InvalidOperationException>(ex.GetBaseException());
  65. Assert.Contains("System.Exception", ex.GetBaseException().Message);
  66. connection.Stop();
  67. }
  68. [Fact]
  69. public void GenericTaskWithException()
  70. {
  71. var host = new MemoryHost();
  72. host.MapHubs();
  73. var connection = new Client.Hubs.HubConnection("http://foo/");
  74. var hub = connection.CreateHubProxy("demo");
  75. connection.Start(host).Wait();
  76. var ex = Assert.Throws<AggregateException>(() => hub.Invoke("GenericTaskWithException").Wait());
  77. Assert.IsType<InvalidOperationException>(ex.GetBaseException());
  78. Assert.Contains("System.Exception", ex.GetBaseException().Message);
  79. connection.Stop();
  80. }
  81. [Fact]
  82. public void GenericTaskWithContinueWith()
  83. {
  84. var host = new MemoryHost();
  85. host.MapHubs();
  86. var connection = new Client.Hubs.HubConnection("http://foo/");
  87. var hub = connection.CreateHubProxy("demo");
  88. connection.Start(host).Wait();
  89. int result = hub.Invoke<int>("GenericTaskWithContinueWith").Result;
  90. Assert.Equal(4, result);
  91. connection.Stop();
  92. }
  93. [Fact]
  94. public void Overloads()
  95. {
  96. var host = new MemoryHost();
  97. host.MapHubs();
  98. var connection = new Client.Hubs.HubConnection("http://foo/");
  99. var hub = connection.CreateHubProxy("demo");
  100. connection.Start(host).Wait();
  101. hub.Invoke("Overload").Wait();
  102. int n = hub.Invoke<int>("Overload", 1).Result;
  103. Assert.Equal(1, n);
  104. connection.Stop();
  105. }
  106. [Fact]
  107. public void UnsupportedOverloads()
  108. {
  109. var host = new MemoryHost();
  110. host.MapHubs();
  111. var connection = new Client.Hubs.HubConnection("http://foo/");
  112. var hub = connection.CreateHubProxy("demo");
  113. connection.Start(host).Wait();
  114. var ex = Assert.Throws<InvalidOperationException>(() => hub.Invoke("UnsupportedOverload", 13177).Wait());
  115. Assert.Equal("'UnsupportedOverload' method could not be resolved.", ex.GetBaseException().Message);
  116. connection.Stop();
  117. }
  118. [Fact]
  119. public void ChangeHubUrl()
  120. {
  121. var host = new MemoryHost();
  122. host.MapHubs("/foo");
  123. var connection = new Client.Hubs.HubConnection("http://site/foo", useDefaultUrl: false);
  124. var hub = connection.CreateHubProxy("demo");
  125. var wh = new ManualResetEvent(false);
  126. hub.On("signal", id =>
  127. {
  128. Assert.NotNull(id);
  129. wh.Set();
  130. });
  131. connection.Start(host).Wait();
  132. hub.Invoke("DynamicTask").Wait();
  133. Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
  134. connection.Stop();
  135. }
  136. [Fact]
  137. public void GuidTest()
  138. {
  139. var host = new MemoryHost();
  140. host.MapHubs();
  141. var connection = new Client.Hubs.HubConnection("http://site/");
  142. var hub = connection.CreateHubProxy("demo");
  143. var wh = new ManualResetEvent(false);
  144. hub.On<Guid>("TestGuid", id =>
  145. {
  146. Assert.NotNull(id);
  147. wh.Set();
  148. });
  149. connection.Start(host).Wait();
  150. hub.Invoke("TestGuid").Wait();
  151. Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
  152. connection.Stop();
  153. }
  154. [Fact]
  155. public void HubHasConnectionEvents()
  156. {
  157. var type = typeof(Hub);
  158. // Hub has the disconnect method
  159. Assert.True(type.GetMethod("OnDisconnected") != null);
  160. // Hub has the connect method
  161. Assert.True(type.GetMethod("OnConnected") != null);
  162. // Hub has the reconnect method
  163. Assert.True(type.GetMethod("OnReconnected") != null);
  164. }
  165. [Fact]
  166. public void ComplexPersonState()
  167. {
  168. var host = new MemoryHost();
  169. host.MapHubs();
  170. var connection = new Client.Hubs.HubConnection("http://site/");
  171. var hub = connection.CreateHubProxy("demo");
  172. var wh = new ManualResetEvent(false);
  173. connection.Start(host).Wait();
  174. var person = new SignalR.Samples.Hubs.DemoHub.DemoHub.Person
  175. {
  176. Address = new SignalR.Samples.Hubs.DemoHub.DemoHub.Address
  177. {
  178. Street = "Redmond",
  179. Zip = "98052"
  180. },
  181. Age = 25,
  182. Name = "David"
  183. };
  184. var person1 = hub.Invoke<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("ComplexType", person).Result;
  185. var person2 = hub.GetValue<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("person");
  186. JObject obj = ((dynamic)hub).person;
  187. var person3 = obj.ToObject<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>();
  188. Assert.NotNull(person1);
  189. Assert.NotNull(person2);
  190. Assert.NotNull(person3);
  191. Assert.Equal("David", person1.Name);
  192. Assert.Equal("David", person2.Name);
  193. Assert.Equal("David", person3.Name);
  194. Assert.Equal(25, person1.Age);
  195. Assert.Equal(25, person2.Age);
  196. Assert.Equal(25, person3.Age);
  197. Assert.Equal("Redmond", person1.Address.Street);
  198. Assert.Equal("Redmond", person2.Address.Street);
  199. Assert.Equal("Redmond", person3.Address.Street);
  200. Assert.Equal("98052", person1.Address.Zip);
  201. Assert.Equal("98052", person2.Address.Zip);
  202. Assert.Equal("98052", person3.Address.Zip);
  203. connection.Stop();
  204. }
  205. [Fact]
  206. public void DynamicInvokeTest()
  207. {
  208. var host = new MemoryHost();
  209. host.MapHubs();
  210. var connection = new Client.Hubs.HubConnection("http://site/");
  211. string callback = @"!!!|\CallMeBack,,,!!!";
  212. var hub = connection.CreateHubProxy("demo");
  213. var wh = new ManualResetEvent(false);
  214. hub.On(callback, () => wh.Set());
  215. connection.Start(host).Wait();
  216. hub.Invoke("DynamicInvoke", callback).Wait();
  217. Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
  218. connection.Stop();
  219. }
  220. [Fact]
  221. public void CreateProxyAfterConnectionStartsThrows()
  222. {
  223. var host = new MemoryHost();
  224. host.MapHubs();
  225. var connection = new Client.Hubs.HubConnection("http://site/");
  226. try
  227. {
  228. connection.Start(host).Wait();
  229. Assert.Throws<InvalidOperationException>(() => connection.CreateHubProxy("demo"));
  230. }
  231. finally
  232. {
  233. connection.Stop();
  234. }
  235. }
  236. [Fact]
  237. public void AddingToMultipleGroups()
  238. {
  239. var host = new MemoryHost();
  240. host.MapHubs();
  241. int max = 10;
  242. var countDown = new CountDownRange<int>(Enumerable.Range(0, max));
  243. var connection = new Client.Hubs.HubConnection("http://foo");
  244. var proxy = connection.CreateHubProxy("MultGroupHub");
  245. proxy.On<User>("onRoomJoin", user =>
  246. {
  247. Assert.True(countDown.Mark(user.Index));
  248. });
  249. connection.Start(host).Wait();
  250. for (int i = 0; i < max; i++)
  251. {
  252. var user = new User { Index = i, Name = "tester", Room = "test" + i };
  253. proxy.Invoke("login", user).Wait();
  254. proxy.Invoke("joinRoom", user).Wait();
  255. }
  256. Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
  257. connection.Stop();
  258. }
  259. [Fact]
  260. public void HubGroupsDontRejoinByDefault()
  261. {
  262. var host = new MemoryHost();
  263. host.Configuration.KeepAlive = null;
  264. host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
  265. host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(1);
  266. host.MapHubs();
  267. int max = 10;
  268. var countDown = new CountDownRange<int>(Enumerable.Range(0, max));
  269. var countDownAfterReconnect = new CountDownRange<int>(Enumerable.Range(max, max));
  270. var connection = new Client.Hubs.HubConnection("http://foo");
  271. var proxy = connection.CreateHubProxy("MultGroupHub");
  272. proxy.On<User>("onRoomJoin", u =>
  273. {
  274. if (u.Index < max)
  275. {
  276. Assert.True(countDown.Mark(u.Index));
  277. }
  278. else
  279. {
  280. Assert.True(countDownAfterReconnect.Mark(u.Index));
  281. }
  282. });
  283. connection.Start(host).Wait();
  284. var user = new User { Name = "tester" };
  285. proxy.Invoke("login", user).Wait();
  286. for (int i = 0; i < max; i++)
  287. {
  288. user.Index = i;
  289. proxy.Invoke("joinRoom", user).Wait();
  290. }
  291. // Force Reconnect
  292. Thread.Sleep(TimeSpan.FromSeconds(3));
  293. for (int i = max; i < 2 * max; i++)
  294. {
  295. user.Index = i;
  296. proxy.Invoke("joinRoom", user).Wait();
  297. }
  298. Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
  299. Assert.True(!countDownAfterReconnect.Wait(TimeSpan.FromSeconds(30)) && countDownAfterReconnect.Count == max);
  300. connection.Stop();
  301. }
  302. [Fact]
  303. public void HubGroupsRejoinWhenAutoRejoiningGroupsEnabled()
  304. {
  305. var host = new MemoryHost();
  306. host.HubPipeline.EnableAutoRejoiningGroups();
  307. host.Configuration.KeepAlive = null;
  308. host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
  309. host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(1);
  310. host.MapHubs();
  311. int max = 10;
  312. var countDown = new CountDownRange<int>(Enumerable.Range(0, max));
  313. var countDownAfterReconnect = new CountDownRange<int>(Enumerable.Range(max, max));
  314. var connection = new Client.Hubs.HubConnection("http://foo");
  315. var proxy = connection.CreateHubProxy("MultGroupHub");
  316. proxy.On<User>("onRoomJoin", u =>
  317. {
  318. if (u.Index < max)
  319. {
  320. Assert.True(countDown.Mark(u.Index));
  321. }
  322. else
  323. {
  324. Assert.True(countDownAfterReconnect.Mark(u.Index));
  325. }
  326. });
  327. connection.Start(host).Wait();
  328. var user = new User { Name = "tester" };
  329. proxy.Invoke("login", user).Wait();
  330. for (int i = 0; i < max; i++)
  331. {
  332. user.Index = i;
  333. proxy.Invoke("joinRoom", user).Wait();
  334. }
  335. // Force Reconnect
  336. Thread.Sleep(TimeSpan.FromSeconds(3));
  337. for (int i = max; i < 2 * max; i++)
  338. {
  339. user.Index = i;
  340. proxy.Invoke("joinRoom", user).Wait();
  341. }
  342. Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
  343. Assert.True(countDownAfterReconnect.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDownAfterReconnect.Count) + " missed " + String.Join(",", countDownAfterReconnect.Left.Select(i => i.ToString())));
  344. connection.Stop();
  345. }
  346. [Fact]
  347. public void RejoiningGroupsOnlyReceivesGroupsBelongingToHub()
  348. {
  349. var logRejoiningGroups = new LogRejoiningGroupsModule();
  350. var host = new MemoryHost();
  351. host.HubPipeline.AddModule(logRejoiningGroups);
  352. host.Configuration.KeepAlive = null;
  353. host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
  354. host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(1);
  355. host.MapHubs();
  356. var connection = new Client.Hubs.HubConnection("http://foo");
  357. var proxy = connection.CreateHubProxy("MultGroupHub");
  358. var proxy2 = connection.CreateHubProxy("MultGroupHub2");
  359. connection.Start(host).Wait();
  360. var user = new User { Name = "tester" };
  361. proxy.Invoke("login", user).Wait();
  362. proxy2.Invoke("login", user).Wait();
  363. // Force Reconnect
  364. Thread.Sleep(TimeSpan.FromSeconds(3));
  365. proxy.Invoke("joinRoom", user).Wait();
  366. proxy2.Invoke("joinRoom", user).Wait();
  367. Thread.Sleep(TimeSpan.FromSeconds(3));
  368. Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("foo"));
  369. Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("tester"));
  370. Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("foo2"));
  371. Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("tester2"));
  372. Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("foo2"));
  373. Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("tester2"));
  374. Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("foo"));
  375. Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("tester"));
  376. connection.Stop();
  377. }
  378. [Fact]
  379. public void CustomQueryStringRaw()
  380. {
  381. var host = new MemoryHost();
  382. host.MapHubs();
  383. var connection = new Client.Hubs.HubConnection("http://foo/", "a=b");
  384. var hub = connection.CreateHubProxy("CustomQueryHub");
  385. connection.Start(host).Wait();
  386. var result = hub.Invoke<string>("GetQueryString", "a").Result;
  387. Assert.Equal("b", result);
  388. connection.Stop();
  389. }
  390. [Fact]
  391. public void CustomQueryString()
  392. {
  393. var host = new MemoryHost();
  394. host.MapHubs();
  395. var qs = new Dictionary<string, string>();
  396. qs["a"] = "b";
  397. var connection = new Client.Hubs.HubConnection("http://foo/", qs);
  398. var hub = connection.CreateHubProxy("CustomQueryHub");
  399. connection.Start(host).Wait();
  400. var result = hub.Invoke<string>("GetQueryString", "a").Result;
  401. Assert.Equal("b", result);
  402. connection.Stop();
  403. }
  404. [Fact]
  405. public void ReturningNullFromConnectAndDisconnectAccepted()
  406. {
  407. var mockHub = new Mock<SomeHub>() { CallBase = true };
  408. mockHub.Setup(h => h.OnConnected()).Returns<Task>(null).Verifiable();
  409. mockHub.Setup(h => h.OnDisconnected()).Returns<Task>(null).Verifiable();
  410. var host = new MemoryHost();
  411. host.HubPipeline.EnableAutoRejoiningGroups();
  412. host.Configuration.KeepAlive = null;
  413. host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(1);
  414. host.DependencyResolver.Register(typeof(SomeHub), () => mockHub.Object);
  415. host.MapHubs();
  416. var connection = new Client.Hubs.HubConnection("http://foo");
  417. var hub = connection.CreateHubProxy("SomeHub");
  418. connection.Start(host).Wait();
  419. connection.Stop();
  420. Thread.Sleep(TimeSpan.FromSeconds(3));
  421. mockHub.Verify();
  422. }
  423. [Fact]
  424. public void ReturningNullFromReconnectAccepted()
  425. {
  426. var mockHub = new Mock<SomeHub>() { CallBase = true };
  427. mockHub.Setup(h => h.OnReconnected()).Returns<Task>(null).Verifiable();
  428. var host = new MemoryHost();
  429. host.HubPipeline.EnableAutoRejoiningGroups();
  430. host.Configuration.KeepAlive = null;
  431. host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
  432. host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(1);
  433. host.DependencyResolver.Register(typeof(SomeHub), () => mockHub.Object);
  434. host.MapHubs();
  435. var connection = new Client.Hubs.HubConnection("http://foo");
  436. var hub = connection.CreateHubProxy("SomeHub");
  437. connection.Start(host).Wait();
  438. // Force Reconnect
  439. Thread.Sleep(TimeSpan.FromSeconds(3));
  440. hub.Invoke("AllFoo").Wait();
  441. Thread.Sleep(TimeSpan.FromSeconds(3));
  442. connection.Stop();
  443. mockHub.Verify();
  444. }
  445. [Fact]
  446. public void UsingHubAfterManualCreationThrows()
  447. {
  448. var hub = new SomeHub();
  449. Assert.Throws<InvalidOperationException>(() => hub.AllFoo());
  450. Assert.Throws<InvalidOperationException>(() => hub.OneFoo());
  451. }
  452. [Fact]
  453. public void CreatedHubsGetDisposed()
  454. {
  455. var mockDemoHubs = new List<Mock<SignalR.Samples.Hubs.DemoHub.DemoHub>>();
  456. var host = new MemoryHost();
  457. host.DependencyResolver.Register(typeof(SignalR.Samples.Hubs.DemoHub.DemoHub), () =>
  458. {
  459. var mockDemoHub = new Mock<SignalR.Samples.Hubs.DemoHub.DemoHub>() { CallBase = true };
  460. mockDemoHubs.Add(mockDemoHub);
  461. return mockDemoHub.Object;
  462. });
  463. host.MapHubs();
  464. var connection = new Client.Hubs.HubConnection("http://foo/");
  465. var hub = connection.CreateHubProxy("demo");
  466. connection.Start(host).Wait();
  467. var result = hub.Invoke<string>("ReadStateValue").Result;
  468. foreach (var mockDemoHub in mockDemoHubs)
  469. {
  470. mockDemoHub.Verify(d => d.Dispose(), Times.Once());
  471. }
  472. connection.Stop();
  473. }
  474. [Fact]
  475. public void SendToAllButCaller()
  476. {
  477. var host = new MemoryHost();
  478. host.MapHubs();
  479. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  480. var connection2 = new Client.Hubs.HubConnection("http://foo/");
  481. var wh1 = new ManualResetEventSlim(initialState: false);
  482. var wh2 = new ManualResetEventSlim(initialState: false);
  483. var hub1 = connection1.CreateHubProxy("SendToSome");
  484. var hub2 = connection2.CreateHubProxy("SendToSome");
  485. connection1.Start(host).Wait();
  486. connection2.Start(host).Wait();
  487. hub1.On("send", wh1.Set);
  488. hub2.On("send", wh2.Set);
  489. hub1.Invoke("SendToAllButCaller").Wait();
  490. Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)));
  491. Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  492. connection1.Stop();
  493. connection2.Stop();
  494. }
  495. [Fact]
  496. public void SendToAllButCallerInGroup()
  497. {
  498. var host = new MemoryHost();
  499. host.MapHubs();
  500. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  501. var connection2 = new Client.Hubs.HubConnection("http://foo/");
  502. var wh1 = new ManualResetEventSlim(initialState: false);
  503. var wh2 = new ManualResetEventSlim(initialState: false);
  504. var hub1 = connection1.CreateHubProxy("SendToSome");
  505. var hub2 = connection2.CreateHubProxy("SendToSome");
  506. connection1.Start(host).Wait();
  507. connection2.Start(host).Wait();
  508. hub1.On("send", wh1.Set);
  509. hub2.On("send", wh2.Set);
  510. hub1.Invoke("JoinGroup", "group").Wait();
  511. hub2.Invoke("JoinGroup", "group").Wait();
  512. hub1.Invoke("AllInGroupButCaller", "group").Wait();
  513. Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)));
  514. Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  515. connection1.Stop();
  516. connection2.Stop();
  517. }
  518. [Fact]
  519. public void SendToAll()
  520. {
  521. var host = new MemoryHost();
  522. host.MapHubs();
  523. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  524. var connection2 = new Client.Hubs.HubConnection("http://foo/");
  525. var wh1 = new ManualResetEventSlim(initialState: false);
  526. var wh2 = new ManualResetEventSlim(initialState: false);
  527. var hub1 = connection1.CreateHubProxy("SendToSome");
  528. var hub2 = connection2.CreateHubProxy("SendToSome");
  529. connection1.Start(host).Wait();
  530. connection2.Start(host).Wait();
  531. hub1.On("send", wh1.Set);
  532. hub2.On("send", wh2.Set);
  533. hub1.Invoke("SendToAll").Wait();
  534. Assert.True(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  535. Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  536. connection1.Stop();
  537. connection2.Stop();
  538. }
  539. [Fact]
  540. public void SendToSelf()
  541. {
  542. var host = new MemoryHost();
  543. host.MapHubs();
  544. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  545. var connection2 = new Client.Hubs.HubConnection("http://foo/");
  546. var wh1 = new ManualResetEventSlim(initialState: false);
  547. var wh2 = new ManualResetEventSlim(initialState: false);
  548. var hub1 = connection1.CreateHubProxy("SendToSome");
  549. var hub2 = connection2.CreateHubProxy("SendToSome");
  550. connection1.Start(host).Wait();
  551. connection2.Start(host).Wait();
  552. hub1.On("send", wh1.Set);
  553. hub2.On("send", wh2.Set);
  554. hub1.Invoke("SendToSelf").Wait();
  555. Assert.True(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  556. Assert.False(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  557. connection1.Stop();
  558. connection2.Stop();
  559. }
  560. [Fact]
  561. public void SendToGroupFromOutsideOfHub()
  562. {
  563. var host = new MemoryHost();
  564. host.MapHubs();
  565. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  566. var hubContext = host.ConnectionManager.GetHubContext("SendToSome");
  567. var wh1 = new ManualResetEventSlim(initialState: false);
  568. var hub1 = connection1.CreateHubProxy("SendToSome");
  569. connection1.Start(host).Wait();
  570. hub1.On("send", wh1.Set);
  571. hubContext.Groups.Add(connection1.ConnectionId, "Foo").Wait();
  572. hubContext.Clients.Group("Foo").send();
  573. Assert.True(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  574. connection1.Stop();
  575. }
  576. [Fact]
  577. public void SendToSpecificClientFromOutsideOfHub()
  578. {
  579. var host = new MemoryHost();
  580. host.MapHubs();
  581. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  582. var hubContext = host.ConnectionManager.GetHubContext("SendToSome");
  583. var wh1 = new ManualResetEventSlim(initialState: false);
  584. var hub1 = connection1.CreateHubProxy("SendToSome");
  585. connection1.Start(host).Wait();
  586. hub1.On("send", wh1.Set);
  587. hubContext.Clients.Client(connection1.ConnectionId).send();
  588. Assert.True(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  589. connection1.Stop();
  590. }
  591. [Fact]
  592. public void SendToAllFromOutsideOfHub()
  593. {
  594. var host = new MemoryHost();
  595. host.MapHubs();
  596. var connection1 = new Client.Hubs.HubConnection("http://foo/");
  597. var connection2 = new Client.Hubs.HubConnection("http://foo/");
  598. var hubContext = host.ConnectionManager.GetHubContext("SendToSome");
  599. var wh1 = new ManualResetEventSlim(initialState: false);
  600. var wh2 = new ManualResetEventSlim(initialState: false);
  601. var hub1 = connection1.CreateHubProxy("SendToSome");
  602. var hub2 = connection2.CreateHubProxy("SendToSome");
  603. connection1.Start(host).Wait();
  604. connection2.Start(host).Wait();
  605. hub1.On("send", wh1.Set);
  606. hub2.On("send", wh2.Set);
  607. hubContext.Clients.All.send();
  608. Assert.True(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  609. Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
  610. connection1.Stop();
  611. connection2.Stop();
  612. }
  613. public class SendToSome : Hub
  614. {
  615. public Task SendToAllButCaller()
  616. {
  617. return Clients.Others.send();
  618. }
  619. public Task SendToAll()
  620. {
  621. return Clients.All.send();
  622. }
  623. public Task JoinGroup(string group)
  624. {
  625. return Groups.Add(Context.ConnectionId, group);
  626. }
  627. public Task AllInGroupButCaller(string group)
  628. {
  629. return Clients.OthersInGroup(group).send();
  630. }
  631. public Task SendToSelf()
  632. {
  633. return Clients.Client(Context.ConnectionId).send();
  634. }
  635. }
  636. public class SomeHub : Hub
  637. {
  638. public void AllFoo()
  639. {
  640. Clients.All.foo();
  641. }
  642. public void OneFoo()
  643. {
  644. Clients.Caller.foo();
  645. }
  646. }
  647. public class CustomQueryHub : Hub
  648. {
  649. public string GetQueryString(string key)
  650. {
  651. return Context.QueryString[key];
  652. }
  653. }
  654. public class MultGroupHub : Hub
  655. {
  656. public virtual Task Login(User user)
  657. {
  658. return Task.Factory.StartNew(
  659. () =>
  660. {
  661. Groups.Remove(Context.ConnectionId, "foo").Wait();
  662. Groups.Add(Context.ConnectionId, "foo").Wait();
  663. Groups.Remove(Context.ConnectionId, user.Name).Wait();
  664. Groups.Add(Context.ConnectionId, user.Name).Wait();
  665. });
  666. }
  667. public Task JoinRoom(User user)
  668. {
  669. return Task.Factory.StartNew(
  670. () =>
  671. {
  672. Clients.Group(user.Name).onRoomJoin(user).Wait();
  673. });
  674. }
  675. }
  676. public class MultGroupHub2 : MultGroupHub
  677. {
  678. public override Task Login(User user)
  679. {
  680. return Task.Factory.StartNew(
  681. () =>
  682. {
  683. Groups.Remove(Context.ConnectionId, "foo2").Wait();
  684. Groups.Add(Context.ConnectionId, "foo2").Wait();
  685. Groups.Remove(Context.ConnectionId, user.Name + "2").Wait();
  686. Groups.Add(Context.ConnectionId, user.Name + "2").Wait();
  687. });
  688. }
  689. }
  690. public class LogRejoiningGroupsModule : HubPipelineModule
  691. {
  692. public Dictionary<string, List<string>> GroupsRejoined = new Dictionary<string,List<string>>();
  693. public override Func<HubDescriptor, IRequest, IEnumerable<string>, IEnumerable<string>> BuildRejoiningGroups(Func<HubDescriptor, IRequest, IEnumerable<string>, IEnumerable<string>> rejoiningGroups)
  694. {
  695. return (hubDescriptor, request, groups) =>
  696. {
  697. if (!GroupsRejoined.ContainsKey(hubDescriptor.Name))
  698. {
  699. GroupsRejoined[hubDescriptor.Name] = new List<string>(groups);
  700. }
  701. else
  702. {
  703. GroupsRejoined[hubDescriptor.Name].AddRange(groups);
  704. }
  705. return groups;
  706. };
  707. }
  708. }
  709. public class User
  710. {
  711. public int Index { get; set; }
  712. public string Name { get; set; }
  713. public string Room { get; set; }
  714. }
  715. public void Dispose()
  716. {
  717. GC.Collect();
  718. GC.WaitForPendingFinalizers();
  719. }
  720. }
  721. }