Ejemplo n.º 1
0
        public void GetSingletone()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ResolverTests>();
            var toResolve = resolver.CreateSession().Resolve <ToResolve2>();

            Assert.IsNotNull(toResolve);

            toResolve.Index = 2;

            toResolve = resolver.CreateSession().Resolve <ToResolve2>();
            Assert.IsNotNull(toResolve);
            Assert.AreEqual(2, toResolve.Index);
        }
Ejemplo n.º 2
0
        public async Task CalculatorAddDuoTest()
        {
            var handlerProvider = new HandlerProvider();
            var resolver        = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handlerProvider.LoadFromReference <HandlingTests>();

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

            //var sum = InterfaceHandling.CreateHandler<ICalculator, InterfaceHandlerTest<ICalculator>>(new HandlerArgument() { Arg = "5" });
            var value = await handlerProvider.HandleAsync <CalculatorResult>(resolver.CreateSession(), "", 1,
                                                                             new CalculatorArgument()
            {
                Left = 2, Right = 3
            },
                                                                             new CalculatorArgument()
            {
                Left = 4, Right = 5
            });

            var result = (CalculatorResult)value;

            Assert.AreEqual(24, result.Result);
        }
Ejemplo n.º 3
0
        public void GetFromParent()
        {
            var parent   = new Resolver();
            var resolver = new Resolver(parent);


            parent.LoadFromReference <ResolverTests>();

            var toResolve = resolver.ResolveSingletone <ToResolve2>();

            Assert.IsNotNull(toResolve);

            toResolve.Index = 5;

            toResolve = parent.ResolveSingletone <ToResolve2>();
            Assert.IsNotNull(toResolve);
            Assert.AreEqual(5, toResolve.Index);

            toResolve = parent.CreateSession().Resolve <ToResolve2>();
            Assert.IsNotNull(toResolve);
            Assert.AreEqual(5, toResolve.Index);

            toResolve = resolver.CreateSession().Resolve <ToResolve2>();
            Assert.IsNotNull(toResolve);
            Assert.AreEqual(5, toResolve.Index);
        }
Ejemplo n.º 4
0
        public async Task HandleManySum()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

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

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < 100000; i++)
            {
                //var result = await handler.HandleAsync(0, new IntArgument() { Left = 2, Right = 3 });
                //var result = await handler.HandleAsync(0, new IntArgument() { Left = 2, Right = 3 });

                var value = await handler.HandleAsync <CalculatorResult>(resolver.CreateSession(), 1, new object[] {
                    new CalculatorArgument()
                    {
                        Left = 2, Right = 3
                    },
                });
            }

            watch.Stop();
            // s: 100k 0.59s
        }
Ejemplo n.º 5
0
        public async Task HandleSumThree()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

            var result = await handler.HandleAsync <int>(resolver.CreateSession(), 0, new object[] {
                new IntArgument()
                {
                    Left = 2, Right = 3
                },
                new IntArgument()
                {
                    Left = 4, Right = 5
                },
                new ByteArgument {
                    Left = 6, Right = 7
                }
            });

            Assert.IsNotNull(result);
            Assert.IsTrue(result is int);
            Assert.IsTrue((int)result == (2 + 3 + 4 + 5 + 6 + 7));
        }
Ejemplo n.º 6
0
        public IResolverSession GetProtocolResolver(IPacketStream packetStream, CancellationToken cancellationToken)
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ProtocolTests>();
            var session = resolver.CreateSession();

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

            handler.LoadFromReference <ProtocolTests>();

            var converter = new Serializer();

            converter.LoadFromReference <ProtocolTests>();

            var protocol = new SocketProtocolHandler(session, packetStream, converter, handler);

            session.Resolver.Register(protocol);
            session.Resolver.Register(handler);
            session.Resolver.Register(converter);

            var listenTask = protocol.ListenAsync(cancellationToken);

            return(session);
        }
Ejemplo n.º 7
0
        public void GetInheritThrow()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ResolverTests>();
            var toResolve = resolver.CreateSession().Resolve <NotIncludedResolve>();

            Assert.IsNotNull(toResolve);
            Assert.IsInstanceOfType(toResolve, typeof(ToResolve4));
        }
Ejemplo n.º 8
0
        public void CreateInstance()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ResolverTests>();
            var toResolve = resolver.CreateSession().CreateInstance <ToResolve>();

            Assert.IsNotNull(toResolve);
            Assert.IsNotNull(toResolve.Resolver);
            Assert.IsNotNull(toResolve.ToResolve2);
            Assert.IsNotNull(toResolve.ToResolve2.Resolver);
            Assert.IsNotNull(toResolve.ToResolve2.ToResolve4);
        }
Ejemplo n.º 9
0
        public async Task HandleSumString()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

            var result = await handler.HandleAsync <string>(resolver.CreateSession(), 0, "s");

            Assert.IsNotNull(result);
            Assert.IsTrue(result is string);
            Assert.IsTrue((string)result == "sums");
        }
Ejemplo n.º 10
0
        public async Task HandleSumZero()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

            var result = await handler.HandleAsync <int>(resolver.CreateSession(), 0, new object[] { });

            Assert.IsNotNull(result);
            Assert.IsTrue(result is int);
            Assert.IsTrue((int)result == 1);
        }
        public async Task SumInterfaceHandlingFromResolver()
        {
            Resolver resolver = new Resolver();

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

            var sum   = resolver.CreateSession().CreateHandler <ISum, InterfaceHandlerTest <ISum> >();
            var value = await sum.Sum("opa", "o");

            Assert.AreEqual("Sumopao5", value);
        }
Ejemplo n.º 12
0
        public void DisposeNotSingletoneWithNotSingletone()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ResolverTests>();
            ToResolve3 toResolve;

            using (var session = resolver.CreateSession())
            {
                toResolve = session.Resolve <ToResolve3>();
            }

            Assert.IsTrue(toResolve.IsDisposed);
            Assert.IsTrue(toResolve.ToResolve4.IsDisposed);

            resolver.Dispose();
        }
Ejemplo n.º 13
0
        public async Task HandleMinusAsyncWithRole()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

            var result = await handler.HandleAsync <int>(resolver.CreateSession(), 2, new IntArgument()
            {
                Left = 2, Right = 3
            });

            Assert.IsNotNull(result);
            Assert.IsTrue(result is int);
            Assert.IsTrue((int)result == -1);
        }
Ejemplo n.º 14
0
        public async Task CalculatorAddStringTest()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

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

            //var sum = InterfaceHandling.CreateHandler<ICalculator, InterfaceHandlerTest<ICalculator>>(new HandlerArgument() { Arg = "5" });
            var result = await handler.HandleAsync <CalculatorResult>(resolver.CreateSession(), "IAddString", 1, "6");

            Assert.AreEqual(7, result.Result);
        }
Ejemplo n.º 15
0
        public async Task <IResolverSession> StartClientAsync(string host, int port, CancellationToken cancellationToken)
        {
            if (!loaded)
            {
                throw new Exception($"Call InitializeFromReference first");
            }

            var client  = new TcpClient();
            var session = Resolver.CreateSession();

            session.Resolver.Register(client, false);
            session.Resolver.AddDisposable(client);

            await client.ConnectAsync(host, port);

            ClientListenAsync(client, session, true, cancellationToken);

            return(session);
        }
Ejemplo n.º 16
0
        public async Task StartServerAsync(string ipString, int port, CancellationToken cancellationToken)
        {
            if (!loaded)
            {
                throw new Exception($"Call InitializeFromReference first");
            }

            var ipAddress = IPAddress.Parse(ipString);
            var listener  = new TcpListener(ipAddress, port);

            listener.Start();
            try
            {
                using (var register = cancellationToken.Register(() => listener.Stop()))
                {
                    do
                    {
                        var client = await listener.AcceptTcpClientAsync();

                        try
                        {
                            var session = Resolver.CreateSession();
                            session.Resolver.Register(client, false);
                            session.Resolver.AddDisposable(client);
                            ClientListenAsync(client, session, false, cancellationToken);
                        }
                        catch
                        {
                            try
                            {
                                client.Dispose();
                            }
                            catch { };
                        }
                    } while (!cancellationToken.IsCancellationRequested);
                }
            }
            finally
            {
                listener.Stop();
            }
        }
Ejemplo n.º 17
0
        public void CreateManyInstanceManySession()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <ResolverTests>();

            Stopwatch watch = Stopwatch.StartNew();

            for (int i = 0; i < 100000; i++)
            {
                using (var session = resolver.CreateSession())
                {
                    var toResolve = session.CreateInstance <ToResolve>();
                }
            }

            resolver.Dispose();

            watch.Stop();
        }
Ejemplo n.º 18
0
        public async Task HandleManyMultiplyAsync()
        {
            var handler  = new HandlerProvider();
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();
            handler.LoadFromReference <HandlingTests>();

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < 100000; i++)
            {
                var result = await handler.HandleAsync <int>(resolver.CreateSession(), 1, new IntArgument()
                {
                    Left = 2, Right = 3
                });
            }

            watch.Stop();
            // 0.48s (0.8) 100k
        }
Ejemplo n.º 19
0
        public async Task HandlerByteMinusAsyncWithAdminMinusRoles()
        {
            var resolver = new Resolver();

            resolver.LoadFromReference <HandlingTests>();

            var handler = new HandlerProvider();

            handler.LoadFromReference <HandlingTests>();

            resolver.Register(new Roles("Minus", "Admin"));

            var result = await handler.HandleAsync <int>(resolver.CreateSession(), 2, new ByteArgument()
            {
                Left = 6, Right = 2
            });

            Assert.IsNotNull(result);
            Assert.IsTrue(result is int);
            Assert.IsTrue((int)result == 3);
        }
Ejemplo n.º 20
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
        }