diff --git a/codeguru_profiler_agent/model/profile.py b/codeguru_profiler_agent/model/profile.py index 7327e74..ba731c9 100644 --- a/codeguru_profiler_agent/model/profile.py +++ b/codeguru_profiler_agent/model/profile.py @@ -43,9 +43,9 @@ def end(self): @end.setter def end(self, value): self._validate_positive_number(value) - if value < self.start: + if value <= self.start: raise ValueError( - "Profile end value must be greater than or equal to {}, got {}".format(self.start, value)) + "Profile end value must be greater than {}, got {}".format(self.start, value)) self._end = value # this is the total cpu time spent in this application since start, not just the overhead self.cpu_time_seconds = time.process_time() - self._start_process_time diff --git a/codeguru_profiler_agent/profiler_runner.py b/codeguru_profiler_agent/profiler_runner.py index c8a83ca..d2c433f 100644 --- a/codeguru_profiler_agent/profiler_runner.py +++ b/codeguru_profiler_agent/profiler_runner.py @@ -88,6 +88,7 @@ def _run_profiler(self): if self.is_profiling_in_progress: if self.collector.flush(): self.is_profiling_in_progress = False + return True sample = self.sampler.sample() self.collector.add(sample) return True diff --git a/test/acceptance/test_live_profiling.py b/test/acceptance/test_live_profiling.py index 6f4d474..3450815 100644 --- a/test/acceptance/test_live_profiling.py +++ b/test/acceptance/test_live_profiling.py @@ -66,11 +66,17 @@ def test_live_profiling(self): start_status = profiler.start() assert start_status assert profiler.is_running() - time.sleep(3) + time.sleep(4) finally: profiler.stop() - assert wrapped_add.call_count >= 3 + # We should see at least 2 samples in 4 seconds as the sequence should happen in the order of + # initial delay (1 second) + # After 1 second, no flush -> sample + # After 2 seconds, it attempt to flush (possibly succeed) -> sample/ no sample + # After 3 seconds, it attempt to flush (must succeed if it did not flush before) -> no sample/ sample + # After 4 seconds, no flush -> sample (if profiler has not stopped yet) + assert wrapped_add.call_count >= 2 assert wrapped_post_agent_profile.call_count >= 1 assert wrapped_configure_agent.call_count >= 1 assert AgentConfiguration.get().sampling_interval == timedelta(seconds=1) diff --git a/test/unit/test_profiler_runner.py b/test/unit/test_profiler_runner.py index 9ba9230..a40127f 100644 --- a/test/unit/test_profiler_runner.py +++ b/test/unit/test_profiler_runner.py @@ -73,6 +73,13 @@ def test_when_it_is_time_to_report_it_refreshes_configuration_again(self): self.profiler_runner._profiling_command() self.mock_collector.refresh_configuration.assert_called_once() + def test_when_it_reports_it_does_not_sample(self): + self.is_time_to_report = True + + assert self.profiler_runner._profiling_command() + self.mock_collector.flush.assert_called_once() + self.mock_collector.add.assert_not_called() + def test_when_disabler_say_to_stop(self): self.mock_disabler.should_stop_profiling.return_value = True self.profiler_runner._profiling_command()