def test_nested_groups(self): program = lp_program.Program('foo') with self.assertRaisesRegexp(ValueError, r'group\(\) cannot be nested.'): with program.group('foo'): with program.group('bar'): program.add_node(mock.Mock())
def test_add_node(self): program = lp_program.Program('foo') node_foo = mock.Mock() program.add_node(node_foo, 'foo') node_bar = mock.Mock() program.add_node(node_bar, 'bar') self.assertEqual(program.groups['foo'], [node_foo]) self.assertEqual(program.groups['bar'], [node_bar])
def test_one_py_node_program(self): has_run = threading.Event() def run(): has_run.set() program = lp_program.Program('test') program.add_node(python.PyNode(run), label='run') launch.launch(program) has_run.wait()
def test_use_label_in_node_and_group(self): program = lp_program.Program('foo') with self.assertRaisesRegexp(ValueError, 'label does not match the current group'): with program.group('foo'): program.add_node(mock.Mock(), label='bar') # If the label matches the current group, then it's ok. with program.group('foo'): program.add_node(mock.Mock(), label='foo')
def test_duplicated_label(self): program = lp_program.Program('foo') node1 = mock.Mock() node2 = mock.Mock() program.add_node(node1, 'foo') program.add_node(node2, 'foo') with program.group('bar'): program.add_node(node1) with program.group('bar'): program.add_node(node2) self.assertEqual(program.groups['foo'], [node1, node2]) self.assertEqual(program.groups['bar'], [node1, node2])
def test_add_nodes_to_group(self): program = lp_program.Program('foo') nodes_foo = [mock.Mock(), mock.Mock()] with program.group('foo'): for node in nodes_foo: program.add_node(node) nodes_bar = [mock.Mock(), mock.Mock()] with program.group('bar'): for node in nodes_bar: program.add_node(node) self.assertEqual(program.groups['foo'], nodes_foo) self.assertEqual(program.groups['bar'], nodes_bar)
def test_handle_exception(self): def run(): raise RuntimeError('Launchpad has stopped working') program = lp_program.Program('test') program.add_node(python.PyNode(run), label='run') # Mock os.kill() so as to make sure it is called. has_run = threading.Event() def mock_kill(pid, unused_signal): self.assertEqual(pid, os.getpid()) has_run.set() with mock.patch.object(os, 'kill', mock_kill): launch.launch(program) has_run.wait()
def test_get_all_nodes(self): nodes = [] program = lp_program.Program('test') node_foo = mock.Mock() nodes.append(node_foo) program.add_node(node_foo, 'foo') node_bar = mock.Mock() nodes.append(node_bar) program.add_node(node_bar, 'bar') with program.group('baz'): node_baz = mock.Mock() nodes.append(node_baz) program.add_node(node_baz) self.assertCountEqual(program.get_all_nodes(), nodes)
def test_add_group_with_empty_label(self): program = lp_program.Program('foo') with self.assertRaisesRegexp(ValueError, 'Label should not be empty.'): with program.group(''): program.add_node(mock.Mock())
def test_add_node_with_empty_label(self): program = lp_program.Program('foo') with self.assertRaisesRegexp(ValueError, 'Label should not be empty.'): program.add_node(mock.Mock(autospec=base.Node), label='')