public void PlaybackStreamReadsRecordedDataIfTimeOffsetPassed() { var currentDateTime = DateTime.Now; var currentDateTimeSourceMock = new Mock<ICurrentDateTimeSource>(); currentDateTimeSourceMock.Setup(currentDateTimeSource => currentDateTimeSource.GetCurrentDateTime()).Returns(() => currentDateTime); var memoryStream = new MemoryStream(); var recordingStream = new RecordingStreamBuilder(memoryStream).SetCurrentDateTimeSource(currentDateTimeSourceMock.Object).Build(); currentDateTime = currentDateTime.Add(TimeSpan.FromSeconds(2)); var pastData = new byte[] { 1, 2, 3, 4 }; recordingStream.Write(pastData, 0, pastData.Length); currentDateTime = currentDateTime.Add(TimeSpan.FromSeconds(3)); var presentData = new byte[] { 5, 6, 7 }; recordingStream.Write(presentData, 0, presentData.Length); currentDateTime = currentDateTime.Add(TimeSpan.FromSeconds(4)); var futureData = new byte[] { 8, 9, 10, 11 }; recordingStream.Write(futureData, 0, futureData.Length); memoryStream.Position = 0; currentDateTime = currentDateTime.Add(TimeSpan.FromSeconds(8)); var playbackStream = new PlaybackStreamBuilder(memoryStream).SetCurrentDateTimeSource(currentDateTimeSourceMock.Object).Build(); currentDateTime = currentDateTime.Add(TimeSpan.FromSeconds(5)); var buffer = new byte[24]; int read = playbackStream.Read(buffer, 0, buffer.Length); Assert.IsTrue(buffer.Take(read).SequenceEqual(pastData.Concat(presentData))); }
public void RecordingStreamWritesTimeOffsetLengthAndDataToTargetStream() { var targetStream = new MemoryStream(); var currentDateTimeSourceMock = new Mock<ICurrentDateTimeSource>(); var initialDateTime = DateTime.Now; var currentDateTime = initialDateTime; currentDateTimeSourceMock.Setup(currentDateTimeSource => currentDateTimeSource.GetCurrentDateTime()) .Returns(() => currentDateTime); var recordingStream = new RecordingStreamBuilder(targetStream).SetCurrentDateTimeSource(currentDateTimeSourceMock.Object).Build(); var timeOffset = TimeSpan.FromSeconds(3); currentDateTime = initialDateTime.Add(timeOffset); var data = new byte[] { 1, 2, 3, 4, 5 }; recordingStream.Write(data, 0, data.Length); var timeOffset2 = TimeSpan.FromSeconds(7); currentDateTime = initialDateTime.Add(timeOffset2); var data2 = new byte[] { 6, 7, 8, 9, 10, 11 }; recordingStream.Write(data2, 0, data2.Length); Assert.IsTrue(targetStream.ToArray().SequenceEqual( BitConverter.GetBytes(timeOffset.Ticks).Concat(BitConverter.GetBytes(data.Length)).Concat(data) .Concat(BitConverter.GetBytes(timeOffset2.Ticks).Concat(BitConverter.GetBytes(data2.Length)).Concat(data2)))); }