Ejemplo n.º 1
0
        public void DepthVideo_Is_Drawn_When_RenderDepth_Is_True_And_The_DepthVideoOutputTarget_Is_Defined_And_Valid()
        {
            VastPark.Legacy.Environment.Initialise(System.IO.Path.GetTempPath());

            var context = new ImmlDocument();
            var parkEngine = new Mock<IParkEngine>();
            parkEngine.Setup(c => c.Context).Returns(context);

            var renderEngine = new Mock<IRenderEngine>();
            parkEngine.Setup(r => r.RenderEngine).Returns(renderEngine.Object);

            var scriptEngine = new Mock<IScriptEngine>();
            scriptEngine.Setup(s => s.WriteLine(It.IsAny<string>())).Callback<string>(a => System.Diagnostics.Debug.WriteLine(a));
            parkEngine.Setup(s => s.ScriptEngine).Returns(scriptEngine.Object);

            var primitive = ImmlElementFactory.CreatePrimitive();
            primitive.Name = "DepthTarget";

            context.Add(primitive);

            var timePeriod = TimeSpan.FromSeconds(30); //allow up to 30 seconds for the context to become ready
            var slimResetEvent = new SlimResetEvent(100);
            var start = DateTime.Now;
            var textureWritten = false;

            Texture.TextureBytesLoaded += delegate
            {
                textureWritten = true;
            };

            ThreadPool.QueueUserWorkItem(w =>
            {
                using (var plugin = new OpenNiPlugin())
                {
                    plugin.SetParkEngine(parkEngine.Object);
                    plugin.RenderDepth = true;
                    plugin.DepthVideoOutputTarget = primitive.Name;
                    plugin.Load();
                    plugin.Enabled = true;

                    while (DateTime.Now.Subtract(start) < timePeriod)
                    {
                        plugin.Update();

                        if (textureWritten)
                        {
                            break;
                        }

                        Thread.Sleep(16);
                    }

                    slimResetEvent.Set();
                }
            });

            slimResetEvent.Wait();

            Assert.True(textureWritten);
        }
Ejemplo n.º 2
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.º 3
0
        public void Scheduler_Executes_Tasks_Chronologically()
        {
            var tasks = 3;
            var executedTasks = 0;
            var guid = Guid.NewGuid();

            var resetEvent = new SlimResetEvent(20);
            var lastTimestamp = DateTime.MinValue;

            var stateController = new Mock<IStateController>();
            stateController.Setup(m => m.Guid).Returns(guid);
            stateController.Setup(m => m.Execute(It.IsAny<ICaptureState>())).Callback <ICaptureState>(delegate(ICaptureState state)
            {
                //validate the order of execution
                Assert.True(state.Timestamp > lastTimestamp);

                lastTimestamp = state.Timestamp;

                executedTasks++;

                if (executedTasks == tasks)
                {
                    resetEvent.Set();
                }
            });

            var stateResolver = new Mock<IStateResolver>();
            stateResolver.Setup(m => m.Find(It.IsAny<Guid>())).Returns(stateController.Object);

            var scheduler = new Scheduler();
            var taskFactory = new TaskFactory(stateResolver.Object);

            for (int i = 0; i < tasks; i++)
            {
                var mock = new Mock<ICaptureState>();
                mock.Setup(m => m.Guid).Returns(guid);
                mock.Setup(m => m.Timestamp).Returns(DateTime.UtcNow.AddMilliseconds(i + 1));

                var task = taskFactory.Create(mock.Object);
                scheduler.Add(task);
            }

            scheduler.Start();

            resetEvent.Wait();

            Assert.Equal(tasks, executedTasks);

            scheduler.Stop();
        }
Ejemplo n.º 4
0
        public void CaptureStream_Instances_Are_Played_Back_In_Chronological_Order()
        {
            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();
        }
Ejemplo n.º 5
0
        public void Scheduler_Executes_Tasks_Within_Fifty_Milliseconds_Of_The_Desired_Time()
        {
            var marginForError = 50; //ms that the execution can be out by
            var tasks = 3;
            var executedTasks = 0;
            var guid = Guid.NewGuid();
            var start = DateTime.MinValue;
            var scheduler = new Scheduler();

            var resetEvent = new SlimResetEvent(20);
            var expectedExecutionOffset = new List<double>();

            var stateController = new Mock<IStateController>();
            stateController.Setup(m => m.Guid).Returns(guid);
            stateController.Setup(m => m.Execute(It.IsAny<ICaptureState>())).Callback<ICaptureState>(delegate(ICaptureState state)
            {
                //validate the time offset as being roughly simlar to the expected offset
                var actualOffset = HighResClock.Now.Subtract(scheduler.Started).TotalMilliseconds;
                var expectedOffset = expectedExecutionOffset[executedTasks];

                Assert.True(actualOffset <= expectedOffset + marginForError); //ok if its late by the marginForError

                executedTasks++;

                if (executedTasks == tasks)
                {
                    resetEvent.Set();
                }
            });

            var stateResolver = new Mock<IStateResolver>();
            stateResolver.Setup(m => m.Find(It.IsAny<Guid>())).Returns(stateController.Object);

            var taskFactory = new TaskFactory(stateResolver.Object);

            for (int i = 0; i < tasks; i++)
            {
                var mock = new Mock<ICaptureState>();
                mock.Setup(m => m.Guid).Returns(guid);
                mock.Setup(m => m.Timestamp).Returns(DateTime.UtcNow.AddMilliseconds(i * 30));
                mock.Setup(m => m.Offset).Returns(i * 30);

                var task = taskFactory.Create(mock.Object);
                scheduler.Add(task);

                expectedExecutionOffset.Add(task.DesiredExecution.TotalMilliseconds);
            }

            scheduler.Start();

            resetEvent.Wait();

            Assert.Equal(tasks, executedTasks);

            scheduler.Stop();
        }
Ejemplo n.º 6
0
        public void When_The_Kinect_Is_Connected_The_Plugin_Successfully_Loads()
        {
            VastPark.Legacy.Environment.Initialise(System.IO.Path.GetTempPath());

            var timePeriod = TimeSpan.FromSeconds(30); //allow up to 30 seconds for the context to become ready
            var slimResetEvent = new SlimResetEvent(100);
            var start = DateTime.Now;
            var contextReady = false;

            ThreadPool.QueueUserWorkItem(w =>
            {
                using(var plugin = new OpenNiPlugin())
                {
                    plugin.Load();
                    plugin.Enabled = true;

                    while (DateTime.Now.Subtract(start) < timePeriod)
                    {
                        plugin.Update();

                        if (plugin.IsContextReady)
                        {
                            break;
                        }

                        Thread.Sleep(16);
                    }

                    contextReady = plugin.IsContextReady;
                    slimResetEvent.Set();
                }
            });

            slimResetEvent.Wait();

            Assert.True(contextReady);
        }
Ejemplo n.º 7
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);
            }
        }