Exemplo n.º 1
0
        public void Can_receive_and_process_same_reply_responses()
        {
            var called = 0;
            var mqHost = CreateMqServer();

            using (var conn = mqHost.ConnectionFactory.CreateConnection())
                using (var channel = conn.CreateModel())
                {
                    channel.PurgeQueue <Incr>();
                }

            mqHost.RegisterHandler <Incr>(m => {
                Debug.WriteLine("In Incr #" + m.GetBody().Value);
                Interlocked.Increment(ref called);
                return(m.GetBody().Value > 0 ? new Incr {
                    Value = m.GetBody().Value - 1
                } : null);
            });

            mqHost.Start();

            var mqClient = mqHost.CreateMessageQueueClient();

            var incr = new Incr {
                Value = 5
            };

            mqClient.Publish(incr);

            Thread.Sleep(10000);

            Assert.That(called, Is.EqualTo(1 + incr.Value));
        }
Exemplo n.º 2
0
        public void Can_receive_and_process_same_reply_responses()
        {
            var called = 0;

            using (var mqServer = CreateMqServer()) {
                RabbitMqConfig.UsingChannel(mqServer.ConnectionFactory, channel => channel.PurgeQueue <Incr>());

                Incr ProcessMessageFn(IMqMessage <Incr> m)
                {
                    Console.WriteLine("In Incr #" + m.GetBody().Value);
                    Interlocked.Increment(ref called);
                    return(m.GetBody().Value > 0 ? new Incr {
                        Value = m.GetBody().Value - 1
                    } : null);
                }

                mqServer.RegisterHandler <Incr>(ProcessMessageFn);

                mqServer.Start();

                var incr = new Incr {
                    Value = 5
                };
                using (var mqClient = mqServer.CreateMessageQueueClient()) {
                    mqClient.Publish(incr);
                }

                Thread.Sleep(1000);
                Assert.That(called, Is.EqualTo(1 + incr.Value));
            }
        }
Exemplo n.º 3
0
        static void Main()
        {
            // Создать делегат Incr, ссылающийся на лямбда-выражение.
            // увеличивающее свой параметр на 2.
            Incr incr = count => count + 2;

            // А теперь использовать лямбда-выражение incr.
            Console.WriteLine("Использование лямбда-выражения incr: ");
            int x = -10;

            while (x <= 0)
            {
                Console.Write(x + " ");
                x = incr(x); // увеличить значение x на 2
            }

            Console.WriteLine("\n");

            // Создать экземпляр делегата IsEven, ссылающийся на лямбда-выражение,
            // возвращающее логическое значение true, если его параметр имеет четное
            // значение, а иначе — логическое значение false.
            IsEven isEven = n => n % 2 == 0;

            // А теперь использовать лямбда-выражение isEven.
            Console.WriteLine("Использование лямбда - выражения isEven: ");
            for (int i = 1; i <= 10; i++)
            {
                if (isEven(i))
                {
                    Console.WriteLine(i + " четное.");
                }
            }
        }
Exemplo n.º 4
0
        static void Main()
        {
            // Create an Incr delegate instance that refers to
            // a lambda expression that increases its parameter by 2.
            Incr incr = count => count + 2;

            //  Incr incr = (int count) => count + 2; // You can specify the type of count if you want
            // Now, use the incr lambda expression.
            Console.WriteLine("Use incr lambda expression: ");
            int x = -10;

            while (x <= 0)
            {
                Console.Write(x + " ");
                x = incr(x);     // increase x by 2
            }
            Console.WriteLine("\n");
            // Create an IsEven delegate instance that refers to
            // a lambda expression that returns true if its parameter
            // is even and false otherwise.
            IsEven isEven = n => n % 2 == 0;

            // Now, use the isEven lambda expression.
            Console.WriteLine("Use isEven lambda expression: ");
            for (int i = 1; i <= 10; i++)
            {
                if (isEven(i))
                {
                    Console.WriteLine(i + " is even.");
                }
            }
        }     // end Main
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Incr incr = count => count + 2;

            Console.WriteLine("Use incr lambda Expression: ");
            int x = -10;

            while (x <= 0)
            {
                Console.WriteLine(x + " ");
                x = incr(x);
            }

            Console.WriteLine("\n");

            IsEven isEven = n => n % 2 == 0;

            Console.WriteLine("Use isEven lambda expression:");

            for (int i = 1; i <= 10; i++)
            {
                if (isEven(i))
                {
                    Console.WriteLine(i + " is even");
                }
            }
        }
        public void Can_receive_and_process_same_reply_responses()
        {
            var mqHost = CreateMqServer();
            var called = 0;

            mqHost.RegisterHandler <Incr>(m => {
                Debug.WriteLine("In Incr #" + m.GetBody().Value);
                called++;
                return(m.GetBody().Value > 0 ? new Incr {
                    Value = m.GetBody().Value - 1
                } : null);
            });

            mqHost.Start();

            var mqClient = mqHost.CreateMessageQueueClient();

            var incr = new Incr {
                Value = 5
            };

            mqClient.Publish(incr);

            Thread.Sleep(1000);

            Assert.That(called, Is.EqualTo(1 + incr.Value));
        }
Exemplo n.º 7
0
 public override void Visit(Incr incr)
 {
     Visit(new Ass(incr.Id, new AddOp {
         Add = Add.Plus, Lhs = new ID {
             Id = incr.Id
         }, Rhs = new Int {
             Value = 1
         }
     }));
 }
Exemplo n.º 8
0
        public static void lambda()
        {
            Incr incr = (count, b) => count + b;

            Console.WriteLine(incr(-10, 5));

            IsEven ie = count => count % 2 == 0;


            Console.WriteLine(ie(2));
        }
        public void Can_serialize_IMqMessage_into_typed_Message()
        {
            var dto = new Incr {
                Value = 1
            };
            var iMsg      = MqMessageFactory.Create(dto);
            var jsonBytes = MqMessageExtensions.ToJsonBytes((object)iMsg);
            IMqMessage <Incr> typedMessage = MqMessageExtensions.FromJsonBytes <MqMessage <Incr> >(jsonBytes);

            Assert.That(typedMessage.GetBody().Value, Is.EqualTo(dto.Value));
        }
Exemplo n.º 10
0
        public void SML_Instruction_Incr_Bad_Stack()
        {
            Mock <IVirtualMachine> mock = new Mock <IVirtualMachine>(MockBehavior.Strict);
            Incr incr = new Incr
            {
                VirtualMachine = mock.Object
            };

            mock.Setup(m => m.Stack).Returns(new Stack());

            incr.Run();
        }
Exemplo n.º 11
0
        public void SML_Instruction_Incr_Bad_Item()
        {
            string inputOne = "2";

            Stack stack = new Stack();
            Mock <IVirtualMachine> mock = new Mock <IVirtualMachine>(MockBehavior.Strict);
            Incr incr = new Incr
            {
                VirtualMachine = mock.Object
            };

            mock.Setup(m => m.Stack).Returns(stack);

            stack.Push(inputOne);

            incr.Run();
        }
Exemplo n.º 12
0
        public static void Demo()
        {
            X x = new X();
            Y y = new Y();

            Console.WriteLine("Xob= {0} Yob= {1}", x.val, y.val);

            // вместо Y теперь принимает Х
            // (контравариантность)
            Incr delegIncr = IncrX;

            x = delegIncr(y);

            // вместо возвращаемого Х возвращаем Y
            // (ковариантность)
            delegIncr = IncrY;
            y         = (Y)delegIncr(y);
            Console.WriteLine("Xob= {0} Yob= {1}", x.val, y.val);
        }
        public static void SimpleExpressionLambdaDemo()
        {
            Incr incr = count => count + 2; // here compler infer the type of count from delegate signature type

            int x = -10;

            Console.WriteLine("Practicing Increment With Expression Lambda ");
            do
            {
                Console.WriteLine("Current value of X :" + x);
                x = incr(x);
            } while (x <= 0);

            Console.WriteLine("\n\n");
            Console.WriteLine("Practicing if provided number is Even with Expression Lambda ");
            Iseven iseven = value => value % 2 == 0;
            Incr   incr2  = (int count) => count + 2;

            do
            {
                Console.WriteLine("Current value of X :" + x);
                if (iseven(x))
                {
                    Console.WriteLine(x + " is even");
                }

                x = incr2(x);
            } while (x <= 10);


            Console.WriteLine("Practicing cheking if value is in range with Expression Lambda ");
            Inrange inrange = (l, h, v) => v >= l && v <= h;

            if (inrange(2, 5, 3))
            {
                Console.WriteLine("3  is between 2 and 5");
            }
            else
            {
                Console.WriteLine("3  isn't between 2 and 5");
            }
        }
Exemplo n.º 14
0
        public void SML_Instruction_Incr_Good()
        {
            int inputOne = 2;
            int expected = inputOne + 1;

            Stack stack = new Stack();
            Mock <IVirtualMachine> mock = new Mock <IVirtualMachine>(MockBehavior.Strict);
            Incr incr = new Incr
            {
                VirtualMachine = mock.Object
            };

            mock.Setup(m => m.Stack).Returns(stack);

            stack.Push(inputOne);

            incr.Run();

            Assert.AreEqual(mock.Object.Stack.Pop(), expected);
        }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            //создать делегат Incr,ссылающийся на лямбда-выражение,увеличивающее свой параметр на 2
            Incr incr = count => count + 2;

            //Incr incr = (int count) => count + 2; альтернативный способ объявления делегата
            //использование лямбда-выражение incr
            Console.WriteLine("Использование лямбда-выражения incr: ");
            int x = -10;

            while (x <= 0)
            {
                Console.Write(x + " ");
                x = incr(x); //увеличить значение x на 2
            }
            Console.WriteLine();
            //создать экземпляр делегата IsEven,ссылающийся на лямбда-выражение,возвращающее результат типа bool
            IsEven isEven = n => n % 2 == 0;

            //использование лямбда-выражение isEven
            Console.WriteLine("Использование лямбда-выражения isEven: ");
            for (int i = 1; i <= 10; i++)
            {
                if (isEven(i))
                {
                    Console.WriteLine(i + " чётное");
                }
            }
            //создать экземпляр делегата InRange,ссылающийся на лямбда-выражение,возвращающее результат типа bool
            InRange rangeOK = (low, high, val) => val >= low && val <= high;

            //использование лямбда-выражение rangeOK
            if (rangeOK(1, 5, 3))
            {
                Console.WriteLine("Число 3 находится в пределах от 1 до 5");
            }
        }
Exemplo n.º 16
0
            static void Main()
            {
                Incr incr = count => count + 2;

                Console.WriteLine("Использование лямбда-выражения incr: ");
                int x = -10;

                while (x <= 0)
                {
                    Console.Write(x + " ");
                    x = incr(x);
                }
                Console.WriteLine("\n");
                IsEven isEven = n => n % 2 == 0;

                Console.WriteLine("Использование лямбда-выражения isEven: ");
                for (int i = 0; i <= 10; i++)
                {
                    if (isEven(i))
                    {
                        Console.WriteLine(i + " четное.");
                    }
                }
            }
Exemplo n.º 17
0
		public void Can_receive_and_process_same_reply_responses()
		{
			mqHost = CreateMqHost();
			var called = 0;

			mqHost.RegisterHandler<Incr>(m => {
				Debug.WriteLine("In Incr #" + m.GetBody().Value);
				called++;
				return m.GetBody().Value > 0 ? new Incr { Value = m.GetBody().Value - 1 } : null;
			});

			mqHost.Start();

			var mqClient = mqHost.CreateMessageQueueClient();

			var incr = new Incr { Value = 5 };
			mqClient.Publish(incr);

			Thread.Sleep(2000);
            mqHost.Dispose();

			Assert.That(called, Is.EqualTo(1 + incr.Value));
        }
Exemplo n.º 18
0
 public static Incr Merge(Incr a, Incr b) => a.Valid && b.Valid && a.R < b.L ? new Incr()
 {
     L = a.L, R = b.R, Valid = true
 } : new Incr {
     Valid = false
 };
Exemplo n.º 19
0
 public virtual void Visit(Incr incr)
 {
 }
        public void Can_receive_and_process_same_reply_responses()
        {
            var called = 0;
            var mqHost = CreateMqServer();

            using (var conn = mqHost.ConnectionFactory.CreateConnection())
            using (var channel = conn.CreateModel())
            {
                channel.PurgeQueue<Incr>();
            }

            mqHost.RegisterHandler<Incr>(m => {
                Debug.WriteLine("In Incr #" + m.GetBody().Value);
                called++;
                return m.GetBody().Value > 0 ? new Incr { Value = m.GetBody().Value - 1 } : null;
            });

            mqHost.Start();

            var mqClient = mqHost.CreateMessageQueueClient();

            var incr = new Incr { Value = 5 };
            mqClient.Publish(incr);

            Thread.Sleep(10000);

            Assert.That(called, Is.EqualTo(1 + incr.Value));
        }
Exemplo n.º 21
0
        public void Can_receive_and_process_same_reply_responses()
        {
            var called = 0;
            var mqHost = CreateMqServer();

            sqsQueueManager.PurgeQueues(QueueNames<Incr>.AllQueueNames);

            mqHost.RegisterHandler<Incr>(m =>
            {
                Debug.WriteLine("In Incr #" + m.GetBody().Value);
                called++;
                return m.GetBody().Value > 0
                    ? new Incr { Value = m.GetBody().Value - 1 }
                    : null;
            });

            mqHost.Start();

            var mqClient = mqHost.MessageFactory.CreateMessageQueueClient();

            var incr = new Incr
            {
                Value = 5
            };
            mqClient.Publish(incr);

            SqsTestAssert.WaitUntilTrueOrTimeout(() => called >= incr.Value + 1);
            
            Assert.That(called, Is.EqualTo(1 + incr.Value));
        }