LiveProzessorTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using NUnit.Framework;
  2. using Processor;
  3. using CSVReader;
  4. using System.Threading;
  5. using System.Diagnostics;
  6. using Networkreader;
  7. namespace Tests
  8. {
  9. public class LiveProzessorTest
  10. {
  11. private IProcessor IProzessor { get; set; }
  12. private NWEventReader NWEventReader { get; set; }
  13. private const string IPAdress = "127.0.0.1";
  14. private const int Port = 13000;
  15. private const int SampleRate = 4;//Hz
  16. private const int NrOfObjects = 101;
  17. private const int LiveDelay = 0;
  18. [SetUp]
  19. public void Setup()
  20. {
  21. }
  22. [Test]
  23. public void TestTimeOrder()
  24. {
  25. Assert.IsTrue(Startup(true));
  26. IProzessor.ReadNextValues().ToArray();
  27. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data
  28. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  29. Assert.AreEqual(NrOfObjects, retList.Length, 5);
  30. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  31. {
  32. if (i != 0)
  33. Assert.IsTrue(retList[i].Time >= retList[i - 1].Time);
  34. }
  35. }
  36. [Test]
  37. public void TestTimeOrderReverse()
  38. {
  39. Assert.IsTrue(Startup(true));
  40. IProzessor.ReadNextValues().ToArray();
  41. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data
  42. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  43. Assert.AreEqual(NrOfObjects, retList.Length, 5);
  44. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  45. {
  46. if (i != 0)
  47. Assert.IsTrue(retList[i].Time <= retList[i - 1].Time);
  48. }
  49. }
  50. [Test]
  51. public void TestReadTiming()
  52. {
  53. Assert.IsTrue(Startup(true));
  54. IProzessor.ReadNextValues().ToArray();
  55. Stopwatch watch = new Stopwatch();
  56. watch.Start();
  57. Thread.Sleep(1000 / SampleRate + LiveDelay); //wait for 2 seconds
  58. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count, 5); //read and expect 2*Samplerate values
  59. double time1 = watch.ElapsedMilliseconds;
  60. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for 2 seconds
  61. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count, 5);//read and expect 2*Samplerate values
  62. double time2 = watch.ElapsedMilliseconds;
  63. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for 2 seconds
  64. Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count, 5);//read and expect 2*Samplerate values
  65. double time3 = watch.ElapsedMilliseconds;
  66. watch.Stop();
  67. Assert.AreEqual(time1, time2 - time1, 100);//Times for same operation should not differ too much
  68. Assert.AreEqual(time2 - time1, time3 - time2, 100);//Times for same operation should not differ too much
  69. Assert.AreEqual(time1, 1000 / SampleRate, 50); //check if time is smaller than samplerate
  70. Assert.AreEqual(time2 - time1, 1000 / SampleRate, 50); //check if time is smaller than samplerate
  71. Assert.AreEqual(time3 - time2, 1000 / SampleRate, 50); //check if time is smaller than samplerate
  72. }
  73. [Test]
  74. public void TestTimeJump()
  75. {
  76. Assert.IsTrue(Startup(false));
  77. Thread.Sleep(2000);
  78. Assert.IsTrue(IProzessor.JumpToTimestamp(1, out _));
  79. IProzessor.ReadNextValues().ToArray();
  80. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data
  81. InputObject[] retList = IProzessor.ReadNextValues().ToArray();
  82. Assert.AreEqual(NrOfObjects, retList.Length, 5);
  83. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  84. {
  85. if (i != 0)
  86. Assert.IsTrue(retList[i].Time >= retList[i - 1].Time);
  87. }
  88. IProzessor.JumpToTimestamp(1, out double x);//jump forward 0.75 sec
  89. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data
  90. retList = IProzessor.ReadNextValues().ToArray();
  91. Assert.AreEqual(NrOfObjects, retList.Length, 5);
  92. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  93. {
  94. if (i != 0)
  95. Assert.IsTrue(retList[i].Time >= retList[i - 1].Time);
  96. }
  97. IProzessor.JumpToTimestamp(2, out double ff);//jump forward 0.75 sec
  98. IProzessor.ReverseTime();// and reverse Time
  99. Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data
  100. retList = IProzessor.ReadNextValues().ToArray();
  101. Assert.AreEqual(NrOfObjects, retList.Length, 5);
  102. for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order
  103. {
  104. if (i != 0)
  105. Assert.IsTrue(retList[i].Time <= retList[i - 1].Time);
  106. }
  107. }
  108. [Test]
  109. public void TestOutOfBoundsReading()
  110. {
  111. Assert.IsTrue(Startup(true));
  112. Assert.IsFalse(IProzessor.JumpToTimestamp(-1, out double x));
  113. Assert.IsFalse(IProzessor.JumpToTimestamp(100, out double y));
  114. Assert.IsFalse(IProzessor.JumpToTimestamp(double.MaxValue, out double z));
  115. Assert.IsFalse(IProzessor.JumpToTimestamp(double.MinValue, out double a));
  116. Assert.IsFalse(IProzessor.JumpToTimestamp(double.NaN, out double b));
  117. }
  118. [TearDown]
  119. public void TearDown()
  120. {
  121. IProzessor.StopStreaming();
  122. NWEventReader.StopReading();
  123. }
  124. private bool Startup(bool direction)
  125. {
  126. IProzessor = new Processor.Processor(IPAdress, Port);
  127. NWEventReader = new NWEventReader(IPAdress, 13001);
  128. IProzessor.StartStreaming(SampleRate, direction);
  129. NWEventReader.StartNWRead();
  130. Stopwatch watch = new Stopwatch();
  131. double starttime = double.NaN;
  132. //Try to start stream
  133. watch.Start();
  134. while (double.IsNaN(starttime))
  135. {
  136. starttime = IProzessor.GetOldestTimeStamp();
  137. Thread.Sleep(10);
  138. if (watch.ElapsedMilliseconds > 5000)
  139. {
  140. IProzessor.StopStreaming();
  141. return false;
  142. }
  143. }
  144. watch.Stop();
  145. if (IProzessor.JumpToTimestamp(starttime, out _))
  146. return true;
  147. else
  148. return false;
  149. }
  150. }
  151. }