using NUnit.Framework; using Processor; using CSVReader; using System.Threading; using System.Diagnostics; using Networkreader; namespace Tests { public class LiveProzessorTest { private IProcessor IProzessor { get; set; } private NWEventReader NWEventReader { get; set; } private const string IPAdress = "127.0.0.1"; private const int Port = 13000; private const int SampleRate = 4;//Hz private const int NrOfObjects = 101; private const int LiveDelay = 0; [SetUp] public void Setup() { } [Test] public void TestTimeOrder() { Assert.IsTrue(Startup(true)); IProzessor.ReadNextValues().ToArray(); Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length, 5); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time >= retList[i - 1].Time); } } [Test] public void TestTimeOrderReverse() { Assert.IsTrue(Startup(true)); IProzessor.ReadNextValues().ToArray(); Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length, 5); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time <= retList[i - 1].Time); } } [Test] public void TestReadTiming() { Assert.IsTrue(Startup(true)); IProzessor.ReadNextValues().ToArray(); Stopwatch watch = new Stopwatch(); watch.Start(); Thread.Sleep(1000 / SampleRate + LiveDelay); //wait for 2 seconds Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count, 5); //read and expect 2*Samplerate values double time1 = watch.ElapsedMilliseconds; Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for 2 seconds Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count, 5);//read and expect 2*Samplerate values double time2 = watch.ElapsedMilliseconds; Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for 2 seconds Assert.AreEqual(NrOfObjects, IProzessor.ReadNextValues().Count, 5);//read and expect 2*Samplerate values double time3 = watch.ElapsedMilliseconds; watch.Stop(); Assert.AreEqual(time1, time2 - time1, 100);//Times for same operation should not differ too much Assert.AreEqual(time2 - time1, time3 - time2, 100);//Times for same operation should not differ too much Assert.AreEqual(time1, 1000 / SampleRate, 50); //check if time is smaller than samplerate Assert.AreEqual(time2 - time1, 1000 / SampleRate, 50); //check if time is smaller than samplerate Assert.AreEqual(time3 - time2, 1000 / SampleRate, 50); //check if time is smaller than samplerate } [Test] public void TestTimeJump() { Assert.IsTrue(Startup(false)); Thread.Sleep(2000); Assert.IsTrue(IProzessor.JumpToTimestamp(1, out _)); IProzessor.ReadNextValues().ToArray(); Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data InputObject[] retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length, 5); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time >= retList[i - 1].Time); } IProzessor.JumpToTimestamp(1, out double x);//jump forward 0.75 sec Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length, 5); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time >= retList[i - 1].Time); } IProzessor.JumpToTimestamp(2, out double ff);//jump forward 0.75 sec IProzessor.ReverseTime();// and reverse Time Thread.Sleep(1000 / SampleRate + LiveDelay);//wait for Data retList = IProzessor.ReadNextValues().ToArray(); Assert.AreEqual(NrOfObjects, retList.Length, 5); for (int i = 0; i < retList.Length; i++)//check if data timestamp is in right order { if (i != 0) Assert.IsTrue(retList[i].Time <= retList[i - 1].Time); } } [Test] public void TestOutOfBoundsReading() { Assert.IsTrue(Startup(true)); Assert.IsFalse(IProzessor.JumpToTimestamp(-1, out double x)); Assert.IsFalse(IProzessor.JumpToTimestamp(100, out double y)); Assert.IsFalse(IProzessor.JumpToTimestamp(double.MaxValue, out double z)); Assert.IsFalse(IProzessor.JumpToTimestamp(double.MinValue, out double a)); Assert.IsFalse(IProzessor.JumpToTimestamp(double.NaN, out double b)); } [TearDown] public void TearDown() { IProzessor.StopStreaming(); NWEventReader.StopReading(); } private bool Startup(bool direction) { IProzessor = new Processor.Processor(IPAdress, Port); NWEventReader = new NWEventReader(IPAdress, 13001); IProzessor.StartStreaming(SampleRate, direction); NWEventReader.StartNWRead(); Stopwatch watch = new Stopwatch(); double starttime = double.NaN; //Try to start stream watch.Start(); while (double.IsNaN(starttime)) { starttime = IProzessor.GetOldestTimeStamp(); Thread.Sleep(10); if (watch.ElapsedMilliseconds > 5000) { IProzessor.StopStreaming(); return false; } } watch.Stop(); if (IProzessor.JumpToTimestamp(starttime, out _)) return true; else return false; } } }