Ejemplo n.º 1
0
        /// <summary>
        /// 绑定方法到当前连接
        /// </summary>
        /// <param name="methodName">方法名</param>
        /// <param name="parameterTypes">参数类型</param>
        /// <param name="returnType">返回值类型</param>
        /// <param name="handler">方法的具体实现</param>
        /// <returns>IDisposable</returns>
        public IDisposable On(string methodName, Type[] parameterTypes, Type returnType, Func <object[], object> handler)
        {
            CheckDisposed();
            Log.Debug($"Registering Handler methodName={methodName}");

            var invocationHandler = new InvocationHandler(parameterTypes, returnType, handler);

            if (_handlers.TryGetValue(methodName, out InvocationHandlerList invocationList))
            {
                lock (invocationList)
                {
                    invocationList.Add(invocationHandler);
                }
            }
            else
            {
                invocationList = new InvocationHandlerList(invocationHandler);
                lock (_handlers)
                {
                    _handlers.Add(methodName, invocationList);
                }
            }

            return(new Subscription(invocationHandler, invocationList));
        }
Ejemplo n.º 2
0
        public async Task When_Disposed_Should_RemoveHandlerFromListTwice()
        {
            // Arrange
            var result                = 0;
            var invocationHandler     = new InvocationHandler(args => Task.Run(() => result++));
            var invocationHandlerList = new InvocationHandlerList(invocationHandler, invocationHandler);
            var subscription          = new Subscription(invocationHandler, invocationHandlerList);

            // Act
            await invocationHandlerList.InvokeAsync(null);

            await invocationHandlerList.InvokeAsync(null);

            await invocationHandlerList.InvokeAsync(null);

            subscription.Dispose();

            await invocationHandlerList.InvokeAsync(null);

            await invocationHandlerList.InvokeAsync(null);

            subscription.Dispose();

            await invocationHandlerList.InvokeAsync(null);

            await invocationHandlerList.InvokeAsync(null);

            // Assert
            Assert.Equal(8, result);
        }
Ejemplo n.º 3
0
        public async Task When_InvokeAsync_InvokeAllCallback()
        {
            // Arrange
            var callbackResult_A = false;
            var callbackResult_B = false;
            var callbackResult_C = false;

            var invocationHandler_A   = new InvocationHandler(args => Task.Run(() => callbackResult_A = true));
            var invocationHandler_B   = new InvocationHandler(args => Task.Run(() => callbackResult_B = true));
            var invocationHandler_C   = new InvocationHandler(args => Task.Run(() => callbackResult_C = true));
            var invocationHandlerList = new InvocationHandlerList(invocationHandler_A, invocationHandler_B, invocationHandler_C);

            // Act
            await invocationHandlerList.InvokeAsync(null);

            // Assert
            Assert.True(callbackResult_A);
            Assert.True(callbackResult_B);
            Assert.True(callbackResult_C);
        }
Ejemplo n.º 4
0
        public async Task When_InvokeAsync_ShouldPassParametersToAll()
        {
            // Arrange
            var parameters       = new object[] { 1, 2, 3 };
            var callbackResult_A = new object[] { -1 };
            var callbackResult_B = new object[] { -1 };
            var callbackResult_C = new object[] { -1 };

            var invocationHandler_A   = new InvocationHandler(args => Task.Run(() => callbackResult_A = parameters));
            var invocationHandler_B   = new InvocationHandler(args => Task.Run(() => callbackResult_B = parameters));
            var invocationHandler_C   = new InvocationHandler(args => Task.Run(() => callbackResult_C = parameters));
            var invocationHandlerList = new InvocationHandlerList(invocationHandler_A, invocationHandler_B, invocationHandler_C);

            // Act
            await invocationHandlerList.InvokeAsync(null);

            // Assert
            Assert.Equal(callbackResult_A, parameters);
            Assert.Equal(callbackResult_B, parameters);
            Assert.Equal(callbackResult_C, parameters);
        }
Ejemplo n.º 5
0
 public Subscription(InvocationHandler handler, InvocationHandlerList handlerList)
 {
     _handler     = handler;
     _handlerList = handlerList;
 }