# Path: pyinstrument/stack_sampler.py
# class StackSamplerSubscriber:
# class StackSampler:
#     class SubscriberNotFound(Exception):
# class AsyncState(NamedTuple):
#     def __init__(
#         self,
#         *,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         bound_to_async_context: bool,
#         async_state: AsyncState | None,
#     ) -> None:
#     def __init__(self) -> None:
#     def subscribe(
#         self,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         use_async_context: bool,
#     ):
#     def unsubscribe(self, target: StackSamplerSubscriberTarget):
#     def _update(self):
#     def _start_sampling(self, interval: float):
#     def _stop_sampling(self):
#     def _sample(self, frame: types.FrameType, event: str, arg: Any):
#     def _timer(self):
# def get_stack_sampler() -> StackSampler:
# def build_call_stack(frame: types.FrameType | None, event: str, arg: Any) -> list[str]:


import asyncio
import contextlib
import functools
import random
from unittest import mock
from trio.testing import MockClock
from pyinstrument import stack_sampler




class FakeClock:
    def __init__(self) -> None:
        self.time = random.random() * 1e6

    def get_time(self):
        return self.time

    def sleep(self, duration):
        self.time += duration


@contextlib.contextmanager
def fake_time(fake_clock=None):
    fake_clock = fake_clock or FakeClock()
    stack_sampler.get_stack_sampler().timer_func = fake_clock.get_time

    try:
        with mock.patch("time.sleep", new=fake_clock.sleep):
            yield fake_clock
    finally:

# Path: pyinstrument/stack_sampler.py
# class StackSamplerSubscriber:
# class StackSampler:
#     class SubscriberNotFound(Exception):
# class AsyncState(NamedTuple):
#     def __init__(
#         self,
#         *,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         bound_to_async_context: bool,
#         async_state: AsyncState | None,
#     ) -> None:
#     def __init__(self) -> None:
#     def subscribe(
#         self,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         use_async_context: bool,
#     ):
#     def unsubscribe(self, target: StackSamplerSubscriberTarget):
#     def _update(self):
#     def _start_sampling(self, interval: float):
#     def _stop_sampling(self):
#     def _sample(self, frame: types.FrameType, event: str, arg: Any):
#     def _timer(self):
# def get_stack_sampler() -> StackSampler:
# def build_call_stack(frame: types.FrameType | None, event: str, arg: Any) -> list[str]:


# Path: test/fake_time_util.py
import asyncio
import contextlib
import functools
import random
from unittest import mock
from trio.testing import MockClock
from pyinstrument import stack_sampler





class FakeClock:
    def __init__(self) -> None:
        self.time = random.random() * 1e6

    def get_time(self):
        return self.time

    def sleep(self, duration):
        self.time += duration


@contextlib.contextmanager
def fake_time(fake_clock=None):
    fake_clock = fake_clock or FakeClock()
    stack_sampler.get_stack_sampler().timer_func = fake_clock.get_time

    try:
        with mock.patch("time.sleep", new=fake_clock.sleep):
            yield fake_clock
    finally:

        stack_sampler.get_stack_sampler().timer_func = None