public void Multiple_IStateRecorders_Are_Written_In_Chronological_Order_Into_A_Shared_Stream()
        {
            var guid = Guid.NewGuid();
            var stream = new ConcurrentStream(new MemoryStream());

            var stateController = new SimpleStateController { Guid = guid };

            var stateResolver = new StateResolver();
            stateResolver.Add(stateController);

            var captureService = new CaptureService
            {
                Stream = stream,
                StateResolver = stateResolver
            };

            var recorder1 = new Mock<IStateRecorder>();
            var recorder2 = new Mock<IStateRecorder>();

            var recorderBuffer1 = new SimpleBuffer<ICaptureState>();
            var recorderBuffer2 = new SimpleBuffer<ICaptureState>();

            recorder1.Setup(b => b.Buffer).Returns(recorderBuffer1);
            recorder2.Setup(b => b.Buffer).Returns(recorderBuffer2);

            //push some dummy states into the buffers

            var state1 = new Mock<ICaptureState>();
            var firstTimestamp = DateTime.Now.AddDays(1);
            state1.Setup(s => s.Timestamp).Returns(firstTimestamp);
            state1.Setup(s => s.Guid).Returns(guid);

            var state2 = new Mock<ICaptureState>();
            var secondTimestamp = DateTime.Now.AddDays(2);
            state2.Setup(s => s.Timestamp).Returns(secondTimestamp);
            state2.Setup(s => s.Guid).Returns(guid);

            recorderBuffer1.Enqueue(state1.Object);
            recorderBuffer2.Enqueue(state2.Object);

            //add the 2nd recorder in first as it's timestamp is at a time in the future beyond the first recorder's state
            captureService.Add(recorder2.Object);

            captureService.Add(recorder1.Object);

            captureService.Start();
            captureService.Flush();

            Assert.Equal(2, captureService.CaptureStream.Count);

            //open the stream for reading now
            stream.Position = 0;

            var captureStream = new CaptureStream(stream, FileAccess.Read, stateResolver);

            //first value read back should be from state1
            Assert.Equal(firstTimestamp, captureStream.Read().Timestamp);
            Assert.Equal(secondTimestamp, captureStream.Read().Timestamp);
        }
        public void CaptureReader_Correctly_Assigns_Offsets_To_ICaptureState_Instances_When_Read()
        {
            var stateController = new WeatherStateController(new WeatherSimulator());

            var stateResolver = new StateResolver();
            stateResolver.Add(stateController);

            var ms = VastPark.FrameworkBase.IO.EmbeddedResource.GetMemoryStream("Continuum.Test.Samples.weather-simulation.continuum", Assembly.GetExecutingAssembly());
            ms.Position = 0;

            var stream = new ConcurrentStream(ms);
            var captureStream = new CaptureStream(stream, FileAccess.Read, stateResolver);

            var lastOffset = 0d;

            for (int i = 0; i < captureStream.Count; i++)
            {
                var state = captureStream.Read();

                Assert.True(state.Offset >= 0);
                Assert.True(state.Offset >= lastOffset);

                lastOffset = state.Offset;
            }
        }
        public void CaptureStream_Supports_Reading_A_File_While_It_Is_Being_Written_From_Another_Thread()
        {
            //base bytes to feed into the stream at random
            var bytes = VastPark.FrameworkBase.IO.EmbeddedResource.GetBytes("Continuum.Test.Samples.weather-simulation.continuum", Assembly.GetExecutingAssembly());
            var endOfHeader = 0L;

            using(var ms = new MemoryStream(bytes))
            {
                var reader = new CaptureReader(new ConcurrentStream(ms), new StateResolver());

                endOfHeader = reader.BaseStream.Position;
            }

            var stream = new MemoryStream();
            var baseStream = new ConcurrentStream(stream);
            var binaryWriter = new BinaryWriter(stream);
            var stateResolver = new StateResolver();
            stateResolver.Add(new Continuum.Test.Mock.WeatherStateController(new WeatherSimulator()));

            //push in some bytes to get it started, enough for the header so that the stream won't throw on construction
            binaryWriter.Write(bytes, 0, (int)endOfHeader);

            var offset = baseStream.Position;
            baseStream.Position = 0;

            var captureStream = new CaptureStream(baseStream, FileAccess.Read, stateResolver);

            //write in a little more so we don't have a full state yet
            binaryWriter.Seek((int)offset, SeekOrigin.Begin);
            //binaryWriter.Write(bytes, (int)offset, 5);

            //offset += 5;

            var readStates = 0;
            var lockObject = new object();

            //create a thread to randomly write chunks of bytes into the base stream
            ThreadPool.QueueUserWorkItem(w =>
            {
                while (offset < bytes.LongLength)
                {
                    var bytesToWrite = new Random().Next(1, 200);

                    if (bytesToWrite > bytes.Length - offset)
                    {
                        bytesToWrite = Convert.ToInt32(bytes.LongLength - offset);
                    }

                    lock (lockObject)
                    {
                        binaryWriter.Seek((int)offset, SeekOrigin.Begin);
                        binaryWriter.Write(bytes, (int)offset, bytesToWrite);
                        offset += bytesToWrite;
                    }

                    Thread.Sleep(200);
                }

                Assert.Equal(bytes.LongLength, baseStream.Length);

                lock (lockObject)
                {
                    binaryWriter.Seek(0, SeekOrigin.Begin);

                    for (int i = 0; i < bytes.LongLength; i++)
                    {
                        Assert.Equal(bytes[i], (byte)baseStream.ReadByte());
                    }
                }
            });

            while (captureStream.Position < captureStream.Count)
            {
                lock (lockObject)
                {
                    if (captureStream.Peek() != null)
                    {
                        captureStream.Read();
                        readStates++;
                    }
                }
            }

            Assert.Equal(captureStream.Count, readStates);
        }
        public void Selecting_A_Position_When_The_Current_Position_Is_Zero_Will_Read_That_Node()
        {
            var controller = new WeatherStateController(new WeatherSimulator());
            _StateResolver = new StateResolver();
            _StateResolver.Add(controller);

            var ms = VastPark.FrameworkBase.IO.EmbeddedResource.GetMemoryStream("Continuum.Test.Samples.weather-simulation.continuum", Assembly.GetExecutingAssembly());

            var stream = new ConcurrentStream(new MemoryStream(ms.ToArray()));
            var captureStream = new CaptureStream(stream, System.IO.FileAccess.Read, _StateResolver);

            var desiredNode = Math.Round(Convert.ToDouble(captureStream.Count) / 2);

            captureStream.Position = (long)desiredNode - 1; //if the desired node is 50, its required to put the stream at position 49 in order to read it

            var seekNode = captureStream.Read();

            //manually read nodes, when the desired node is reached it should match the node pulled out previously
            stream = new ConcurrentStream(new MemoryStream(ms.ToArray()));
            captureStream = new CaptureStream(stream, FileAccess.Read, _StateResolver);

            ICaptureState actualNode = null;

            for (int i = 0; i < desiredNode; i++)
            {
                actualNode = captureStream.Read();
            }

            Assert.Equal(actualNode.Guid, seekNode.Guid);
            Assert.Equal(actualNode.Offset, seekNode.Offset);
            Assert.Equal(actualNode.Timestamp, seekNode.Timestamp);

            for (int i = 0; i < actualNode.Data.Length; i++)
            {
                Assert.Equal(actualNode.Data[i], seekNode.Data[i]);
            }
        }
        public void Selecting_A_Position_Forward_In_The_Stream_When_The_Current_Position_Is_Not_Zero_Will_Read_That_Node()
        {
            _StateResolver = new StateResolver();
            var controller = new WeatherStateController(new WeatherSimulator());

            _StateResolver.Add(controller);

            var ms = VastPark.FrameworkBase.IO.EmbeddedResource.GetMemoryStream("Continuum.Test.Samples.weather-simulation.continuum", Assembly.GetExecutingAssembly());

            var stream = new ConcurrentStream(new MemoryStream(ms.ToArray()));
            var captureStream = new CaptureStream(stream, System.IO.FileAccess.Read, _StateResolver);

            var desiredNode = Math.Round(Convert.ToDouble(captureStream.Count) / 2);

            //read up the number of nodes to move the position pointer forwards in the stream
            for (int i = 0; i < desiredNode; i++)
            {
                captureStream.Read();
            }

            //seek to the new position
            captureStream.Position += 2;

            //this node is equivalent to node desiredNode + 3
            var seekNode = captureStream.Read();

            //manually read nodes, when the desired node is reached it should match the node pulled out previously
            stream = new ConcurrentStream(new MemoryStream(ms.ToArray()));
            captureStream = new CaptureStream(stream, FileAccess.Read, _StateResolver);

            ICaptureState actualNode = null;

            for (int i = 0; i < desiredNode + 3; i++)
            {
                actualNode = captureStream.Read();
            }

            Assert.Equal(actualNode.Guid, seekNode.Guid);
            Assert.Equal(actualNode.Offset, seekNode.Offset);
            Assert.Equal(actualNode.Timestamp, seekNode.Timestamp);

            for (int i = 0; i < actualNode.Data.Length; i++)
            {
                Assert.Equal(actualNode.Data[i], seekNode.Data[i]);
            }
        }
        public void Custom_Capture_Formats_Are_Recorded_Correctly()
        {
            var resetEvent = new SlimResetEvent(20);

            var desiredStates = 200;
            var generatedStates = 0;

            var ms = new MemoryStream();
            var stream = new ConcurrentStream(ms);

            var weatherSimulator = new WeatherSimulator();

            _StateResolver = new StateResolver();
            //build the controller that manages weather states
            var controller = new WeatherStateController(weatherSimulator);

            //watch the simluator for the desired number of states
            weatherSimulator.NewTemperature += delegate
            {
                generatedStates++;

                if (generatedStates >= desiredStates)
                {
                    weatherSimulator.Stop();
                    resetEvent.Set();
                }
            };

            //register it with the capture service
            _StateResolver = new StateResolver();
            _StateResolver.Add(controller);

            //create the capture stream, will create an entry in the SAT for the WeatherStateController
            var captureStream = new CaptureStream(stream, FileAccess.Write, _StateResolver);

            //pass the controller the stream so that it can be written to
            controller.Initialise(captureStream);

            //start the simulator, the controller will watch for changes internally
            weatherSimulator.Start();

            //wait until the event is raised [desiredStates] number of times
            resetEvent.Wait();

            Assert.Equal(generatedStates, captureStream.Count);

            //rewind the stream and open it for reading, verify the values match
            stream.Position = 0;

            captureStream = new CaptureStream(stream, FileAccess.Read, _StateResolver);

            for (int i = 0; i < captureStream.Count; i++)
            {
                var state = captureStream.Read();

                Assert.Equal(weatherSimulator.History[i], (state as WeatherCaptureState).Temperature);
            }
        }
        public void CaptureStream_Supports_Writing_A_State_And_Then_Reading_It()
        {
            var stateController = new WeatherStateController(new WeatherSimulator());
            var stateResolver = new StateResolver();
            stateResolver.Add(stateController);

            var baseStream = new ConcurrentStream(new MemoryStream());
            var captureStream = new CaptureStream(baseStream, FileAccess.ReadWrite, stateResolver);

            Assert.True(captureStream.CanRead);
            Assert.True(captureStream.CanWrite);

            //write a state then read it back
            var expectedBytes = Guid.NewGuid().ToByteArray();

            captureStream.Write(new WeatherCaptureState(expectedBytes, stateController.Guid, DateTime.UtcNow, 0));

            //the read pointer is still at the start of the stream
            var readState = captureStream.Read();

            for (int i = 0; i < readState.Data.Length; i++)
            {
                Assert.Equal(expectedBytes[i], readState.Data[i]);
            }
        }