Exemplo n.º 1
0
        private async Task <TInterface[]> GetConnections <TInterface>(TcpListener listener, CancellationToken cancellationToken) where TInterface : class
        {
            var       socketTask = listener.AcceptTcpClientAsync();
            TcpClient tcpClient  = new TcpClient("127.0.0.1", ((IPEndPoint)listener.LocalEndpoint).Port);

            var tcpServer = await socketTask;

            //var serverPacketStream = new TestPacketStream();
            //var clientPacketStream = new TestPacketStream();
            //serverPacketStream.Connected(clientPacketStream);
            //clientPacketStream.Connected(serverPacketStream);

            //var resolver1 = GetProtocolResolver(clientPacketStream, true); //tcpClient.GetStream());
            //var resolver2 = GetProtocolResolver(serverPacketStream); // tcpServer.GetStream());

            var resolver1 = GetProtocolResolver(tcpClient.GetStream());
            var resolver2 = GetProtocolResolver(tcpServer.GetStream());


            resolver1.Resolver.Register(new CalculatorParameter()
            {
                First = 10
            });
            resolver2.Resolver.Register(new CalculatorParameter()
            {
                First = 10
            });

            var calculator1 = InterfaceHandling.CreateHandler <TInterface, ProtocolInterfaceHandler <TInterface> >(resolver1);
            var calculator2 = InterfaceHandling.CreateHandler <TInterface, ProtocolInterfaceHandler <TInterface> >(resolver2);

            return(new TInterface[] { calculator1, calculator2 });
        }
        public async Task SumInterfaceHandlingFromArguments()
        {
            var sum = InterfaceHandling.CreateHandler <ISum, InterfaceHandlerTest <ISum> >(new HandlerArgument()
            {
                Arg = "5"
            });
            var value = await sum.Sum("opa", "o");

            Assert.AreEqual("Sumopao5", value);
        }
 //[ExpectedException(typeof(NotImplementedException))]
 public async Task SumPropertyThrowNotImplemented()
 {
     Assert.ThrowsAsync <NotImplementedException>(async() =>
     {
         var sum = InterfaceHandling.CreateHandler <ISumArg, InterfaceHandlerTest <ISumArg> >(new HandlerArgument()
         {
             Arg = "5"
         });
         var value = sum.Prop;
     });
 }
        public async Task SumArgInterfaceHandlingFromArguments()
        {
            var sum = InterfaceHandling.CreateHandler <ISumArg, InterfaceHandlerTest <ISumArg> >(new HandlerArgument()
            {
                Arg = "5"
            });
            var value = await sum.Sum(20, new SumArgument()
            {
                Param2 = "a"
            });

            //var value = await sum.Sum(20, "10");
            Assert.AreEqual("Sum20Param: a5", value);
        }
 public async Task SumIntResultThrow()
 {
     try
     {
         var sum = InterfaceHandling.CreateHandler <ISumIntResult, InterfaceHandlerTest <ISumIntResult> >(new HandlerArgument()
         {
             Arg = "5"
         });
         throw new Exception("Should throw exception");
     }
     catch (Exception e)
     {
         if (!e.Message.Contains("Handler and interface return types"))
         {
             throw;
         }
     }
 }
Exemplo n.º 6
0
        public async Task InterfaceToMethodProtocolHandlerTest()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ProtocolTests>();

            resolver.Register(new CalculatorParameter()
            {
                First = 10
            });

            var session = resolver.CreateSession();
            //resolver.Register(session);

            var handler = new HandlerProvider(session.Resolve <IMethodContextInfoProvider>());

            handler.LoadFromReference <ProtocolTests>();

            var methodHandler = new MethodProtocolHandler(handler, session);

            resolver.Register(methodHandler, true);
            var calculator = InterfaceHandling.CreateHandler <ICalculator, ProtocolInterfaceHandler <ICalculator> >(session);

            List <Task> tasks = new List <Task>();
            var         watch = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                var task = calculator.AddAsync("20");
                tasks.Add(task);
            }

            await Task.WhenAll(tasks);

            watch.Stop();

            // a: 10k 0.2-0.3s

            //Assert.AreEqual(30, task.Result);
        }
        public async Task SumInterfaceHandlingMany()
        {
            Resolver resolver = new Resolver();

            resolver.LoadFromReference <InterfaceHandlingTests>();
            resolver.Register(new HandlerArgument()
            {
                Arg = "5"
            });

            var sum = InterfaceHandling.CreateHandler <ISum, InterfaceHandlerTest <ISum> >(resolver.CreateSession());

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < 100000; i++)
            {
                var value = await sum.Sum("opa", "o");
            }

            watch.Stop();

            // 100k 0.17s
        }
        public static TInterface CreateChannel <TInterface>(this IResolverSession session) where TInterface : class
        {
            var channel = InterfaceHandling.CreateHandler <TInterface, ProtocolInterfaceHandler <TInterface> >(session);

            return(channel);
        }