public void SimulationStateChangedOnNodesTest() { var simulation = new MyLocalSimulation(); var handler = new MySimulationHandler(simulation); MyProject project = new MyProject { Network = new MyNetwork() }; project.CreateWorld(typeof(MyTestingWorld)); var node = project.CreateNode<TestingNode>(); node.Event = new AutoResetEvent(false); project.Network.AddChild(node); var connection = new MyConnection(project.Network.GroupInputNodes[0], project.Network.Children[0]); connection.Connect(); project.Network.PrepareConnections(); handler.Project = project; handler.UpdateMemoryModel(); handler.StartSimulation(oneStepOnly: false); node.Event.WaitOne(); Assert.Equal(MySimulationHandler.SimulationState.STOPPED, node.PreviousState); Assert.Equal(MySimulationHandler.SimulationState.RUNNING, node.CurrentState); handler.PauseSimulation(); node.Event.WaitOne(); Assert.Equal(MySimulationHandler.SimulationState.RUNNING, node.PreviousState); Assert.Equal(MySimulationHandler.SimulationState.PAUSED, node.CurrentState); handler.StartSimulation(oneStepOnly: true); node.Event.WaitOne(); // Here the sim goes from paused to RUNNING_STEP. Assert.Equal(MySimulationHandler.SimulationState.PAUSED, node.PreviousState); Assert.Equal(MySimulationHandler.SimulationState.RUNNING_STEP, node.CurrentState); node.Event.WaitOne(); // Here it goes to PAUSED. Assert.Equal(MySimulationHandler.SimulationState.RUNNING_STEP, node.PreviousState); Assert.Equal(MySimulationHandler.SimulationState.PAUSED, node.CurrentState); handler.StopSimulation(); node.Event.WaitOne(); Assert.Equal(MySimulationHandler.SimulationState.PAUSED, node.PreviousState); Assert.Equal(MySimulationHandler.SimulationState.STOPPED, node.CurrentState); handler.Finish(); }
public void FileCanBeReadWhenSimulationIsNotRunning() { string directory = Path.GetFullPath(@"Data\"); const string fileName = "csv_file_test.brain"; m_outputFileFullPath = Path.GetTempFileName(); var outputFileName = Path.GetFileName(m_outputFileFullPath); var outputFileDirectory = Path.GetDirectoryName(m_outputFileFullPath); string projectPath = directory + fileName; var simulation = new MyLocalSimulation(); // TODO(HonzaS): This should not be required! // The referenced assemblies get loaded only if a Type is required here. But since the serializer // is just looking for types by name, it doesn't force the runtime to load all of the assemblies. // In this case, we would miss the BasicNodes if the following line was deleted. // Two solutions: 1) Use the Managed Extensibility Framework or 2) load all referenced assemblies // explicitely (as a part of the BS testing framework). var csvNode = new MyCsvFileWriterNode(); MyProject project = MyProject.Deserialize(File.ReadAllText(projectPath), Path.GetDirectoryName(projectPath)); var handler = new MySimulationHandler(simulation) { Project = project }; // The CSV node MyNode node = project.Network.GetChildNodeById(6); PropertyInfo fileNameProperty = node.GetType().GetProperty("OutputFile", BindingFlags.Instance | BindingFlags.Public); fileNameProperty.SetValue(node, outputFileName); PropertyInfo directoryProperty = node.GetType().GetProperty("OutputDirectory", BindingFlags.Instance | BindingFlags.Public); directoryProperty.SetValue(node, outputFileDirectory); handler.UpdateMemoryModel(); handler.StateChanged += StateChanged; try { handler.StartSimulation(); m_continueEvent.WaitOne(); Assert.Throws<IOException>(() => { File.Open(m_outputFileFullPath, FileMode.Open, FileAccess.ReadWrite); }); // Every time we change simulation state, the StateChanged method gets notified. // We're changing the state several times here and the StateChanged methods checks that // the file that the Csv node uses is available for writing. // First, go through start->pause->stop. handler.PauseSimulation(); m_continueEvent.WaitOne(); handler.StopSimulation(); m_continueEvent.WaitOne(); // Now, try start->stop only. handler.StartSimulation(); m_continueEvent.WaitOne(); handler.StopSimulation(); m_continueEvent.WaitOne(); // The file should have been successfully opened three times - every time the simulation was paused or stopped. Assert.Equal(3, m_openCount); } finally { handler.Finish(); File.Delete(m_outputFileFullPath); m_outputFileFullPath = null; } }