def before(self):
     self.agent_config = AgentConfiguration(
         should_profile=True,
         sampling_interval=timedelta(milliseconds=9100),
         minimum_time_reporting=timedelta(seconds=7),
         reporting_interval=timedelta(minutes=7),
         max_stack_depth=999,
         cpu_limit_percentage=9)
     AgentConfiguration.set(self.agent_config)
    def before(self):
        self.mock_reporter = MagicMock(name="reporter", spec=SdkReporter)
        self.mock_profile = MagicMock(name="profile", spec=Profile)
        self.mock_profile_factory = MagicMock(name="profile_factory",
                                              spec=Profile,
                                              return_value=self.mock_profile)
        self.timer = mock_timer()
        self.time_now = CURRENT_TIME_FOR_TESTING_SECOND
        self.clock = lambda: self.time_now
        self.reporting_interval = DEFAULT_REPORTING_INTERVAL

        self.environment = {
            "profiling_group_name": TEST_PROFILING_GROUP_NAME,
            "sampling_interval": timedelta(seconds=TEST_SAMPLING_INTERVAL),
            "host_weight": TEST_HOST_WEIGHT,
            "profile_factory": self.mock_profile_factory,
            "errors_metadata": ERRORS_METADATA,
            "memory_limit_bytes": DEFAULT_MEMORY_LIMIT_BYTES,
            "clock": self.clock,
            "timer": self.timer,
        }

        self.configuration = {
            "reporter": self.mock_reporter,
            "environment": self.environment,
        }

        AgentConfiguration.set(
            AgentConfiguration(
                should_profile=True,
                sampling_interval=timedelta(seconds=TEST_SAMPLING_INTERVAL),
                minimum_time_reporting=INITIAL_MINIMUM_REPORTING_INTERVAL,
                reporting_interval=self.reporting_interval,
                max_stack_depth=999,
                cpu_limit_percentage=10))

        assert len(self.environment.keys()) == 8
        self.profiler = Profiler(
            profiling_group_name=TEST_PROFILING_GROUP_NAME,
            environment_override=self.environment)
        assert len(self.environment.keys()) == 7

        self.subject = LocalAggregator(**self.configuration)
        self.mock_profile_factory.reset_mock()
        self.timer.reset_mock()

        def move_clock_to(duration_timedelta):
            self.time_now = \
                CURRENT_TIME_FOR_TESTING_SECOND + duration_timedelta.total_seconds()

        self.move_clock_to = move_clock_to
    def test_it_calls_the_get_stacks_method_with_the_custom_max_stack_depth(
            self):
        AgentConfiguration.set(AgentConfiguration._get_new_config(configure_agent_response={
            "agentParameters": {
                "SamplingIntervalInMilliseconds": "2700",
                "MinimumTimeForReportingInMilliseconds": "60000",
                "MaxStackDepth": "10"
            },
            "periodInSeconds": 123
        }))
        sampler = Sampler(environment=self.environment)

        sampler.sample()

        self.mock_get_stacks.assert_called_once_with(
            threads_to_sample=ANY,
            excluded_threads=ANY,
            max_depth=10,
        )
def set_agent_config(sampling_interval_seconds=1,
                     cpu_limit_percentage=DEFAULT_CPU_LIMIT_PERCENTAGE):
    """
    Reporting interval needs to have a minimum value and cpu_limit_percentage is used later in tests;
    the other values can be None as we are not using them here.
    """
    return AgentConfiguration.set(
        AgentConfiguration(
            sampling_interval=timedelta(seconds=sampling_interval_seconds),
            reporting_interval=timedelta(seconds=300),
            minimum_time_reporting=timedelta(seconds=60),
            cpu_limit_percentage=cpu_limit_percentage))
 def before(self):
     AgentConfiguration.set(AgentConfiguration._get_new_config(configure_agent_response={
         "agentParameters": {
             "SamplingIntervalInMilliseconds": "2700",
             "MinimumTimeForReportingInMilliseconds": "60000",
             "MaxStackDepth": "1000"
         },
         "periodInSeconds": 123
     }))
     self.mock_get_stacks = create_autospec(get_stacks)
     self.mock_thread_lister = MagicMock(name="thread_lister")
     self._current_frames_reply = {
         "fake_thread_1": "fake_thread_frames_1",
         "fake_thread_2": "fake_thread_frames_2"
     }
     self.mock_thread_lister._current_frames = \
         MagicMock(name="current_frames_list", return_value=self._current_frames_reply)
     self.environment = {
         "get_stacks": self.mock_get_stacks,
         "thread_lister": self.mock_thread_lister,
     }
 def test_set_throws_error_when_setting_a_none_instance(self):
     with pytest.raises(ValueError):
         AgentConfiguration.set(None)
 def set_new_configuration():
     AgentConfiguration.set(self.agent_configuration)