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 CaptureService_Can_Write_A_Capture_To_Disk_When_Backed_By_A_FileStream()
        {
            var statesToWrite = 100;
            var simulator = new WeatherSimulator();
            var controller = new WeatherStateController(simulator);

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

            var tmpFile = System.IO.Path.GetTempFileName();

            try
            {
                using (var ms = new FileStream(tmpFile,FileMode.Create))
                {
                    var baseStream = new ConcurrentStream(ms);
                    var captureService = new CaptureService
                    {
                        Stream = baseStream,
                        StateResolver = stateResolver
                    };

                    captureService.Start();

                    controller.Initialise(captureService.CaptureStream);

                    simulator.Start();

                    var count = 0;
                    var resetEvent = new SlimResetEvent(20);

                    simulator.NewTemperature += delegate
                    {
                        count++;

                        if (count == statesToWrite)
                        {
                            simulator.Stop();
                            resetEvent.Set();
                        }
                    };

                    resetEvent.Wait();

                    captureService.Flush();
                }

                Assert.True(File.Exists(tmpFile));
                Assert.True(new FileInfo(tmpFile).Length > 0);

            }
            finally
            {
                File.Delete(tmpFile);
            }
        }
Beispiel #3
0
        public void PlaybackService_Supports_Multiple_ICaptureStream_Instances()
        {
            var resetEvent = new SlimResetEvent(20);
            var expectedStates = long.MaxValue;
            var executedStates = 0;
            var stateController = new WeatherStateController(new WeatherSimulator());

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

            stateController.StateExecuted += delegate
            {
                executedStates++;

                if (executedStates == expectedStates)
                {
                    resetEvent.Set();
                }
            };

            var playbackService = new PlaybackService(stateResolver);

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

            var stream1 = new ConcurrentStream(new MemoryStream(ms.ToArray()));
            var captureStream1 = new CaptureStream(stream1, System.IO.FileAccess.Read, stateResolver);

            var stream2 = new ConcurrentStream(new MemoryStream(ms.ToArray()));
            var captureStream2 = new CaptureStream(stream2, System.IO.FileAccess.Read, stateResolver);

            expectedStates = captureStream1.Count + captureStream2.Count;

            playbackService.Add(captureStream1);
            playbackService.Add(captureStream2);

            playbackService.Start();

            resetEvent.Wait();
        }
Beispiel #4
0
        public void When_A_Filter_Is_Applied_That_Returns_True_The_State_Is_Not_Scheduled_For_Playback()
        {
            var stateController = new WeatherStateController(new WeatherSimulator());
            var stateResolver = new StateResolver();
            stateResolver.Add(stateController);

            var scheduler = new Mock<IScheduler>();
            var taskFactory = new Mock<ITaskFactory>();

            var playbackService = new PlaybackService(stateResolver, scheduler.Object, taskFactory.Object);

            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);

            playbackService.Add(captureStream);

            //add in a filter that will always return true to filter out the states
            playbackService.Add(new StreamFilter(f => true));

            playbackService.Start();

            //task factory should not have built any tasks
            taskFactory.Verify(t => t.Create(It.IsAny<ICaptureState>()), Times.Never());
            scheduler.Verify(s => s.Add(It.IsAny<ITask>()), Times.Never());
        }
Beispiel #5
0
        public void When_A_Codec_Is_Not_Available_An_Event_Is_Raised_With_The_Guid_Of_The_Required_Codec()
        {
            var stateController = new WeatherStateController(new WeatherSimulator());

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

            var playbackService = new PlaybackService(stateResolver);

            playbackService.CodecRequired += delegate(object sender, CodecRequiredEventArgs e)
            {
                Assert.Equal(stateController.Guid, e.Guid);
            };

            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);

            playbackService.Add(captureStream);
        }
Beispiel #6
0
        public void When_A_Codec_Is_Not_Available_An_Event_Is_Raised_Giving_The_Caller_An_Opportunity_To_Acquire_It()
        {
            var stateController = new WeatherStateController(new WeatherSimulator());

            var stateResolver = new StateResolver();

            var eventRaised = false;

            var playbackService = new PlaybackService(stateResolver);

            playbackService.CodecRequired += delegate
            {
                eventRaised = true;
            };

            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);

            playbackService.Add(captureStream);

            Assert.True(eventRaised);
        }
        public void CaptureWriter_Buids_A_Valid_Continuum_Header()
        {
            var baseStream = new ConcurrentStream(new MemoryStream());
            var stateResolver = new StateResolver();

            var stateController = new WeatherStateController(new WeatherSimulator());

            stateResolver.Add(stateController);

            var captureWriter = new CaptureWriter(baseStream, stateResolver);

            baseStream.Position = 0;

            byte[] header = new byte[Constants.CONTINUUM_HEADER_SIGNATURE.Length];
            baseStream.Read(header, 0, header.Length);

            for (int i = 0; i < Constants.CONTINUUM_HEADER_SIGNATURE.Length; i++)
            {
                Assert.Equal(Constants.CONTINUUM_HEADER_SIGNATURE[i], header[i]);
            }

            //dont worry about 4 bytes reserved for version info at the moment
            baseStream.Position = baseStream.Position + 4;

            //ignore the timestamp for now
            baseStream.Position = baseStream.Position + 8;

            //ignore the count property for now
            baseStream.Position = baseStream.Position + 8;

            //ignore the length property for now
            baseStream.Position = baseStream.Position + 8;

            //read the SAT, there should be at least one entry for it to be valid
            //the SAT looks like this:
            //<lengthOfSac><firstId of type short><delimiter>

            var lengthOfSatBuffer = new byte[4];
            baseStream.Read(lengthOfSatBuffer, 0, lengthOfSatBuffer.Length);

            var lengthOfSat = BitConverter.ToInt32(lengthOfSatBuffer, 0);

            Assert.True(lengthOfSat > 0);

            //read back the sat
            var readSat = new Dictionary<short, Guid>();

            for (int i = 0; i < lengthOfSat; i++)
            {
                var idBuffer = new byte[2];
                baseStream.Read(idBuffer, 0, idBuffer.Length);

                var guid = new byte[16]; //guids are 16-bytes
                baseStream.Read(guid, 0, guid.Length);

                readSat.Add(BitConverter.ToInt16(idBuffer, 0), new Guid(guid));
            }

            //match the sat
            foreach (var kvp in stateResolver.AllocationTable)
            {
                Assert.True(readSat.ContainsKey(kvp.Value));
                Assert.True(readSat[kvp.Value] == kvp.Key);
            }
        }
        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 Selecting_A_Position_Beyond_The_Length_Of_The_Stream_Will_Throw_An_Exception()
        {
            _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);

            Assert.Throws(typeof(Exception), new Assert.ThrowsDelegate(delegate { captureStream.Position = captureStream.Count + 1; }));
        }
        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]);
            }
        }