def test_profiling_already_enabled(self, mocked):
        """
        Worker method should raise exception if profiling already enabled.

        """
        config = self.create_config()
        dumper = _Dumper(object(), config, [])
        with self.assertRaises(NovaServiceProfilingException):
            dumper.work()
    def test_set_ended(self, mocked_start):
        """
        Tests that _ended attribute is set at the end of work()

        """
        config = self.create_config()
        dumper = _Dumper(object(), config, [])
        dumper._stop = True
        dumper.work()
        self.assertTrue(dumper._ended is not None)
    def test_outputs_init(self):
        """
        Tests that the list of outputs passed to init
        is saved to the dumper's outputs

        """
        config = self.create_config()
        outputs = ['a', 'b']
        dumper = _Dumper(object(), config, outputs)
        self.assertEquals(dumper._outputs, outputs)
    def test_set_clock_type(self, mocked):
        """
        Tests setting of the clock type.

        """
        config = self.create_config()
        config.clock_type = 'blah'
        dumper = _Dumper(object(), config, [])
        dumper.set_clock_type()
        mocked.assert_called_with('blah')
    def test_dump_no_clear(self, mocked_clear, mocked_stop, mocked_start):
        """
        Tests that the profiler is not stopped, cleared, then restarted

        """
        config = self.create_config(clear_each_interval=False)
        dumper = _Dumper(object(), config, [])
        dumper._dump()
        mocked_stop.assert_not_called()
        mocked_clear.assert_not_called()
        mocked_start.assert_not_called()
    def test_dump_with_clear(
        self, mocked_time, mocked_clear,
        mocked_stop, mocked_start
    ):
        """
        Tests that dump stops, clears, then restarts the profiler

        """
        config = self.create_config(clear_each_interval=True)
        dumper = _Dumper(object(), config, [])
        dumper._started = 1
        dumper._dump()
        mocked_stop.assert_called_with()
        mocked_clear.assert_called_with()
        mocked_start.assert_called_with()
        self.assertEquals(mocked_time.call_count, 3)
    def test_dump_outputs(self, *mocked):
        """
        Tests that the write method of each output is called
        during a dump.

        """
        class FakeOutput(object):
            def write(self, context, stats):
                pass

        config = self.create_config()
        outputs = [FakeOutput(), FakeOutput()]
        for o in outputs:
            o.write = mock.Mock()

        dumper = _Dumper(object(), config, outputs)
        dumper._dump()
        for o in outputs:
            self.assertEquals(o.write.call_count, 1)
    def test_outputs_exception(self, *mocked):
        """
        Execution should continue even if output encounters
        exception during write.

        """
        config = self.create_config()
        class BadOutput(object):
            def write(self, ctx, stats):
                raise Exception("Stuff happened yo")

        class GoodOutput(object):
            def write(Self, ctx, stats):
                pass

        outputs = [BadOutput(), GoodOutput()]
        outputs[1].write = mock.Mock()

        dumper = _Dumper(object(), config, outputs)
        dumper._dump()
        self.assertEquals(outputs[1].write.call_count, 1)