public void SingleDeviceSendToAndPeekAndCommitTest() { var ocassionalConnectionClient = new OccasionallyConnectionClient(PlatformApi, _deviceId, _apiKey); ocassionalConnectionClient.SendMessageTo(_deviceId, "msg1"); ocassionalConnectionClient.SendMessageTo(_deviceId, "msg2"); var result1 = ocassionalConnectionClient.PeekMessage(); Assert.IsTrue(result1.MessageId >= 0); Assert.IsTrue(result1.Payload == "msg1"); Assert.IsTrue(result1.Timestamp > new DateTime(2014, 1, 1)); Assert.AreEqual(_deviceId, result1.SenderDeviceId); var result2 = ocassionalConnectionClient.PeekMessage(); Assert.AreEqual(result1.MessageId, result2.MessageId); Assert.IsTrue(result2.Payload == "msg1"); Assert.IsTrue(result2.Timestamp > new DateTime(2014, 1, 1)); Assert.AreEqual(_deviceId, result2.SenderDeviceId); ocassionalConnectionClient.CommitMessage(); var result3 = ocassionalConnectionClient.PeekMessage(); Assert.AreEqual(result2.MessageId + 1, result3.MessageId); Assert.IsTrue(result3.Payload == "msg2"); Assert.IsTrue(result3.Timestamp > new DateTime(2014, 1, 1)); }
private static void OcassionalSend(string[] lines, int deviceOrder, int sleep, int maxSend) { var line = lines[deviceOrder]; var parts = line.Split(' '); var deviceId = parts[0]; var apiKey = parts[1]; Log(deviceId); var ocassionalConnectionClient = new OccasionallyConnectionClient(Config.PlatformApi, deviceId, apiKey); var rnd = new Random(); int cnt = 0; while (true) { var target = lines[rnd.Next(maxSend)]; var targetDeviceId = target.Split(' ')[0]; try { ocassionalConnectionClient.SendMessageTo(targetDeviceId, "{\"Temperature:\": 24, \"Humidity\": 60, \"Time\":" + DateTime.UtcNow.Ticks + "}"); cnt++; } catch (WebException) { Log("sendthrottled"); } Thread.Sleep(sleep); Log(cnt.ToString()); } }
public void SingleDeviceSendToAndReceiveAndForgetTest() { var ocassionalConnectionClient = new OccasionallyConnectionClient(PlatformApi, _deviceId, _apiKey); ocassionalConnectionClient.SendMessageTo(_deviceId, "test"); var result = ocassionalConnectionClient.ReceiveAndForgetMessage(); Assert.IsTrue(result.MessageId >= 0); Assert.IsTrue(result.Payload == "test"); Assert.IsTrue(result.Timestamp > new DateTime(2014, 1, 1)); Assert.AreEqual(_deviceId, result.SenderDeviceId); }
public void MultiDeviceSendAndReceiveOutgoingTest() { var managementClient = new ManagementClient(ManagementApi); managementClient.User.Register(new Register { Email = Guid.NewGuid() + "@test.hu", Name = "test user", Password = "******" }); var companyId = managementClient.Company.Create(new Company { Name = "company" }); var serviceId = managementClient.Service.Create(new Service { CompanyId = companyId, Name = "service" }); var networkId = managementClient.Network.Create(new Network { CompanyId = companyId, ServiceId = serviceId, Name = "árvíztűrő tükörfúrógép" }); var apiKey = managementClient.Service.Get(serviceId).ApiKey; const int deviceCount = 10; const int messageCount = 2; var list = new List <string>(); Parallel.For(0, deviceCount, new ParallelOptions() { MaxDegreeOfParallelism = 2 }, (i) => { var deviceId = managementClient.Device.Create(new Device { CompanyId = companyId, ServiceId = serviceId, NetworkId = networkId, Name = "name" + i }); lock (list) { list.Add(deviceId); } }); var tasks = new List <Task>(); for (int i = 0; i < deviceCount; i++) { var deviceId = list[i]; var task = Task.Factory.StartNew(() => { var ocassionalConnectionClient = new OccasionallyConnectionClient(PlatformApi, deviceId, apiKey); for (var j = 0; j < messageCount; j++) { ocassionalConnectionClient.SendMessageTo(deviceId, "msg" + j); Thread.Sleep(100); } }); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); for (int i = 0; i < deviceCount; i++) { var deviceId = list[i]; var task = Task.Factory.StartNew(() => { var ocassionalConnectionClient = new OccasionallyConnectionClient(PlatformApi, deviceId, apiKey); for (var j = 0; j < messageCount; j++) { var message = ocassionalConnectionClient.ReceiveAndForgetMessage(); Assert.IsTrue(message.Payload.Contains("msg")); Assert.AreEqual(deviceId, message.SenderDeviceId); } }); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); }
public void SingleDeviceSendToTest() { var ocassionalConnectionClient = new OccasionallyConnectionClient(PlatformApi, _deviceId, _apiKey); ocassionalConnectionClient.SendMessageTo(_deviceId, "test"); }