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 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 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 TestWoopsaServerAuthentication() { TestObjectServerAuthentification objectServer = new TestObjectServerAuthentification(); using (WoopsaServer server = new WoopsaServer(objectServer)) { server.Authenticator = new SimpleAuthenticator("TestRealm", (sender, e) => { e.IsAuthenticated = e.Username == "woopsa"; }); using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa")) { const string TestUserName = "******"; client.Username = TestUserName; WoopsaBoundClientObject root = client.CreateBoundRoot(); WoopsaProperty propertyVotes = root.Properties.ByName("Votes"); propertyVotes.Value = 5; Assert.AreEqual(objectServer.Votes, 5); Assert.AreEqual((int)propertyVotes.Value, 5); WoopsaProperty propertyCurrentUserName = root.Properties.ByName(nameof(TestObjectServerAuthentification.CurrentUserName)); Assert.AreEqual(propertyCurrentUserName.Value, TestUserName); client.Username = "******"; bool authenticationCheckOk; try { propertyVotes.Value = 5; authenticationCheckOk = false; } catch { authenticationCheckOk = true; } Assert.IsTrue(authenticationCheckOk); } } }
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 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(); } }
public void TestWoopsaMultiRequestNoRemoteMultiRequestService() { WoopsaObject serverRoot = new WoopsaObject(null, ""); TestObjectMultiRequest objectServer = new TestObjectMultiRequest(); WoopsaObjectAdapter adapter = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer); using (WoopsaServer server = new WoopsaServer((IWoopsaContainer)serverRoot, TestingPort)) { using (WoopsaClient client = new WoopsaClient(TestingUrl)) { ExecuteMultiRequestTestSerie(client, objectServer); } } }
public void TestWoopsaMultiRequest() { WoopsaObject serverRoot = new WoopsaObject(null, ""); TestObjectMultiRequest objectServer = new TestObjectMultiRequest(); WoopsaObjectAdapter adapter = new WoopsaObjectAdapter(serverRoot, "TestObject", objectServer); using (WoopsaServer server = new WoopsaServer(serverRoot)) { using (WoopsaClient client = new WoopsaClient("http://localhost/woopsa")) { ExecuteMultiRequestTestSerie(client, objectServer); } } }
static void Main(string[] args) { var root = new WoopsaObject(null, "Gateway.IoT"); WoopsaClient client = new WoopsaClient("http://192.168.1.66/woopsa", root); client.Username = "******"; client.Password = "******"; client.CreateBoundRoot("device_garage"); var woopsaServer = new WoopsaServer(root, 10443); woopsaServer.Authenticator = new SimpleAuthenticator("Raspberry", (sender, e) => e.IsAuthenticated = e.Username == "admin" && e.Password == "admin"); }
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 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 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, 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) { } } } }
private void ExecuteHugeMultiRequestTestSerie(WoopsaClient client, TestObjectMultiRequest objectServer) { WoopsaClientMultiRequest multiRequest = new WoopsaClientMultiRequest(); for (int i = 0; i < 200; i++) // ~200 X 15 requests { multiRequest.Write("TestObject/A", 1); multiRequest.Write("TestObject/B", 2); multiRequest.Read("TestObject/Sum"); multiRequest.Write("TestObject/C", 4); multiRequest.Meta("TestObject"); WoopsaMethodArgumentInfo[] argumentInfosSet = new WoopsaMethodArgumentInfo[] { new WoopsaMethodArgumentInfo("a", WoopsaValueType.Real), new WoopsaMethodArgumentInfo("b", WoopsaValueType.Real) }; multiRequest.Invoke("TestObject/Set", argumentInfosSet, 4, 5); multiRequest.Read("TestObject/Sum"); multiRequest.Invoke("TestObject/Click", new Dictionary <string, WoopsaValue>()); multiRequest.Read("TestObject/IsClicked"); multiRequest.Write("TestObject/IsClicked", false); multiRequest.Read("TestObject/IsClicked"); WoopsaMethodArgumentInfo[] argumentInfosSetS = new WoopsaMethodArgumentInfo[] { new WoopsaMethodArgumentInfo("value", WoopsaValueType.Text) }; multiRequest.Invoke("TestObject/SetS", argumentInfosSetS, "Hello"); multiRequest.Read("TestObject/S"); multiRequest.Write("TestObject/S", "ABC"); multiRequest.Read("TestObject/S"); } client.ExecuteMultiRequest(multiRequest); }
static void Main(string[] args) { Console.WriteLine(" *** Welcome to the Woopsa Demo Client! *** "); Console.WriteLine(" Note: read the source code to understand what's happening behind the scenes!"); Console.WriteLine(""); string serverUrl; if (File.Exists("url.config")) { serverUrl = File.ReadAllText("url.config"); Console.WriteLine("Using url.config"); } else { Console.Write("Please enter the Woopsa server URL or leave blank for default (http://localhost/woopsa): "); serverUrl = Console.ReadLine(); if (serverUrl == "") { serverUrl = "http://localhost/woopsa"; } } WoopsaClient client = new WoopsaClient(serverUrl); Console.WriteLine("Woopsa client created on URL: {0}", serverUrl); WoopsaBoundClientObject root = client.CreateBoundRoot(); ExploreItem(root); // Leave the DOS window open Console.WriteLine("Press any key to exit..."); Console.ReadLine(); client.Dispose(); }
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); } } }
private void ExecuteMultiRequestTestSerie(WoopsaClient client, TestObjectMultiRequest objectServer) { WoopsaClientMultiRequest multiRequest = new WoopsaClientMultiRequest(); WoopsaClientRequest request1 = multiRequest.Write("TestObject/A", 1); WoopsaClientRequest request2 = multiRequest.Write("TestObject/B", 2); WoopsaClientRequest request3 = multiRequest.Read("TestObject/Sum"); WoopsaClientRequest request4 = multiRequest.Write("TestObject/C", 4); WoopsaClientRequest request5 = multiRequest.Meta("TestObject"); WoopsaMethodArgumentInfo[] argumentInfosSet = new WoopsaMethodArgumentInfo[] { new WoopsaMethodArgumentInfo("a", WoopsaValueType.Real), new WoopsaMethodArgumentInfo("b", WoopsaValueType.Real) }; WoopsaClientRequest request6 = multiRequest.Invoke("TestObject/Set", argumentInfosSet, 4, 5); WoopsaClientRequest request7 = multiRequest.Read("TestObject/Sum"); WoopsaClientRequest request8 = multiRequest.Invoke("TestObject/Click", new Dictionary <string, WoopsaValue>()); WoopsaClientRequest request9 = multiRequest.Read("TestObject/IsClicked"); WoopsaClientRequest request10 = multiRequest.Write("TestObject/IsClicked", false); WoopsaClientRequest request11 = multiRequest.Read("TestObject/IsClicked"); WoopsaMethodArgumentInfo[] argumentInfosSetS = new WoopsaMethodArgumentInfo[] { new WoopsaMethodArgumentInfo("value", WoopsaValueType.Text) }; WoopsaClientRequest request12 = multiRequest.Invoke("TestObject/SetS", argumentInfosSetS, "Hello"); WoopsaClientRequest request13 = multiRequest.Read("TestObject/S"); WoopsaClientRequest request14 = multiRequest.Write("TestObject/S", "ABC"); WoopsaClientRequest request15 = multiRequest.Read("TestObject/S"); client.ExecuteMultiRequest(multiRequest); Assert.AreEqual(objectServer.A, 4); Assert.AreEqual(objectServer.B, 5); Assert.AreEqual(request3.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.AreEqual(request3.Result.Value, 3.0); Assert.AreEqual(request4.Result.ResultType, WoopsaClientRequestResultType.Error); Assert.IsNotNull(request4.Result.Error); Assert.AreEqual(request5.Result.ResultType, WoopsaClientRequestResultType.Meta); Assert.IsNotNull(request5.Result.Meta); Assert.AreEqual(request6.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.IsNotNull(request6.Result.Value); Assert.AreEqual(request6.Result.Value.Type, WoopsaValueType.Null); Assert.AreEqual(request7.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.IsNotNull(request7.Result.Value); Assert.AreEqual(request7.Result.Value, 9.0); Assert.AreEqual(request8.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.AreEqual(request8.Result.Value.Type, WoopsaValueType.Null); Assert.IsTrue(request9.Result.Value); Assert.AreEqual(request10.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.IsFalse(request11.Result.Value); Assert.AreEqual(request12.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.AreEqual(request12.Result.Value.Type, WoopsaValueType.Null); Assert.AreEqual(request13.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.IsNotNull(request13.Result.Value); Assert.AreEqual(request13.Result.Value, "Hello"); Assert.AreEqual(objectServer.S, "ABC"); Assert.AreEqual(request15.Result.ResultType, WoopsaClientRequestResultType.Value); Assert.IsNotNull(request15.Result.Value); Assert.AreEqual(request15.Result.Value, "ABC"); }
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 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(); } }
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); } } } } }