public void TestWoopsaServerPerformance()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (dynamic dynamicClient = new WoopsaDynamicClient("http://localhost/woopsa"))
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    dynamicClient.Votes = 0;
                    Console.WriteLine("First invocation duration : {0} ms", watch.Elapsed.TotalMilliseconds);
                    int i = 0;
                    watch.Restart();
                    while (watch.Elapsed < TimeSpan.FromSeconds(1))
                    {
                        dynamicClient.Votes = i;
                        Assert.AreEqual(objectServer.Votes, i);
                        Assert.AreEqual((int)dynamicClient.Votes, i);
                        i++;
                    }
                    Console.WriteLine("Invocation duration : {0} ms", watch.Elapsed.TotalMilliseconds / 2 / i);
                    // TODO : Votes.Change does not work as Votes is a WoopsaValue does not contain Change !?!
                    //				dynamicClient.Votes.Change += new EventHandler<WoopsaNotificationEventArgs>((o, e) => Console.WriteLine("Value : {0}", e.Value.ToInt32()));
                    //		Thread.Sleep(100);
                    //	dynamicClient.Votes = 15;
                    //			WoopsaClient client = new WoopsaClient("http://localhost/woopsa");
                    //				int votes = client.Properties.ByName("Votes").Value.ToInt32();
                    //				client.Properties.ByName("Votes");
                }
            }
        }
        public void TestWoopsaProtocol()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    root.Properties.ByName("Votes").Value = new WoopsaValue(11);
                    Assert.AreEqual(objectServer.Votes, 11);
                    var result = root.Properties.ByName("Votes").Value;
                    Assert.AreEqual(11, result.ToInt64());
                    result = root.Methods.ByName(nameof(TestObjectServer.IncrementVotes)).
                             Invoke(5);
                    Assert.AreEqual(16, root.Properties.ByName("Votes").Value.ToInt64());
                    Assert.AreEqual(WoopsaValueType.Null, result.Type);
                    NameValueCollection args = new NameValueCollection();
                    args.Add("count", "8");
                    result = client.ClientProtocol.Invoke("/" + nameof(TestObjectServer.IncrementVotes),
                                                          args);
                    Assert.AreEqual(24, root.Properties.ByName("Votes").Value.ToInt64());
                    Assert.AreEqual(WoopsaValueType.Null, result.Type);
                }
            }
        }
        public void TestRouteHandlerEmbeddedResource()
        {
            WoopsaRoot          serverRoot   = new WoopsaRoot();
            TestObjectServer    objectServer = new TestObjectServer();
            WoopsaObjectAdapter adapter      = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer);

            using (WoopsaServer server = new WoopsaServer(serverRoot, TestingPort))
            {
                var test1 = new EmbeddedResource.Class1();        // Force to load assembly
                var test2 = new Woopsa.EmbeddedResource.Class1(); // Force to load assembly
                var routeHandlerEmbeddedResources = new RouteHandlerEmbeddedResources();
                server.WebServer.Routes.Add("resources", HTTPMethod.GET,
                                            routeHandlerEmbeddedResources);
                using (HttpClient client = new HttpClient())
                {
                    var request = client.GetAsync($"{TestingUrl}/EmbeddedResource/Images/woopsa-logo.png");
                    request.Wait();
                    Assert.IsTrue(request.Result.IsSuccessStatusCode);
                    request = client.GetAsync($"{TestingUrl}/EmbeddedResource/Images/SubImages/woopsa-logo.png");
                    request.Wait();
                    Assert.IsTrue(request.Result.IsSuccessStatusCode);

                    request = client.GetAsync($"{TestingUrl}/Woopsa.EmbeddedResource/Images/woopsa-logo.png");
                    request.Wait();
                    Assert.IsTrue(request.Result.IsSuccessStatusCode);

                    request = client.GetAsync($"{TestingUrl}/Woopsa.EmbeddedResource/Images/SubImages/woopsa-logo.png");
                    request.Wait();
                    Assert.IsTrue(request.Result.IsSuccessStatusCode);
                }
            }
        }
 public void TestWoopsaProtocol()
 {
     TestObjectServer objectServer = new TestObjectServer();
     using (WoopsaServer server = new WoopsaServer(objectServer))
     {
         using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
         {
             WoopsaBoundClientObject root = client.CreateBoundRoot();
             root.Properties.ByName("Votes").Value = new WoopsaValue(11);
             Assert.AreEqual(objectServer.Votes, 11);
             var result = root.Properties.ByName("Votes").Value;
             Assert.AreEqual(11, result.ToInt64());
             result = root.Methods.ByName(nameof(TestObjectServer.IncrementVotes)).
                 Invoke(5);
             Assert.AreEqual(16, root.Properties.ByName("Votes").Value.ToInt64());
             Assert.AreEqual(WoopsaValueType.Null, result.Type);
             NameValueCollection args = new NameValueCollection();
             args.Add("count", "8");
             result = client.ClientProtocol.Invoke("/" + nameof(TestObjectServer.IncrementVotes),
                 args);
             Assert.AreEqual(24, root.Properties.ByName("Votes").Value.ToInt64());
             Assert.AreEqual(WoopsaValueType.Null, result.Type);
         }
     }
 }
        public void TestWoopsaProtocolPerformance()
        {
            TestObjectServer objectServer = new TestObjectServer();
            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    IWoopsaProperty property = root.Properties.ByName("Votes");
                    property.Value = new WoopsaValue(0);
                    int n = property.Value.ToInt32();
                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    for (int i = 0; i < 100; i++)
                    {
                        property.Value = new WoopsaValue(i);
                        Assert.AreEqual(objectServer.Votes, i);
                        var result = property.Value;
                        Assert.AreEqual(result.ToInt64(), i);
                    }
                    TimeSpan duration = watch.Elapsed;
                    Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200));
                }
            }
        }
 public void TestWoopsaClientSubscriptionChannel()
 {
     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();
             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(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 TestWoopsaProtocolPerformance()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root     = client.CreateBoundRoot();
                    IWoopsaProperty         property = root.Properties.ByName("Votes");
                    property.Value = new WoopsaValue(0);
                    int       n     = property.Value.ToInt32();
                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    for (int i = 0; i < 100; i++)
                    {
                        property.Value = new WoopsaValue(i);
                        Assert.AreEqual(objectServer.Votes, i);
                        var result = property.Value;
                        Assert.AreEqual(result.ToInt64(), i);
                    }
                    TimeSpan duration = watch.Elapsed;
                    Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200));
                }
            }
        }
        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);
                }
            }
        }
Exemple #9
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);
                }
            }
        }
Exemple #10
0
        public void TestWoopsaProtocolPerformance()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root     = client.CreateBoundRoot();
                    IWoopsaProperty         property = root.Properties.ByName("Votes");
                    property.Value = new WoopsaValue(0);
                    int       n     = property.Value.ToInt32();
                    Stopwatch watch = new Stopwatch();
                    watch.Start();

                    for (int i = 0; i < 100; i++)
                    {
                        property.Value = new WoopsaValue(i);
                        Assert.AreEqual(objectServer.Votes, i);
                        var result = property.Value;
                        Assert.AreEqual(result.ToInt64(), i);
                    }
                    TimeSpan duration = watch.Elapsed;
                    Assert.IsTrue(duration < TimeSpan.FromMilliseconds(200), $"Duration takes ${duration.Milliseconds}ms, instead of 200ms");
                    Console.WriteLine($"Duration takes ${duration.Milliseconds}ms");
                }
            }
        }
        public void TestWoopsaWaitNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();
            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    // Just to show how to see all items
                    foreach (var item in root.Items)
                    {
                        Console.WriteLine("Item = " + item.Name);
                        if (item.Name == "SubscriptionService")
                            Console.WriteLine("Trouvé");
                    }

                    // create a subscription object
                    WoopsaObject subscription = root.Items.ByNameOrNull("SubscriptionService") as WoopsaObject;
                    if (subscription != null)
                    {
                        int result = 0;
                        WoopsaMethod methodCreateScubscriptionChannel = subscription.Methods.ByNameOrNull("CreateSubscriptionChannel");
                        if (methodCreateScubscriptionChannel != null)
                            // call the method "CreateSubscriptionChannel" on the server
                            result = methodCreateScubscriptionChannel.Invoke(1000);   // define the queue size
                        int channel = result;

                        WoopsaMethod methodRegisterScubscription = subscription.Methods.ByNameOrNull("RegisterSubscription");
                        if (methodRegisterScubscription != null)
                            // call the method "registerScubscription" on the server
                            result = methodRegisterScubscription.Invoke(channel, WoopsaValue.WoopsaRelativeLink("/Votes"), 0.01, 0.01);
                        int subscriptionNbr = result;

                        WoopsaJsonData jData;
                        WoopsaMethod methodWaitNotification = subscription.Methods.ByNameOrNull("WaitNotification");
                        if (methodWaitNotification != null)
                        {
                            Stopwatch watch = new Stopwatch();
                            watch.Start();
                            // call the method "WaitNotification" on the server
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            int lastNotification;
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                            // Get notifications again
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                        }
                    }
                }

            }
        }
        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);
                }
            }
        }
Exemple #13
0
        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();
            }
        }
        public void TestWoopsaProtocolRootContainer()
        {
            WoopsaRoot          serverRoot   = new WoopsaRoot();
            TestObjectServer    objectServer = new TestObjectServer();
            WoopsaObjectAdapter adapter      = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer);

            using (WoopsaServer server = new WoopsaServer(serverRoot))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    (root.Items.ByName("TestObject") as WoopsaObject).Properties.ByName("Votes").Value = 17;
                    Assert.AreEqual(objectServer.Votes, 17);
                }
            }
        }
        public void TestWoopsaProtocolUnboundClient()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
            {
                WoopsaUnboundClientObject root         = client.CreateUnboundRoot("root");
                WoopsaProperty            propertyVote = root.GetProperty("Votes", WoopsaValueType.Integer, false);
                using (WoopsaServer server = new WoopsaServer(objectServer))
                {
                    propertyVote.Value = new WoopsaValue(123);
                    Assert.AreEqual(objectServer.Votes, 123);
                    var result = propertyVote.Value;
                    Assert.AreEqual(result.ToInt64(), 123);
                }
            }
        }
        public void TestWoopsaDynamicNotificationUnexistingProperty()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                // Solution with dynamic client
                using (dynamic dynamicClient = new WoopsaDynamicClient(TestingUrl))
                {
                    int channel = dynamicClient.SubscriptionService.CreateSubscriptionChannel(QUEUE_SIZE);
                    // Subscription for an nonexistent variable (should work)
                    dynamicClient.SubscriptionService.RegisterSubscription(channel,
                                                                           WoopsaValue.WoopsaRelativeLink("/Vote"), TimeSpan.FromMilliseconds(MONITOR_INTERVAL),
                                                                           TimeSpan.FromMilliseconds(PUBLISH_INTERVAL));

                    Stopwatch      watch = new Stopwatch();
                    WoopsaValue    lastNotifications;
                    WoopsaJsonData jsonData;
                    int            lastNotificationId;
                    watch.Start();
                    do
                    {
                        lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                        Assert.AreEqual(lastNotifications.Type, WoopsaValueType.JsonData);
                        jsonData = lastNotifications.JsonData;
                        if (watch.ElapsedMilliseconds > 1000)
                        {
                            Assert.Fail("Timeout without receveiving any notification");
                        }
                    }while (jsonData.Length == 0);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Get again the same notification
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData          = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 1);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                }
            }
        } //end TestWoopsaDynamicNotification
        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 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 #19
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);
            }
        }
        public void TestWoopsaWaitNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                using (WoopsaClient client = new WoopsaClient(TestingUrl))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    // Just to show how to see all items
                    foreach (var item in root.Items)
                    {
                        Console.WriteLine("Item = " + item.Name);
                        if (item.Name == "SubscriptionService")
                        {
                            Console.WriteLine("Trouvé");
                        }
                    }

                    // create a subscription object
                    WoopsaObject subscription = root.Items.ByNameOrNull("SubscriptionService") as WoopsaObject;
                    if (subscription != null)
                    {
                        int          result = 0;
                        WoopsaMethod methodCreateScubscriptionChannel = subscription.Methods.ByNameOrNull("CreateSubscriptionChannel");
                        if (methodCreateScubscriptionChannel != null)
                        {
                            // call the method "CreateSubscriptionChannel" on the server
                            result = methodCreateScubscriptionChannel.Invoke(1000);   // define the queue size
                        }
                        int channel = result;

                        WoopsaMethod methodRegisterScubscription = subscription.Methods.ByNameOrNull("RegisterSubscription");
                        if (methodRegisterScubscription != null)
                        {
                            // call the method "registerScubscription" on the server
                            result = methodRegisterScubscription.Invoke(channel, WoopsaValue.WoopsaRelativeLink("/Votes"), 0.01, 0.01);
                        }
                        int subscriptionNbr = result;

                        WoopsaJsonData jData;
                        WoopsaMethod   methodWaitNotification = subscription.Methods.ByNameOrNull("WaitNotification");
                        if (methodWaitNotification != null)
                        {
                            Stopwatch watch = new Stopwatch();
                            watch.Start();
                            // call the method "WaitNotification" on the server
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            int lastNotification;
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                            // Get notifications again
                            Thread.Sleep(100);
                            jData = methodWaitNotification.Invoke(channel, 0).JsonData;
                            Assert.IsTrue(jData.Length > 0);
                            lastNotification = jData[0]["Id"];
                            Assert.AreEqual(lastNotification, 1);
                        }
                    }
                }
            }
        }
 public void TestWoopsaProtocolUnboundClient()
 {
     TestObjectServer objectServer = new TestObjectServer();
     using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
     {
         WoopsaUnboundClientObject root = client.CreateUnboundRoot("root");
         WoopsaProperty propertyVote = root.GetProperty("Votes", WoopsaValueType.Integer, false);
         using (WoopsaServer server = new WoopsaServer(objectServer))
         {
             propertyVote.Value = new WoopsaValue(123);
             Assert.AreEqual(objectServer.Votes, 123);
             var result = propertyVote.Value;
             Assert.AreEqual(result.ToInt64(), 123);
         }
     }
 }
        public void TestWoopsaClientSubscriptionChannelUnexistingItem()
        {
            TestObjectServer objectServer = new TestObjectServer();
            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
                {
                    WoopsaBoundClientObject root = client.CreateBoundRoot();
                    try
                    {
                        WoopsaClientSubscription sub = root.Subscribe("ThisDoesNotExistInTheServer",
                            (sender, e) => { },
                            TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(20));
                        Assert.Fail();
                    }
                    catch (Exception)
                    {
                    }

                }
            }
        }
 public void TestWoopsaServerPerformance()
 {
     TestObjectServer objectServer = new TestObjectServer();
     using (WoopsaServer server = new WoopsaServer(objectServer))
     {
         using (dynamic dynamicClient = new WoopsaDynamicClient("http://localhost/woopsa"))
         {
             Stopwatch watch = new Stopwatch();
             watch.Start();
             dynamicClient.Votes = 0;
             Console.WriteLine("First invocation duration : {0} ms", watch.Elapsed.TotalMilliseconds);
             int i = 0;
             watch.Restart();
             while (watch.Elapsed < TimeSpan.FromSeconds(1))
             {
                 dynamicClient.Votes = i;
                 Assert.AreEqual(objectServer.Votes, i);
                 Assert.AreEqual((int)dynamicClient.Votes, i);
                 i++;
             }
             Console.WriteLine("Invocation duration : {0} ms", watch.Elapsed.TotalMilliseconds / 2 / i);
             // TODO : Votes.Change does not work as Votes is a WoopsaValue does not contain Change !?!
             //				dynamicClient.Votes.Change += new EventHandler<WoopsaNotificationEventArgs>((o, e) => Console.WriteLine("Value : {0}", e.Value.ToInt32()));
             //		Thread.Sleep(100);
             //	dynamicClient.Votes = 15;
             //			WoopsaClient client = new WoopsaClient("http://localhost/woopsa");
             //				int votes = client.Properties.ByName("Votes").Value.ToInt32();
             //				client.Properties.ByName("Votes");
         }
     }
 }
 public void TestWoopsaProtocolRootContainer()
 {
     WoopsaRoot serverRoot = new WoopsaRoot();
     TestObjectServer objectServer = new TestObjectServer();
     WoopsaObjectAdapter adapter = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer);
     using (WoopsaServer server = new WoopsaServer(serverRoot))
     {
         using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa"))
         {
             WoopsaBoundClientObject root = client.CreateBoundRoot();
             (root.Items.ByName("TestObject") as WoopsaObject).Properties.ByName("Votes").Value = 17;
             Assert.AreEqual(objectServer.Votes, 17);
         }
     }
 }
        public void TestWoopsaDynamicNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();
            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                // Solution with dynamic client
                using (dynamic dynamicClient = new WoopsaDynamicClient("http://localhost/woopsa"))
                {
                    int channel = dynamicClient.SubscriptionService.CreateSubscriptionChannel(QUEUE_SIZE);
                    // Subscription for a valid variable
                    dynamicClient.SubscriptionService.RegisterSubscription(channel,
                       WoopsaValue.WoopsaRelativeLink("/Votes"), TimeSpan.FromMilliseconds(MONITOR_INTERVAL),
                       TimeSpan.FromMilliseconds(PUBLISH_INTERVAL));

                    Stopwatch watch = new Stopwatch();
                    WoopsaValue lastNotifications;
                    WoopsaJsonData jsonData;
                    int lastNotificationId;
                    watch.Start();
                    do
                    {
                        lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                        Assert.AreEqual(lastNotifications.Type, WoopsaValueType.JsonData);
                        jsonData = lastNotifications.JsonData;
                        if (watch.ElapsedMilliseconds > 1000)
                            Assert.Fail("Timeout without receveiving any notification");
                    }
                    while (jsonData.Length == 0);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Get again the same notification
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 1);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Generate a new notification
                    objectServer.Votes++;
                    Thread.Sleep(PUBLISH_INTERVAL * 10);
                    // Check we have now 2
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 2);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 2);
                    // Check we can remove 1 and still have 1
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 1);
                    jsonData = lastNotifications.JsonData;
                    Assert.AreEqual(jsonData.Length, 1);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 2);
                    // Enable the code below to test the wait of the timeout when they are 0 notifications pending
                    /*
                    // Check we can remove 1 and have 0. This takes 5 seconds (we wait the timeout)
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, lastNotificationId);
                    jsonData = lastNotifications.JsonData;
                    Assert.AreEqual(jsonData.Length, 0);
                    */
                }
            }
        }
        public void TestWoopsaDynamicNotification()
        {
            TestObjectServer objectServer = new TestObjectServer();

            using (WoopsaServer server = new WoopsaServer(objectServer, TestingPort))
            {
                // Solution with dynamic client
                using (dynamic dynamicClient = new WoopsaDynamicClient(TestingUrl))
                {
                    int channel = dynamicClient.SubscriptionService.CreateSubscriptionChannel(QUEUE_SIZE);
                    // Subscription for a valid variable
                    dynamicClient.SubscriptionService.RegisterSubscription(channel,
                                                                           WoopsaValue.WoopsaRelativeLink("/Votes"), TimeSpan.FromMilliseconds(MONITOR_INTERVAL),
                                                                           TimeSpan.FromMilliseconds(PUBLISH_INTERVAL));

                    Stopwatch      watch = new Stopwatch();
                    WoopsaValue    lastNotifications;
                    WoopsaJsonData jsonData;
                    int            lastNotificationId;
                    watch.Start();
                    do
                    {
                        lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                        Assert.AreEqual(lastNotifications.Type, WoopsaValueType.JsonData);
                        jsonData = lastNotifications.JsonData;
                        if (watch.ElapsedMilliseconds > 1000)
                        {
                            Assert.Fail("Timeout without receveiving any notification");
                        }
                    }while (jsonData.Length == 0);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Get again the same notification
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData          = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 1);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Generate a new notification
                    objectServer.Votes++;
                    Thread.Sleep(PUBLISH_INTERVAL * 10);
                    // Check we have now 2
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData          = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 2);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 2);
                    // Check we can remove 1 and still have 1
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 1);
                    jsonData          = lastNotifications.JsonData;
                    Assert.AreEqual(jsonData.Length, 1);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 2);
                    // Enable the code below to test the wait of the timeout when they are 0 notifications pending

                    /*
                     * // Check we can remove 1 and have 0. This takes 5 seconds (we wait the timeout)
                     * lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, lastNotificationId);
                     * jsonData = lastNotifications.JsonData;
                     * Assert.AreEqual(jsonData.Length, 0);
                     */
                }
            }
        } //end TestWoopsaDynamicNotification
        public void TestWoopsaDynamicNotificationUnexistingProperty()
        {
            TestObjectServer objectServer = new TestObjectServer();
            using (WoopsaServer server = new WoopsaServer(objectServer))
            {
                // Solution with dynamic client
                using (dynamic dynamicClient = new WoopsaDynamicClient("http://localhost/woopsa"))
                {
                    int channel = dynamicClient.SubscriptionService.CreateSubscriptionChannel(QUEUE_SIZE);
                    // Subscription for an nonexistent variable (should work)
                    dynamicClient.SubscriptionService.RegisterSubscription(channel,
                        WoopsaValue.WoopsaRelativeLink("/Vote"), TimeSpan.FromMilliseconds(MONITOR_INTERVAL),
                        TimeSpan.FromMilliseconds(PUBLISH_INTERVAL));

                    Stopwatch watch = new Stopwatch();
                    WoopsaValue lastNotifications;
                    WoopsaJsonData jsonData;
                    int lastNotificationId;
                    watch.Start();
                    do
                    {
                        lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                        Assert.AreEqual(lastNotifications.Type, WoopsaValueType.JsonData);
                        jsonData = lastNotifications.JsonData;
                        if (watch.ElapsedMilliseconds > 1000)
                            Assert.Fail("Timeout without receveiving any notification");
                    }
                    while (jsonData.Length == 0);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                    // Get again the same notification
                    lastNotifications = dynamicClient.SubscriptionService.WaitNotification(channel, 0);
                    jsonData = lastNotifications.JsonData;
                    Assert.IsTrue(jsonData.IsArray);
                    Assert.AreEqual(jsonData.Length, 1);
                    Assert.IsTrue(jsonData[0].IsDictionary);
                    lastNotificationId = jsonData[jsonData.Length - 1]["Id"];
                    Assert.AreEqual(lastNotificationId, 1);
                }
            }
        }