Exemple #1
0
        public void TestWoopsaClientSubscriptionToProperty()
        {
            bool             isValueChanged = false;
            TestObjectServer objectServer   = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject  root          = client.CreateBoundRoot();
                    WoopsaClientProperty     propertyVotes = root.Properties.ByName("Votes") as WoopsaClientProperty;
                    WoopsaClientSubscription subscription  = propertyVotes.Subscribe((sender, e) => { isValueChanged = true; },
                                                                                     TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                    objectServer.Votes = 2;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isValueChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isValueChanged);
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannelServerStartAfter()
        {
            bool             isValueChanged = false;
            TestObjectServer objectServer   = new TestObjectServer();

            using (WoopsaClient client = new WoopsaClient(TestingUrl))
            {
                WoopsaUnboundClientObject root         = client.CreateUnboundRoot("");
                WoopsaClientSubscription  subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                        (sender, e) => { isValueChanged = true; },
                                                                        TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
                {
                    objectServer.Votes = 2;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(2)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isValueChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isValueChanged);
                }
            }
        }
        public void TestWoopsaClientSubscriptionToProperty()
        {
            bool             isVotesChanged       = false;
            bool             isStringValueChanged = false;
            TestObjectServer objectServer         = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    int    newVotes                        = 0;
                    string newStringValue                  = string.Empty;
                    WoopsaBoundClientObject  root          = client.CreateBoundRoot();
                    WoopsaClientProperty     propertyVotes = root.Properties.ByName("Votes") as WoopsaClientProperty;
                    WoopsaClientSubscription subscription  = propertyVotes.Subscribe((sender, e) =>
                    {
                        newVotes       = e.Notification.Value;
                        isVotesChanged = true;
                    },
                                                                                     TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));

                    WoopsaClientProperty     propertyString = root.Properties.ByName("StringValue") as WoopsaClientProperty;
                    WoopsaClientSubscription subscription2  = propertyString.Subscribe((sender, e) =>
                    {
                        var t                = client.ClientProtocol.Read("Votes");
                        newStringValue       = e.Notification.Value;
                        isStringValueChanged = true;
                    },
                                                                                       TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));

                    objectServer.Votes       = 2;
                    objectServer.StringValue = "Test";
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isVotesChanged || !isStringValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isVotesChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isVotesChanged);
                    Assert.AreEqual(true, isStringValueChanged);

                    Assert.AreEqual(2, newVotes);
                    Assert.AreEqual("Test", newStringValue);
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannel5000SubscriptionsObservableCollection()
        {
            const int objectsCount       = 5000;
            int       totalNotifications = 0;
            ObservableCollection <ManySubscriptionTestObject> list =
                new ObservableCollection <ManySubscriptionTestObject>();

            for (int i = 0; i < objectsCount; i++)
            {
                list.Add(new ManySubscriptionTestObject()
                {
                    Trigger = i
                });
            }
            using (WoopsaServer server = new WoopsaServer(new WoopsaObjectAdapter(null, "list", list, null, null,
                                                                                  WoopsaObjectAdapterOptions.None, WoopsaVisibility.DefaultIsVisible | WoopsaVisibility.IEnumerableObject), TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl, null, objectsCount))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    for (int i = 0; i < list.Count; i++)
                    {
                        int index = i;
                        WoopsaClientSubscription subscription = root.Subscribe(
                            WoopsaUtils.CombinePath(
                                WoopsaObjectAdapter.EnumerableItemDefaultName(i),
                                nameof(ManySubscriptionTestObject.Trigger)),
                            (sender, e) =>
                        {
                            list[index].HasNotified = true;
                            totalNotifications++;
                        },
                            TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(200));
                    }
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((totalNotifications < objectsCount) && (watch.Elapsed < TimeSpan.FromSeconds(500)))
                    {
                        Thread.Sleep(10);
                    }
                    if (totalNotifications == objectsCount)
                    {
                        Console.WriteLine("All notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("{0} notification received, {1} expected", totalNotifications, objectsCount);
                    }
                    Assert.AreEqual(objectsCount, totalNotifications);
                }
            }
        }
        public void TestWoopsaIsLastCommunicationSuccessful()
        {
            bool             isSuccessfull = false;
            TestObjectServer objectServer  = new TestObjectServer();

            using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
            {
                WoopsaUnboundClientObject root         = client.CreateUnboundRoot("root");
                WoopsaClientSubscription  subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                        (sender, e) => {  },
                                                                        TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                client.ClientProtocol.IsLastCommunicationSuccessfulChange +=
                    (sender, e) =>
                {
                    isSuccessfull = client.ClientProtocol.IsLastCommunicationSuccessful;
                };
                Stopwatch watch = new Stopwatch();
                using (WoopsaServer server = new WoopsaServer(objectServer))
                {
                    watch.Restart();
                    while ((!isSuccessfull) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isSuccessfull)
                    {
                        Console.WriteLine("Sucessful after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No successful communication");
                    }
                    Assert.IsTrue(isSuccessfull);
                }
                watch.Restart();
                while ((isSuccessfull) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                {
                    Thread.Sleep(10);
                }
                if (!isSuccessfull)
                {
                    Console.WriteLine("Communication loss detected after {0} ms", watch.Elapsed.TotalMilliseconds);
                }
                else
                {
                    Console.WriteLine("No communication loss detection");
                }
                Assert.IsFalse(isSuccessfull);
                subscription.Unsubscribe();
            }
        }
Exemple #6
0
        internal WoopsaClientSubscription Subscribe(string servicePath, string relativePath,
                                                    EventHandler <WoopsaNotificationEventArgs> handler,
                                                    TimeSpan monitorInterval, TimeSpan publishInterval)
        {
            WoopsaClientSubscription subscription =
                new WoopsaClientSubscription(this, servicePath, relativePath, monitorInterval, publishInterval, handler);

            lock (_subscriptions)
            {
                _subscriptions.Add(subscription);
                SubscriptionsChanged = true;
            }
            EnsureServiceThreadStarted();
            return(subscription);
        }
        public void TestWoopsaClientSubscriptionDisappearingProperty()
        {
            bool       isValueChanged = false;
            MainClass  objectServer   = new MainClass();
            InnerClass inner          = new InnerClass();

            objectServer.Inner = inner;
            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject  root         = client.CreateBoundRoot();
                    WoopsaObject             Inner        = root.Items.ByName(nameof(MainClass.Inner)) as WoopsaObject;
                    WoopsaClientProperty     propertyInfo = Inner.Properties.ByName(nameof(InnerClass.Info)) as WoopsaClientProperty;
                    WoopsaClientSubscription subscription = propertyInfo.Subscribe(
                        (sender, e) =>
                    {
                        isValueChanged = true;
                    },
                        TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                    inner.Info = "Test";
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isValueChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    isValueChanged     = false;
                    objectServer.Inner = new BaseInnerClass();
                    //                    objectServer.Inner = new object();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isValueChanged);
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannelUnexistingItem()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    try
                    {
                        WoopsaClientSubscription sub = root.Subscribe("ThisDoesNotExistInTheServer",
                                                                      (sender, e) => { },
                                                                      TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                        Assert.Fail();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
        public void TestWoopsaClientSubscriptionChannelNoRemoteSubscriptionService()
        {
            bool           isValueChanged = false;
            WoopsaObject   objectServer   = new WoopsaObject(null, "");
            int            votes          = 0;
            WoopsaProperty propertyVotes  = new WoopsaProperty(objectServer, "Votes", WoopsaValueType.Integer, (p) => votes,
                                                               (p, value) => { votes = value.ToInt32(); });

            using (WoopsaServer server = new WoopsaServer((IWoopsaContainer)objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject  root         = client.CreateBoundRoot();
                    WoopsaClientSubscription subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                           (sender, e) => { isValueChanged = true; },
                                                                           TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                    votes = 2;
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(2000)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isValueChanged)
                    {
                        Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No notification received");
                    }
                    subscription.Unsubscribe();
                    Assert.AreEqual(true, isValueChanged);
                }
            }
        }
        public void TestWoopsaIsLastCommunicationSuccessful()
        {
            bool             isSuccessfull = false;
            int              counter       = 0;
            TestObjectServer objectServer  = new TestObjectServer();

            using (WoopsaClient client = new WoopsaClient(TestingUrl))
            {
                Assert.IsFalse(client.ClientProtocol.IsLastCommunicationSuccessful);

                client.ClientProtocol.IsLastCommunicationSuccessfulChange +=
                    (sender, e) =>
                {
                    isSuccessfull = client.ClientProtocol.IsLastCommunicationSuccessful;
                    counter++;
                };
                // Initial & unsuccessful
                try
                {
                    client.ClientProtocol.Read("Not existing");
                }
                catch
                { }
                Assert.IsFalse(isSuccessfull);
                Assert.AreEqual(1, counter);


                WoopsaUnboundClientObject root         = client.CreateUnboundRoot("root");
                WoopsaClientSubscription  subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                        (sender, e) => {  },
                                                                        TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));

                Stopwatch watch = new Stopwatch();
                using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
                {
                    watch.Restart();
                    while ((!isSuccessfull) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                    {
                        Thread.Sleep(10);
                    }
                    if (isSuccessfull)
                    {
                        Console.WriteLine("Sucessful after {0} ms", watch.Elapsed.TotalMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("No successful communication");
                    }
                    Assert.IsTrue(isSuccessfull);
                }
                watch.Restart();
                while ((isSuccessfull) && (watch.Elapsed < TimeSpan.FromSeconds(20)))
                {
                    Thread.Sleep(10);
                }
                if (!isSuccessfull)
                {
                    Console.WriteLine("Communication loss detected after {0} ms", watch.Elapsed.TotalMilliseconds);
                }
                else
                {
                    Console.WriteLine("No communication loss detection");
                }
                Assert.IsFalse(isSuccessfull);
                subscription.Unsubscribe();
            }
        }
Exemple #11
0
        public void TestWoopsaSubscriptionRemovedWhenServerRestart()
        {
            bool             isValueChanged = false;
            TestObjectServer objectServer   = new TestObjectServer();

            using (WoopsaClient client = new WoopsaClient(TestingUrl))
            {
                using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
                    using (WoopsaBoundClientObject root = client.CreateBoundRoot())
                    {
                        using (WoopsaClientSubscription subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                                      (sender, e) => { isValueChanged = true; },
                                                                                      TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20)))
                        {
                            Stopwatch watch = new Stopwatch();
                            watch.Start();
                            while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(10)))
                            {
                                Thread.Sleep(10);
                            }

                            Assert.AreEqual(1, client.SubscriptionChannel.RegisteredSubscriptionCount);
                            if (isValueChanged)
                            {
                                Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                            }
                            else
                            {
                                Console.WriteLine("No notification received");
                            }
                        }
                    }

                Assert.IsTrue(isValueChanged);
                Assert.AreEqual(1, client.SubscriptionChannel.SubscriptionsCount);
                Assert.AreEqual(1, client.SubscriptionChannel.RegisteredSubscriptionCount);

                isValueChanged = false;
                using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
                    using (WoopsaBoundClientObject root = client.CreateBoundRoot())
                    {
                        using (WoopsaClientSubscription subscription = root.Subscribe(nameof(TestObjectServer.Votes),
                                                                                      (sender, e) => { isValueChanged = true; },
                                                                                      TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20)))
                        {
                            Stopwatch watch = new Stopwatch();
                            watch.Start();
                            while ((!isValueChanged) && (watch.Elapsed < TimeSpan.FromSeconds(10)))
                            {
                                Thread.Sleep(10);
                            }

                            Assert.AreEqual(1, client.SubscriptionChannel.RegisteredSubscriptionCount);
                            if (isValueChanged)
                            {
                                Console.WriteLine("Notification after {0} ms", watch.Elapsed.TotalMilliseconds);
                            }
                            else
                            {
                                Console.WriteLine("No notification received");
                            }
                        }
                    }
                Assert.IsTrue(isValueChanged);
                Assert.AreEqual(1, client.SubscriptionChannel.SubscriptionsCount);
                Assert.AreEqual(1, client.SubscriptionChannel.RegisteredSubscriptionCount);
            }
        }