示例#1
0
 public AsyncServiceOperation()
 {
     this.OnDone = () =>
     {
         Mre.Set();
     };
 }
示例#2
0
            public void Start(IPEndPoint remoteEndPoint)
            {
                if (Client != null)
                {
                    return;
                }
                Client = new Net.BlockingSocketClient();
                Client.Disconnected += (c, ep, e) =>
                {
                    Exception.Value = e;
                };
                Client.MessageReady += (c) =>
                {
                    lock (_lock)
                    {
                        if (c.MessageCount > 0)
                        {
                            var response = c?.GetNextMessage();
                            if (response == null)
                            {
                                System.Diagnostics.Debugger.Break();
                            }

                            var message = response.AsString();
                            if (message == Request.Value.ToUpper())
                            {
                                Responses.Value += 1;
                            }
                            else
                            {
                                Exception.Value = new System.Exception("Message Lost");
                            }
                        }
                        Mre.Set();
                    }
                };
                Client.ConnectAsync(remoteEndPoint).ContinueWith(task =>
                {
                    Mre.Reset();
                    _thread = W.Threading.Thread.Create(cts =>
                    {
                        while (!cts.IsCancellationRequested)
                        {
                            if (Client.IsConnected)
                            {
                                Request.Value   = string.Format("Message: {0}", Requests);
                                Requests.Value += 1;
                                Mre.Reset();
                                Client.Send(Request.Value.AsBytes());
                                Mre.Wait();
                            }
                            //W.Threading.Thread.Sleep(1);
                        }
                        if (Client.IsConnected)
                        {
                            Client.Disconnect();
                        }
                    });
                });
            }
示例#3
0
        public void AnyOperator()
        {
            Order order = GetOrder();
            //order.Items.Any(a => a.ItemCode == "test")
            Rule rule = new Rule
            {
                MemberName = "Items",// The array property
                Operator   = "Any",
                Rules      = new[]
                {
                    new Rule
                    {
                        MemberName  = "ItemCode", // the property in the above array item
                        Operator    = "Equal",
                        TargetValue = "Test",
                    }
                }
            };
            Mre  engine     = new Mre();
            var  boolMethod = engine.CompileRule <Order>(rule);
            bool passes     = boolMethod(order);

            Assert.IsTrue(passes);

            var item = order.Items.First(x => x.ItemCode == "Test");

            item.ItemCode = "Changed";
            passes        = boolMethod(order);
            Assert.IsFalse(passes);
        }
示例#4
0
 public void WaitCompletion()
 {
     Mre.WaitOne();
     if (Exception != null)
     {
         Assert.Fail(Exception.ToString());
     }
     Mre.Reset();
 }
示例#5
0
        public void BadPropertyName()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("NotAProperty", MreOperator.Equal, 1);

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(false);       // should not get here.
        }
示例#6
0
        public void BadMethod()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.MethodOnChild("Customer.FirstName", "NotAMethod", "ohn");

            Mre  engine = new Mre();
            var  c1123  = engine.CompileRule <Order>(rule);
            bool passes = c1123(order);

            Assert.IsTrue(false);       // should not get here.
        }
示例#7
0
        public void NotACollection()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("Customer[1]", MreOperator.Equal, "123");

            Mre  engine = new Mre();
            var  c1123  = engine.CompileRule <Order>(rule);
            bool passes = c1123(order);

            Assert.IsTrue(false);       // should not get here.
        }
示例#8
0
        public void EnumAsWord()
        {
            Order order = ExampleUsage.GetOrder();

            order.Total = null;
            Rule rule = Rule.Create("Status", MreOperator.Equal, "Open");

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);
        }
示例#9
0
        public void IsFloat_Bad_Word()
        {
            var target = new IsTypeClass {
                NumAsString = "1234.567", OtherField = "Hello, World"
            };

            Rule rule         = Rule.IsFloat("OtherField");
            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <IsTypeClass>(rule);
            bool passes       = compiledRule(target);

            Assert.IsFalse(passes);
        }
示例#10
0
        public void IsDouble_OK_Int()
        {
            var target = new IsTypeClass {
                NumAsString = "1234", OtherField = "Hello, World"
            };

            Rule rule         = Rule.IsDouble("NumAsString");
            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <IsTypeClass>(rule);
            bool passes       = compiledRule(target);

            Assert.IsTrue(passes);
        }
示例#11
0
        public void ChildProperties2()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("Customer.Country.CountryCode", MreOperator.Equal, "AUS");

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);

            order.Customer.Country.CountryCode = "USA";
            passes = compiledRule(order);
            Assert.IsFalse(passes);
        }
示例#12
0
        public void DecimalProperties()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("Total", MreOperator.GreaterThan, 12.00m);

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);

            order.Total = 9.99m;
            passes      = compiledRule(order);
            Assert.IsFalse(passes);
        }
示例#13
0
        public void DateProperties()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("OrderDate", MreOperator.LessThan, "1800-01-01");

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);

            order.OrderDate = new DateTime(1814, 9, 13);
            passes          = compiledRule(order);
            Assert.IsFalse(passes);
        }
示例#14
0
        public void IntProperties()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("OrderId", MreOperator.Equal, 1);

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);

            order.OrderId = 5;
            passes        = compiledRule(order);
            Assert.IsFalse(passes);
        }
示例#15
0
        public void ArrayTest()
        {
            var array = new ArrayInside();

            Rule rule = Rule.Create("Dbl[1]", MreOperator.Equal, 22.222);

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <ArrayInside>(rule);
            bool passes       = compiledRule(array);

            Assert.IsTrue(passes);

            array.Dbl[1] = .0001;
            passes       = compiledRule(array);
            Assert.IsFalse(passes);
        }
示例#16
0
        public void SelfReferenialTest()
        {
            Order order = ExampleUsage.GetOrder();

            Rule rule = Rule.Create("Items[1].Cost", MreOperator.Equal, "*.Items[0].Cost");

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);

            order.Items[1].Cost = 6.99m;
            passes = compiledRule(order);
            Assert.IsFalse(passes);
        }
示例#17
0
        public void RegexIsMatch2()//Had to add a Regex evaluator to make it feel 'Complete'
        {
            Order order = ExampleUsage.GetOrder();
            // Regex = Capital letter, vowel, then two constanants  ("John" passes, "Jane" fails)
            Rule rule = Rule.Create("Customer.FirstName", MreOperator.IsMatch, @"^[A-Z][aeiou][bcdfghjklmnpqrstvwxyz]{2}$");

            Mre  engine     = new Mre();
            var  regexCheck = engine.CompileRule <Order>(rule);
            bool passes     = regexCheck(order);

            Assert.IsTrue(passes);

            order.Customer.FirstName = "Jane";
            passes = regexCheck(order);
            Assert.IsFalse(passes);
        }
示例#18
0
        public void ChildPropertyBooleanMethods2()
        {
            Order order = ExampleUsage.GetOrder();
            //Regular method that exists on string.. As a note expression methods are not available
            Rule rule = Rule.MethodOnChild("Customer.FirstName", "EndsWith", "ohn");

            Mre  engine         = new Mre();
            var  childPropCheck = engine.CompileRule <Order>(rule);
            bool passes         = childPropCheck(order);

            Assert.IsTrue(passes);

            order.Customer.FirstName = "jane";
            passes = childPropCheck(order);
            Assert.IsFalse(passes);
        }
示例#19
0
        public void ConditionalLogic2()
        {
            Order order = ExampleUsage.GetOrder();
            Rule  rule  = Rule.Create("Customer.LastName", MreOperator.Equal, "Doe")
                          & (Rule.Create("Customer.FirstName", MreOperator.Equal, "John") | Rule.Create("Customer.FirstName", MreOperator.Equal, "Jane"));

            Mre  engine   = new Mre();
            var  fakeName = engine.CompileRule <Order>(rule);
            bool passes   = fakeName(order);

            Assert.IsTrue(passes);

            order.Customer.FirstName = "Philip";
            passes = fakeName(order);
            Assert.IsFalse(passes);
        }
示例#20
0
 public void Stop()
 {
     lock (_lock)
     {
         if (_thread != null)
         {
             Mre.Set();
             _thread.Cancel();
             _thread.WaitForValue(t => t.IsRunning == false, 1000);
             _thread.Dispose();
             _thread = null;
         }
         Client?.Disconnect();
         Client = null;
     }
 }
示例#21
0
        public void ChildPropertiesOfNull()
        {
            Order order = GetOrder();

            order.Customer = null;
            Rule rule = new Rule
            {
                MemberName  = "Customer.Country.CountryCode",
                Operator    = ExpressionType.Equal.ToString("g"),
                TargetValue = "AUS"
            };
            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsFalse(passes);
        }
示例#22
0
        public void NullableProperties()
        {
            Order order = ExampleUsage.GetOrder();

            order.Total = null;
            Rule rule = Rule.Create("Total", MreOperator.Equal, null);

            Mre  engine       = new Mre();
            var  compiledRule = engine.CompileRule <Order>(rule);
            bool passes       = compiledRule(order);

            Assert.IsTrue(passes);

            order.Total = 9.99m;
            passes      = compiledRule(order);
            Assert.IsFalse(passes);
        }
示例#23
0
        public void DataRowTest()
        {
            var dr = GetDataRow();
            // (int) dr["Column2"] == 123 &&  (string) dr["Column1"] == "Test"
            Rule rule = DataRule.Create <int>("Column2", MreOperator.Equal, "123") & DataRule.Create <string>("Column1", MreOperator.Equal, "Test");

            Mre  engine = new Mre();
            var  c1123  = engine.CompileRule <DataRow>(rule);
            bool passes = c1123(dr);

            Assert.IsTrue(passes);

            dr["Column2"] = 456;
            dr["Column1"] = "Hello";
            passes        = c1123(dr);
            Assert.IsFalse(passes);
        }
示例#24
0
        public void AnyOperator()
        {
            Order order = ExampleUsage.GetOrder();
            //order.Items.Any(a => a.ItemCode == "test")
            Rule rule = Rule.Any("Items", Rule.Create("ItemCode", MreOperator.Equal, "Test"));

            Mre  engine     = new Mre();
            var  boolMethod = engine.CompileRule <Order>(rule);
            bool passes     = boolMethod(order);

            Assert.IsTrue(passes);

            var item = order.Items.First(x => x.ItemCode == "Test");

            item.ItemCode = "Changed";
            passes        = boolMethod(order);
            Assert.IsFalse(passes);
        }
示例#25
0
        public void AllOperator()
        {
            Order order = ExampleUsage.GetOrder();
            //order.Items.All(a => a.Cost > 2.00m)
            Rule rule = Rule.All("Items", Rule.Create("Cost", MreOperator.GreaterThan, "2.00"));

            Mre  engine     = new Mre();
            var  boolMethod = engine.CompileRule <Order>(rule);
            bool passes     = boolMethod(order);

            Assert.IsTrue(passes);

            var item = order.Items.First(x => x.ItemCode == "Test");

            item.Cost = 1.99m;
            passes    = boolMethod(order);
            Assert.IsFalse(passes);
        }
示例#26
0
 private static void BeginWrite()
 {
     while (Kernel.KeepThreadsRunning)
     {
         Mre.WaitOne();
         while (!_writer.BaseStream.CanWrite)
         {
             if (!Kernel.KeepThreadsRunning)
             {
                 break;
             }
             Thread.Sleep(100);
         }
         if (_writer.BaseStream.CanWrite)
         {
             _writer.WriteLine(TakeJob());
         }
     }
 }
示例#27
0
        public void BooleanMethods2()
        {
            Order order = ExampleUsage.GetOrder();

            //The Order Object Contains a method named 'HasItem(string itemCode)' that returns true/false
            Rule rule = Rule.Method("HasItem", "Test");

            Mre  engine     = new Mre();
            var  boolMethod = engine.CompileRule <Order>(rule);
            bool passes     = boolMethod(order);

            Assert.IsTrue(passes);

            var item = order.Items.First(x => x.ItemCode == "Test");

            item.ItemCode = "Changed";
            passes        = boolMethod(order);
            Assert.IsFalse(passes);
        }
示例#28
0
        public void RegexIsMatch()//Had to add a Regex evaluator to make it feel 'Complete'
        {
            Order order = GetOrder();
            Rule  rule  = new Rule
            {
                MemberName  = "Customer.FirstName",
                Operator    = "IsMatch",
                TargetValue = @"^[a-zA-Z0-9]*$"
            };
            Mre  engine     = new Mre();
            var  regexCheck = engine.CompileRule <Order>(rule);
            bool passes     = regexCheck(order);

            Assert.IsTrue(passes);

            order.Customer.FirstName = "--NoName";
            passes = regexCheck(order);
            Assert.IsFalse(passes);
        }
示例#29
0
        public void ChildPropertyOfNullBooleanMethods()
        {
            Order order = GetOrder();

            order.Customer = null;
            Rule rule = new Rule
            {
                MemberName = "Customer.FirstName",
                Operator   = "EndsWith", //Regular method that exists on string.. As a note expression methods are not available
                Inputs     = new List <object> {
                    "ohn"
                }
            };
            Mre  engine         = new Mre();
            var  childPropCheck = engine.CompileRule <Order>(rule);
            bool passes         = childPropCheck(order);

            Assert.IsFalse(passes);
        }
示例#30
0
        public void DataRowTest_RuntimeType()
        {
            var dt = CreateEmptyDataTable();

            Rule rule = DataRule.Create("Column2", MreOperator.Equal, 123, dt.Columns["Column2"].DataType);

            Mre engine = new Mre();
            var c1123  = engine.CompileRule <DataRow>(rule);

            var  dr     = GetDataRow(dt);
            bool passes = c1123(dr);

            Assert.IsTrue(passes);

            dr["Column2"] = 456;
            dr["Column1"] = "Hello";
            passes        = c1123(dr);
            Assert.IsFalse(passes);
        }