コード例 #1
0
        public void InvokeWithStateCopiesStateToHubProxy()
        {
            var hubResult = new HubResult
            {
                State = new Dictionary<string, JToken>
                {
                    { "state", JToken.FromObject(1) }
                }
            };

            var connection = new Mock<IHubConnection>();
            connection.Setup(m => m.RegisterCallback(It.IsAny<Action<HubResult>>()))
                      .Callback<Action<HubResult>>(callback =>
                      {
                          callback(hubResult);
                      });

            connection.Setup(m => m.Send(It.IsAny<string>()))
                      .Returns(TaskAsyncHelper.Empty);

            connection.SetupGet(x => x.JsonSerializer).Returns(new JsonSerializer());

            var hubProxy = new HubProxy(connection.Object, "foo");

            hubProxy.Invoke("Anything").Wait();

            Assert.Equal(1, hubProxy["state"]);
        }
コード例 #2
0
ファイル: ClientHubProvider.cs プロジェクト: Veggie13/Genesis
 private void Disconnect()
 {
     _hub.DisconnectHub();
     _app.AppID = 0;
     _hub = null;
     ChannelServices.UnregisterChannel(_chan);
     _app = null;
 }
コード例 #3
0
ファイル: HubConnection.cs プロジェクト: JamesTryand/SignalR
 public IHubProxy CreateProxy(string hub)
 {
     HubProxy hubProxy;
     if (!_hubs.TryGetValue(hub, out hubProxy)) {
         hubProxy = new HubProxy(this, hub);
         _hubs[hub] = hubProxy;
     }
     return hubProxy;
 }
コード例 #4
0
		public IHubProxy CreateProxy(string hubName)
		{
			HubProxy _hubProxy;
			if (!m_hubs.TryGetValue(hubName, out _hubProxy))
			{
				_hubProxy = new HubProxy(this, hubName);
				m_hubs[hubName] = _hubProxy;
			}
			return _hubProxy;
		}
コード例 #5
0
ファイル: ClientHubProvider.cs プロジェクト: Veggie13/Genesis
        public bool Initialize(IGenesisApplication app)
        {
            if (_hub != null)
                return false;

            ChannelServices.RegisterChannel(_chan, false);
            Hub mainHub = (Hub)Activator.GetObject(typeof(Hub), _url);
            _hub = new HubProxy(mainHub);
            _hub.RegisterApplication(app);
            mainHub.AboutToDie += new Hub.DeathBedEvent(OnDeathBed);
            _app = app;

            return true;
        }
コード例 #6
0
ファイル: HubProxyTest.cs プロジェクト: ninjaAB/SignalR
        public void InvokeEventRaisesEvent()
        {
            var connection = new Mock<SignalR.Client.IConnection>();
            var hubProxy = new HubProxy(connection.Object, "foo");
            bool eventRaised = false;

            hubProxy.On("foo", () =>
            {
                eventRaised = true;
            });

            hubProxy.InvokeEvent("foo", new object[] { });
            Assert.True(eventRaised);
        }
コード例 #7
0
ファイル: HubProxyTest.cs プロジェクト: ninjaAB/SignalR
        public void GetSubscriptionsReturnsListOfSubscriptions()
        {
            var connection = new Mock<SignalR.Client.IConnection>();
            var hubProxy = new HubProxy(connection.Object, "foo");

            hubProxy.On<int>("foo", i => { });

            hubProxy.On("baz", () => { });

            var subscriptions = hubProxy.GetSubscriptions().ToList();
            Assert.Equal(2, subscriptions.Count);
            Assert.Equal("foo", subscriptions[0]);
            Assert.Equal("baz", subscriptions[1]);
        }
コード例 #8
0
ファイル: HubConnection.cs プロジェクト: Avatarchik/uSignalR
        /// <summary>
        ///     Creates an <see cref="IHubProxy" /> for the hub with the specified name.
        /// </summary>
        /// <param name="hubName">The name of the hub.</param>
        /// <returns>A <see cref="IHubProxy" /></returns>
        public IHubProxy CreateProxy(string hubName)
        {
            if (State != ConnectionState.Disconnected)
                throw new InvalidOperationException("A HubProxy cannot be added after the connection has been started.");

            HubProxy hubProxy;

            if (_hubs.TryGetValue(hubName, out hubProxy))
                return hubProxy;

            hubProxy = new HubProxy(this, hubName);
            _hubs[hubName] = hubProxy;

            return hubProxy;
        }
コード例 #9
0
ファイル: HubProxyTest.cs プロジェクト: ninjaAB/SignalR
        public void InvokeEventRaisesEventWithData()
        {
            var connection = new Mock<SignalR.Client.IConnection>();
            var hubProxy = new HubProxy(connection.Object, "foo");
            bool eventRaised = false;

            hubProxy.On<int>("foo", (i) =>
            {
                eventRaised = true;
                Assert.Equal(1, i);
            });

            hubProxy.InvokeEvent("foo", new object[] { 1 });
            Assert.True(eventRaised);
        }
コード例 #10
0
ファイル: HubProxyFacts.cs プロジェクト: nonintanon/SignalR
        public void InvokeWithErrorInHubResultReturnsFaultedTask()
        {
            var result = new HubResult<object>
            {
                Error = "This in an error"
            };

            var connection = new Mock<SignalR.Client.IConnection>();
            connection.Setup(m => m.Send<HubResult<object>>(It.IsAny<string>()))
                      .Returns(TaskAsyncHelper.FromResult(result));

            var hubProxy = new HubProxy(connection.Object, "foo");

            AssertAggregateException(() => hubProxy.Invoke("Invoke").Wait(),
                                     "This in an error");
        }
コード例 #11
0
ファイル: HubProxyTest.cs プロジェクト: ninjaAB/SignalR
        public void InvokeReturnsHubsResult()
        {
            var hubResult = new HubResult<object>
            {
                Result = "Something"
            };

            var connection = new Mock<SignalR.Client.IConnection>();
            connection.Setup(m => m.Send<HubResult<object>>(It.IsAny<string>()))
                      .Returns(TaskAsyncHelper.FromResult(hubResult));

            var hubProxy = new HubProxy(connection.Object, "foo");

            var result = hubProxy.Invoke<object>("Anything").Result;

            Assert.Equal(result, "Something");
        }
コード例 #12
0
        protected FixtureBase()
        {
            var hubConnectionMock = new Mock<IHubConnection>();
            hubConnectionMock.SetupGet(m => m.JsonSerializer).Returns(new JsonSerializer());

            _hubProxy = new HubProxy(hubConnectionMock.Object, "whatEver");
            HubProxyMock = new Mock<MockedHubProxy>(_hubProxy);
            HubProxyMock.SetupGet(m => m.JsonSerializer).Returns(_hubProxy.JsonSerializer);
            HubProxyMock.Setup(m => m.Subscribe(It.IsAny<string>()))
                .Returns((string eventName) => _hubProxy.Subscribe(eventName));

            HubProxyMock.Setup(m => m.InvokeEvent(It.IsAny<Expression<Action<IClientContract>>>()))
                .Callback<Expression<Action<IClientContract>>>(call =>
                {
                    var invocation = call.GetActionDetails();
                    _hubProxy.InvokeEvent(invocation.MethodName,
                        invocation.Parameters.Select(JToken.FromObject).ToList());
                });
        }
コード例 #13
0
        public void InvokeWithStateCopiesStateToHubProxy()
        {
            var result = new HubResult<object>
            {
                State = new Dictionary<string, object>
                {
                    { "state", 1 }
                }
            };

            var connection = new Mock<SignalR.Client.IConnection>();
            connection.Setup(m => m.Send<HubResult<object>>(It.IsAny<string>()))
                      .Returns(TaskAsyncHelper.FromResult(result));

            var hubProxy = new HubProxy(connection.Object, "foo");

            hubProxy.Invoke("Anything").Wait();

            Assert.Equal(1, hubProxy["state"]);
        }
コード例 #14
0
ファイル: HubProxyFacts.cs プロジェクト: stirno/SignalR
        public void InvokeReturnsHubsResult()
        {
            var hubResult = new HubResult
            {
                Result = "Something"
            };

            var connection = new Mock<SignalR.Client.Hubs.IHubConnection>();
            connection.Setup(m => m.RegisterCallback(It.IsAny<Action<HubResult>>()))
                      .Callback<Action<HubResult>>(callback =>
                      {
                          callback(hubResult);
                      });

            connection.Setup(m => m.Send(It.IsAny<string>()))
                      .Returns(TaskAsyncHelper.Empty);

            var hubProxy = new HubProxy(connection.Object, "foo");

            var result = hubProxy.Invoke<object>("Anything").Result;

            Assert.Equal(result, "Something");
        }
コード例 #15
0
        public void InvokeWithErrorInHubResultReturnsFaultedTask()
        {
            var hubResult = new HubResult
            {
                Error = "This in an error"
            };

            var connection = new Mock<IHubConnection>();
            connection.Setup(m => m.RegisterCallback(It.IsAny<Action<HubResult>>()))
                      .Callback<Action<HubResult>>(callback =>
                      {
                          callback(hubResult);
                      });

            connection.Setup(m => m.Send(It.IsAny<string>()))
                      .Returns(TaskAsyncHelper.Empty);

            connection.SetupGet(x => x.JsonSerializer).Returns(new JsonSerializer());

            var hubProxy = new HubProxy(connection.Object, "foo");

            TestUtilities.AssertAggregateException(() => hubProxy.Invoke("Invoke").Wait(),
                                     "This in an error");
        }
コード例 #16
0
        public void FailedHubCallbackDueToReconnectFollowedByInvoke()
        {
            // Arrange
            var testTcs = new TaskCompletionSource<object>();
            var crashTcs = new TaskCompletionSource<object>();
            var connection = new HubConnection("http://test");
            var transport = new Mock<IClientTransport>();

            transport.Setup(t => t.Negotiate(connection, /* connectionData: */ It.IsAny<string>()))
                     .Returns(TaskAsyncHelper.FromResult(new NegotiationResponse
                     {
                         ProtocolVersion = connection.Protocol.ToString(),
                         ConnectionId = "Something",
                         DisconnectTimeout = 120
                     }));

            transport.Setup(t => t.Start(connection, /* connectionData: */ It.IsAny<string>(), /* disconnectToken: */ It.IsAny<CancellationToken>()))
                     .Returns(TaskAsyncHelper.Empty);

            transport.Setup(t => t.Send(connection, /* data: */ It.Is<string>(s => s.IndexOf("crash") >= 0), /* connectionData: */ It.IsAny<string>()))
                     .Returns(crashTcs.Task) // We want this task to never complete as the call to EnsureReconnecting will do it for us
                     .Callback(() =>
                     {
                         Task.Run(() =>
                         {
                             try
                             {
                                 // EnsureReconnecting will change the state and ultimately clear the pending invocation callbacks
                                 connection.EnsureReconnecting();
                                 testTcs.SetResult(null);
                             }
                             catch (Exception ex)
                             {
                                 testTcs.SetException(ex);
                             }
                         });
                     });

            var proxy = new HubProxy(connection, "test");

            // Act
            connection.Start(transport.Object).Wait();
            var crashTask = proxy.Invoke("crash")
                .ContinueWith(t => proxy.Invoke("test"),
                    TaskContinuationOptions.ExecuteSynchronously); // We need to ensure this executes sync so we're on the same stack

            // Assert
            Assert.Throws(typeof(AggregateException), () => crashTask.Wait());
            Assert.DoesNotThrow(() => testTcs.Task.Wait());
        }
コード例 #17
0
        public void HubCallbackClearedOnFailedInvocation()
        {
            var connection = new Mock<HubConnection>("http://foo");
            var tcs = new TaskCompletionSource<object>();

            tcs.TrySetCanceled();

            connection.Setup(c => c.Send(It.IsAny<string>())).Returns(tcs.Task);

            var hubProxy = new HubProxy(connection.Object, "foo");

            var aggEx = Assert.Throws<AggregateException>(() => { hubProxy.Invoke("foo", "arg1").Wait(); });
            var ex = aggEx.Unwrap();

            Assert.IsType(typeof(TaskCanceledException), ex);

            Assert.Equal(connection.Object._callbacks.Count, 0);
        }
コード例 #18
0
        public void InvokeEventRaisesEventWithData()
        {
            var connection = new Mock<IHubConnection>();
            connection.SetupGet(x => x.JsonSerializer).Returns(new JsonSerializer());

            var hubProxy = new HubProxy(connection.Object, "foo");
            bool eventRaised = false;

            hubProxy.On<int>("foo", (i) =>
            {
                eventRaised = true;
                Assert.Equal(1, i);
            });

            hubProxy.InvokeEvent("foo", new[] { JToken.FromObject(1) });
            Assert.True(eventRaised);
        }
コード例 #19
0
        public void InvokeEventRaisesEvent()
        {
            var connection = new Mock<IHubConnection>();
            connection.SetupGet(x => x.JsonSerializer).Returns(new JsonSerializer());

            var hubProxy = new HubProxy(connection.Object, "foo");
            bool eventRaised = false;

            hubProxy.On("foo", () =>
            {
                eventRaised = true;
            });

            hubProxy.InvokeEvent("foo", new JToken[] { });
            Assert.True(eventRaised);
        }
コード例 #20
0
ファイル: HubConnection.cs プロジェクト: hallco978/SignalR
        /// <summary>
        /// Creates an <see cref="IHubProxy"/> for the hub with the specified name.
        /// </summary>
        /// <param name="hubName">The name of the hub.</param>
        /// <returns>A <see cref="IHubProxy"/></returns>
        public IHubProxy CreateHubProxy(string hubName)
        {
            if (State != ConnectionState.Disconnected)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_ProxiesCannotBeAddedConnectionStarted));
            }

            HubProxy hubProxy;
            if (!_hubs.TryGetValue(hubName, out hubProxy))
            {
                hubProxy = new HubProxy(this, hubName);
                _hubs[hubName] = hubProxy;
            }
            return hubProxy;
        }
コード例 #21
0
        public void InvokeWorksWithNullArgument()
        {
            var connection = new Mock<IHubConnection>();
            connection.Setup(c => c.RegisterCallback(It.IsAny<Action<HubResult>>()))
                      .Callback<Action<HubResult>>(callback =>
                      {
                          callback(new HubResult());
                      });

            connection.Setup(m => m.Send(It.IsAny<string>())).Returns(TaskAsyncHelper.Empty);
            connection.SetupGet(x => x.JsonSerializer).Returns(new JsonSerializer());

            var hubProxy = new HubProxy(connection.Object, "foo");

            var o = new NullInvokeTest { Name = null, Number = 42, Strings = new[] { "Kazimierz", null, "Tetmajer" } };
            hubProxy.Invoke("method", 1, null, new[] { "a", "b" }, o);

            connection.Verify(
                c => c.Send(@"{""I"":null,""H"":""foo"",""M"":""method""," +
                @"""A"":[1,null,[""a"",""b""],{""Name"":null,""Number"":42,""Strings"":[""Kazimierz"",null,""Tetmajer""]}]}"),
                Times.Once());
        }