예제 #1
0
        public void Nested_callbacks_should_be_able_to_round_trip()
        {
            // arrange
            m_localContainer.Register(
                Component.For<IService>().LifeStyle.Transient.At(REMOTE_ADDR, SERVICE_KEY, PROXY_KEY)
                );

            m_remoteContainer.Register(
                Component.For<IService>().Instance(m_service).Named(SERVICE_KEY)
                );

            var proxy = m_localContainer.Resolve<IService>();

            const int magicNumber = 666;
            m_service.Stub(s => s.MethodWithNestedCallback(null))
                .IgnoreArguments()
                .Call().Action<Action<Action<Action<int>>>>(cb1 => cb1(cb2 => cb2(magicNumber)));
            var result = new WaitableValue<int>();

            // act
            proxy.MethodWithNestedCallback(cb => cb(x => result.Value = x));

            // assert
            var callbackCalled = result.WaitOne(TimeSpan.FromSeconds(1));
            callbackCalled.Should().BeTrue();

            result.Value.Should().Be(magicNumber);
        }
예제 #2
0
        public void Invocation_of_callback_before_timeout_should_not_invoke_the_callback_with_timeout_context()
        {
            // arrange
            m_localContainer.Register(
                Component.For<IService>().LifeStyle.Transient.At(REMOTE_ADDR, SERVICE_KEY, PROXY_KEY)
                );

            m_remoteContainer.Register(
                Component.For<IService>().Instance(m_service).Named(SERVICE_KEY)
                );

            var proxy = m_localContainer.Resolve<IService>();

            var timeoutResult = new WaitableValue<bool>();
            var addResult = new WaitableValue<int>();
            Action<int, int, Action<int>> delayedCallbackInvocation =
                (x, y, cb) => Observable.Timer(TimeSpan.FromSeconds(0.3)).Subscribe(_ => cb(x + y));
            m_service.Stub(s => s.Add(0, 0, null))
                .IgnoreArguments()
                .Call().Action(delayedCallbackInvocation);

            // act
            MessagelessContext.Execute(
                ctx => proxy.Add(111, 222, result =>
                {
                    if (MessagelessContext.CurrentContext.CallbackTimedOut)
                        timeoutResult.Value = true;
                    else
                        addResult.Value = result;
                }),
                timeout: TimeSpan.FromSeconds(0.5));

            // assert
            var calledWithTimeoutContext = timeoutResult.WaitOne(TimeSpan.FromSeconds(1));
            calledWithTimeoutContext.Should().BeFalse();

            var calledWithResult = addResult.WaitOne(TimeSpan.FromSeconds(0.1));
            calledWithResult.Should().BeTrue();
            addResult.Value.Should().Be(111+222);
        }
예제 #3
0
        public void Ignoring_a_callback_with_timeout_should_invoke_the_callback_with_timeout_context()
        {
            // arrange
            m_localContainer.Register(
                Component.For<IService>().LifeStyle.Transient.At(REMOTE_ADDR, SERVICE_KEY, PROXY_KEY)
                );

            m_remoteContainer.Register(
                Component.For<IService>().Instance(m_service).Named(SERVICE_KEY)
                );

            var proxy = m_localContainer.Resolve<IService>();

            var result = new WaitableValue<bool>();
            Action<int, int, Action<int>> ignoreCallback = (x, y, cb) => { };
            m_service.Stub(s => s.Add(0, 0, null))
                .IgnoreArguments()
                .Call().Action(ignoreCallback);

            // act
            MessagelessContext.Execute(
                ctx => proxy.Add(111, 222, i =>
                {
                    result.Value = MessagelessContext.CurrentContext.CallbackTimedOut;
                }),
                timeout: TimeSpan.FromSeconds(0.5));

            // assert
            var calledWithTimeoutContext = result.WaitOne(TimeSpan.FromSeconds(1));
            calledWithTimeoutContext.Should().BeTrue();

            result.Value.Should().BeTrue();
        }
예제 #4
0
        public void Calling_method_with_parameterless_callback_on_proxy_should_not_fail()
        {
            // arrange
            m_localContainer.Register(
                Component.For<IService>().LifeStyle.Transient.At(REMOTE_ADDR, SERVICE_KEY, PROXY_KEY)
                );

            m_remoteContainer.Register(
                Component.For<IService>().Instance(m_service).Named(SERVICE_KEY)
                );

            var proxy = m_localContainer.Resolve<IService>();

            var result = new WaitableValue<int>();
            const int magicNumber = 666;
            m_service.Stub(s => s.MethodWithParameterlessCallback(null))
                .IgnoreArguments()
                .Call().Action<Action>(cb => cb());

            // act
            proxy.MethodWithParameterlessCallback(() => result.Value = magicNumber);

            // assert
            var callbackWasCalled = result.WaitOne(TimeSpan.FromSeconds(1));
            callbackWasCalled.Should().BeTrue();
            result.Value.Should().Be(magicNumber);
        }
예제 #5
0
        public void Calling_method_with_null_callback_on_proxy_should_not_fail()
        {
            // arrange
            m_localContainer.Register(
                Component.For<IService>().LifeStyle.Transient.At(REMOTE_ADDR, SERVICE_KEY, PROXY_KEY)
                );

            m_remoteContainer.Register(
                Component.For<IService>().Instance(m_service).Named(SERVICE_KEY)
                );

            var proxy = m_localContainer.Resolve<IService>();

            var isNullCallback = new WaitableValue<bool>();
            m_service.Stub(s => s.Add(0, 0, null))
                .IgnoreArguments()
                .Call().Action((int x, int y, Action<int> cb) => isNullCallback.Value = (cb == null));

            // act
            proxy.Add(111, 222, null);

            // assert
            var methodWasCalled = isNullCallback.WaitOne(TimeSpan.FromSeconds(1));
            methodWasCalled.Should().BeTrue();

            isNullCallback.Value.Should().BeTrue();
        }
예제 #6
0
        public void Calling_generic_method_on_proxy_should_be_propogated_to_remote_service()
        {
            // arrange
            m_localContainer.Register(
                Component.For<IService>().LifeStyle.Transient.At(REMOTE_ADDR, SERVICE_KEY, PROXY_KEY)
                );

            m_remoteContainer.Register(
                Component.For<IService>().Instance(m_service).Named(SERVICE_KEY)
                );

            var proxy = m_localContainer.Resolve<IService>();

            var waitableValue = new WaitableValue<int>();
            const int magicNumber = 666;
            m_service.Stub(s => s.GenericMethod(magicNumber))
                .Call().Action<int>(arg => { waitableValue.Value = arg; });

            // act
            proxy.GenericMethod(magicNumber);

            // assert
            var methodCalled = waitableValue.WaitOne(TimeSpan.FromSeconds(1));
            methodCalled.Should().BeTrue();
            waitableValue.Value.Should().Be(magicNumber);
        }