PageRenderTime 38ms CodeModel.GetById 16ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/SignalR.Tests/HubFacts.cs

https://github.com/kpmrafeeq/SignalR
C# | 384 lines | 292 code | 92 blank | 0 comment | 1 complexity | fcedac3063498cd2f51d442850229a78 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Threading;
  5using System.Threading.Tasks;
  6using Newtonsoft.Json.Linq;
  7using SignalR.Client.Hubs;
  8using SignalR.Hosting.Memory;
  9using SignalR.Hubs;
 10using Xunit;
 11
 12namespace SignalR.Tests
 13{
 14    public class HubFacts
 15    {
 16        [Fact]
 17        public void ReadingState()
 18        {
 19            var host = new MemoryHost();
 20            host.MapHubs();
 21            var connection = new Client.Hubs.HubConnection("http://foo/");
 22
 23            var hub = connection.CreateProxy("demo");
 24
 25            hub["name"] = "test";
 26
 27            connection.Start(host).Wait();
 28
 29            var result = hub.Invoke<string>("ReadStateValue").Result;
 30
 31            Assert.Equal("test", result);
 32
 33            connection.Stop();
 34        }
 35
 36        [Fact]
 37        public void SettingState()
 38        {
 39            var host = new MemoryHost();
 40            host.MapHubs();
 41            var connection = new Client.Hubs.HubConnection("http://foo/");
 42
 43            var hub = connection.CreateProxy("demo");
 44            connection.Start(host).Wait();
 45
 46            var result = hub.Invoke<string>("SetStateValue", "test").Result;
 47
 48            Assert.Equal("test", result);
 49            Assert.Equal("test", hub["Company"]);
 50
 51            connection.Stop();
 52        }
 53
 54        [Fact]
 55        public void GetValueFromServer()
 56        {
 57            var host = new MemoryHost();
 58            host.MapHubs();
 59            var connection = new Client.Hubs.HubConnection("http://foo/");
 60
 61            var hub = connection.CreateProxy("demo");
 62
 63            connection.Start(host).Wait();
 64
 65            var result = hub.Invoke<int>("GetValue").Result;
 66
 67            Assert.Equal(10, result);
 68            connection.Stop();
 69        }
 70
 71        [Fact]
 72        public void TaskWithException()
 73        {
 74            var host = new MemoryHost();
 75            host.MapHubs();
 76            var connection = new Client.Hubs.HubConnection("http://foo/");
 77
 78            var hub = connection.CreateProxy("demo");
 79
 80            connection.Start(host).Wait();
 81
 82            var ex = Assert.Throws<AggregateException>(() => hub.Invoke("TaskWithException").Wait());
 83
 84            Assert.IsType<InvalidOperationException>(ex.GetBaseException());
 85            Assert.Contains("System.Exception", ex.GetBaseException().Message);
 86            connection.Stop();
 87        }
 88
 89        [Fact]
 90        public void GenericTaskWithException()
 91        {
 92            var host = new MemoryHost();
 93            host.MapHubs();
 94            var connection = new Client.Hubs.HubConnection("http://foo/");
 95
 96            var hub = connection.CreateProxy("demo");
 97
 98            connection.Start(host).Wait();
 99
100            var ex = Assert.Throws<AggregateException>(() => hub.Invoke("GenericTaskWithException").Wait());
101
102            Assert.IsType<InvalidOperationException>(ex.GetBaseException());
103            Assert.Contains("System.Exception", ex.GetBaseException().Message);
104            connection.Stop();
105        }
106
107        [Fact]
108        public void Overloads()
109        {
110            var host = new MemoryHost();
111            host.MapHubs();
112            var connection = new Client.Hubs.HubConnection("http://foo/");
113
114            var hub = connection.CreateProxy("demo");
115
116            connection.Start(host).Wait();
117
118            hub.Invoke("Overload").Wait();
119            int n = hub.Invoke<int>("Overload", 1).Result;
120
121            Assert.Equal(1, n);
122            connection.Stop();
123        }
124
125        [Fact]
126        public void UnsupportedOverloads()
127        {
128            var host = new MemoryHost();
129            host.MapHubs();
130            var connection = new Client.Hubs.HubConnection("http://foo/");
131
132            var hub = connection.CreateProxy("demo");
133
134            connection.Start(host).Wait();
135
136            var ex = Assert.Throws<InvalidOperationException>(() => hub.Invoke("UnsupportedOverload", 13177).Wait());
137
138            Assert.Equal("'UnsupportedOverload' method could not be resolved.", ex.GetBaseException().Message);
139            connection.Stop();
140        }
141
142        [Fact]
143        public void ChangeHubUrl()
144        {
145            var host = new MemoryHost();
146            host.MapHubs("/foo");
147            var connection = new Client.Hubs.HubConnection("http://site/foo", useDefaultUrl: false);
148
149            var hub = connection.CreateProxy("demo");
150
151            var wh = new ManualResetEvent(false);
152
153            hub.On("signal", id =>
154            {
155                Assert.NotNull(id);
156                wh.Set();
157            });
158
159            connection.Start(host).Wait();
160
161            hub.Invoke("DynamicTask").Wait();
162
163            Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
164            connection.Stop();
165        }
166
167        [Fact]
168        public void GuidTest()
169        {
170            var host = new MemoryHost();
171            host.MapHubs();
172            var connection = new Client.Hubs.HubConnection("http://site/");
173
174            var hub = connection.CreateProxy("demo");
175
176            var wh = new ManualResetEvent(false);
177
178            hub.On<Guid>("TestGuid", id =>
179            {
180                Assert.NotNull(id);
181                wh.Set();
182            });
183
184            connection.Start(host).Wait();
185
186            hub.Invoke("TestGuid").Wait();
187
188            Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
189            connection.Stop();
190        }
191
192        [Fact]
193        public void ComplexPersonState()
194        {
195            var host = new MemoryHost();
196            host.MapHubs();
197            var connection = new Client.Hubs.HubConnection("http://site/");
198
199            var hub = connection.CreateProxy("demo");
200
201            var wh = new ManualResetEvent(false);
202
203            connection.Start(host).Wait();
204
205            var person = new SignalR.Samples.Hubs.DemoHub.DemoHub.Person
206            {
207                Address = new SignalR.Samples.Hubs.DemoHub.DemoHub.Address
208                {
209                    Street = "Redmond",
210                    Zip = "98052"
211                },
212                Age = 25,
213                Name = "David"
214            };
215
216            var person1 = hub.Invoke<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("ComplexType", person).Result;
217            var person2 = hub.GetValue<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("person");
218            JObject obj = ((dynamic)hub).person;
219            var person3 = obj.ToObject<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>();
220
221            Assert.NotNull(person1);
222            Assert.NotNull(person2);
223            Assert.NotNull(person3);
224            Assert.Equal("David", person1.Name);
225            Assert.Equal("David", person2.Name);
226            Assert.Equal("David", person3.Name);
227            Assert.Equal(25, person1.Age);
228            Assert.Equal(25, person2.Age);
229            Assert.Equal(25, person3.Age);
230            Assert.Equal("Redmond", person1.Address.Street);
231            Assert.Equal("Redmond", person2.Address.Street);
232            Assert.Equal("Redmond", person3.Address.Street);
233            Assert.Equal("98052", person1.Address.Zip);
234            Assert.Equal("98052", person2.Address.Zip);
235            Assert.Equal("98052", person3.Address.Zip);
236
237            connection.Stop();
238        }
239
240        [Fact]
241        public void DynamicInvokeTest()
242        {
243            var host = new MemoryHost();
244            host.MapHubs();
245            var connection = new Client.Hubs.HubConnection("http://site/");
246            string callback = "!!!CallMeBack!!!";
247
248            var hub = connection.CreateProxy("demo");
249
250            var wh = new ManualResetEvent(false);
251
252            hub.On(callback, () => wh.Set());
253
254            connection.Start(host).Wait();
255
256            hub.Invoke("DynamicInvoke", callback).Wait();
257
258            Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
259            connection.Stop();
260        }
261
262        [Fact]
263        public void CreateProxyAfterConnectionStartsThrows()
264        {
265            var host = new MemoryHost();
266            host.MapHubs();
267            var connection = new Client.Hubs.HubConnection("http://site/");
268
269            try
270            {
271                connection.Start(host).Wait();
272                Assert.Throws<InvalidOperationException>(() => connection.CreateProxy("demo"));
273            }
274            finally
275            {
276                connection.Stop();
277            }
278        }
279
280        [Fact]
281        public void AddingToMultipleGroups()
282        {
283            var host = new MemoryHost();
284            host.MapHubs();
285            int max = 100;
286
287            var countDown = new CountDown(max);
288            var list = Enumerable.Range(0, max).ToList();
289            var connection = new Client.Hubs.HubConnection("http://foo");
290            var proxy = connection.CreateProxy("MultGroupHub");
291
292            proxy.On<User>("onRoomJoin", user =>
293            {
294                lock (list)
295                {
296                    list.Remove(user.Index);
297                }
298
299                countDown.Dec();
300            });
301
302            connection.Start(host).Wait();
303
304            for (int i = 0; i < max; i++)
305            {
306                proxy.Invoke("login", new User { Index = i, Name = "tester", Room = "test" + i }).Wait();
307            }
308
309            Assert.True(countDown.Wait(TimeSpan.FromSeconds(10)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", list.Select(i => i.ToString())));
310
311            connection.Stop();
312        }
313
314        [Fact]
315        public void CustomQueryStringRaw()
316        {
317            var host = new MemoryHost();
318            host.MapHubs();
319            var connection = new Client.Hubs.HubConnection("http://foo/", "a=b");
320
321            var hub = connection.CreateProxy("CustomQueryHub");
322
323            connection.Start(host).Wait();
324
325            var result = hub.Invoke<string>("GetQueryString", "a").Result;
326
327            Assert.Equal("b", result);
328
329            connection.Stop();
330        }
331
332        [Fact]
333        public void CustomQueryString()
334        {
335            var host = new MemoryHost();
336            host.MapHubs();
337            var qs = new Dictionary<string, string>();
338            qs["a"] = "b";
339            var connection = new Client.Hubs.HubConnection("http://foo/", qs);
340
341            var hub = connection.CreateProxy("CustomQueryHub");
342
343            connection.Start(host).Wait();
344
345            var result = hub.Invoke<string>("GetQueryString", "a").Result;
346
347            Assert.Equal("b", result);
348
349            connection.Stop();
350        }
351
352        public class CustomQueryHub : Hub
353        {
354            public string GetQueryString(string key)
355            {
356                return Context.QueryString[key];
357            }
358        }
359
360        public class MultGroupHub : Hub
361        {
362            public Task Login(User user)
363            {
364                return Task.Factory.StartNew(
365                    () =>
366                    {
367                        Groups.Remove(Context.ConnectionId, "foo").Wait();
368                        Groups.Add(Context.ConnectionId, "foo").Wait();
369
370                        Groups.Remove(Context.ConnectionId, user.Name).Wait();
371                        Groups.Add(Context.ConnectionId, user.Name).Wait();
372                        Clients[user.Name].onRoomJoin(user).Wait();
373                    });
374            }
375        }
376
377        public class User
378        {
379            public int Index { get; set; }
380            public string Name { get; set; }
381            public string Room { get; set; }
382        }
383    }
384}