public void Single_Component_Is_Updated_After_Incoming_Data_Without_Logging() { byte ID = 0; // Value = 1337 = 0x539 byte valMSB = 0x05; byte valLSB = 0x39; LoadComponent expectedResult = new LoadComponent(0, "", ""); expectedResult.Set(1337); TestStandMapping mapping = new TestStandMapping(); Session session = new Session(mapping); Mock <IDataLog> dataMock = new Mock <IDataLog>(); Mock <ISerialPort> serialMock = new Mock <ISerialPort>(); byte[] buffer = { 0xAA, 0xBB, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x03, ID, valMSB, valLSB, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xBB }; int i = 0; serialMock.Setup(x => x.IsOpen).Returns(true).Callback(() => Console.WriteLine("IsOpen called")); serialMock.Setup(x => x.BytesToRead).Returns(buffer.Length - i).Callback(() => Console.WriteLine("BytesToRead called")); serialMock.Setup(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Returns((byte[] b, int o, int c) => { int nbytes = Math.Min(c, buffer.Length - i); Array.Copy(buffer, i, b, 0, nbytes); i += nbytes; i = (i >= buffer.Length) ? 0 : i; Console.WriteLine("Got {0} bytes", nbytes); return(nbytes); }).Callback(() => Console.WriteLine("Read called")); bool wait = true; IOThread conn = new IOThread(dataMock.Object, ref session); dataMock.Setup(x => x.Enqueue(It.IsAny <DataPacket>())).Callback((DataPacket dp) => { session.UpdateComponents(dp.Bytes); conn.StopConnection(); wait = false; }); conn.StartConnection(serialMock.Object, null); Console.WriteLine("Thread started"); Stopwatch watch = new Stopwatch(); watch.Start(); while (wait && watch.ElapsedMilliseconds < 2000) { Thread.Sleep(100); } Assert.AreEqual(expectedResult.Newtons(), ((LoadComponent)mapping.ComponentsByID()[ID]).Newtons()); }
public void Verify_Timed_Servo_Command() { TestStandMapping mapping = new TestStandMapping(); Session session = new Session(mapping); DataStore dataStore = new DataStore(session); Mock <IUserInterface> ui = new Mock <IUserInterface>(); Mock <LogThread> logThread = new Mock <LogThread>(dataStore); IOThread ioThread = new IOThread(dataStore, ref session); Program p = new Program(dataStore, logThread.Object, ioThread, ui.Object); ServoCommand cmd1 = new ServoCommand(6, 69.0f); ServoCommand cmd2 = new ServoCommand(6, 0.0f); byte[] actual = {}; Mock <ISerialPort> serial = new Mock <ISerialPort>(); serial.Setup(x => x.IsOpen).Returns(true); serial.Setup(x => x.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback((byte[] b, int o, int c) => { actual = b; }); ioThread.StartConnection(serial.Object, null); p.OnTimedCommand(cmd1, cmd2, 2000); Thread.Sleep(IOThread.AckWaitMillis - 10); byte[] expected1 = { 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0x03, 0x06, 0xB0, 0xA3, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF }; Assert.AreEqual(expected1, actual); Thread.Sleep(2000 - IOThread.AckWaitMillis - 10); byte[] expected2 = { 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0x03, 0x06, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF }; Assert.AreEqual(expected2, actual); ioThread.StopConnection(); }
public void Verify_Solenoid_Close_Command() { TestStandMapping mapping = new TestStandMapping(); Session session = new Session(mapping); DataStore dataStore = new DataStore(session); Mock <IUserInterface> ui = new Mock <IUserInterface>(); Mock <LogThread> logThread = new Mock <LogThread>(dataStore); IOThread ioThread = new IOThread(dataStore, ref session); Program p = new Program(dataStore, logThread.Object, ioThread, ui.Object); SolenoidCommand sc = new SolenoidCommand(3, false); byte[] actual = {}; Mock <ISerialPort> serial = new Mock <ISerialPort>(); serial.Setup(x => x.IsOpen).Returns(true); serial.Setup(x => x.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback((byte[] b, int o, int c) => { actual = b; }); ioThread.StartConnection(serial.Object, null); p.OnCommand(sc); Thread.Sleep(IOThread.AckWaitMillis - 10); ioThread.StopConnection(); byte[] expected = { 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0x03, 0x03, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF }; Assert.AreEqual(expected, actual); }