# Path: test/util.py
# def busy_wait(duration):
#     end_time = time.time() + duration
# 
#     while time.time() < end_time:
#         do_nothing()
# 
# def do_nothing():
#     pass


import threading
import time
import pytest
from typing import Any, List
from unittest import TestCase
from pyinstrument.low_level.stat_profile import setstatprofile
from ..util import busy_wait, do_nothing






class CallCounter:
    def __init__(self, thread) -> None:
        self.thread = thread
        self.count = 0

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        assert self.thread is threading.current_thread()
        self.count += 1


def test_threaded():
    # assert that each thread gets its own callbacks, and check that it
    # doesn't crash!

    counters: list[CallCounter | None] = [None for _ in range(10)]
    stop = False

    def profile_a_busy_wait(i):
        thread = threads[i]
        counter = CallCounter(thread)
        counters[i] = counter

        setstatprofile(counter, 0.001)
        while not stop:

# Path: test/util.py
# def busy_wait(duration):
#     end_time = time.time() + duration
# 
#     while time.time() < end_time:
#         do_nothing()
# 
# def do_nothing():
#     pass


# Path: test/low_level/test_threaded.py
import threading
import time
import pytest
from typing import Any, List
from unittest import TestCase
from pyinstrument.low_level.stat_profile import setstatprofile
from ..util import busy_wait, do_nothing







class CallCounter:
    def __init__(self, thread) -> None:
        self.thread = thread
        self.count = 0

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        assert self.thread is threading.current_thread()
        self.count += 1


def test_threaded():
    # assert that each thread gets its own callbacks, and check that it
    # doesn't crash!

    counters: list[CallCounter | None] = [None for _ in range(10)]
    stop = False

    def profile_a_busy_wait(i):
        thread = threads[i]
        counter = CallCounter(thread)
        counters[i] = counter

        setstatprofile(counter, 0.001)
        while not stop:

            do_nothing()