Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }