File: test_threading_actor.py

package info (click to toggle)
python-pykka 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: python: 2,817; makefile: 113
file content (67 lines) | stat: -rw-r--r-- 1,604 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import annotations

import threading
from typing import TYPE_CHECKING

import pytest

from pykka import ThreadingActor

if TYPE_CHECKING:
    from collections.abc import Iterator

    from pykka import ActorRef


class RegularActor(ThreadingActor):
    pass


class DaemonActor(ThreadingActor):
    use_daemon_thread = True


@pytest.fixture
def regular_actor_ref() -> Iterator[ActorRef[RegularActor]]:
    ref = RegularActor.start()
    yield ref
    ref.stop()


@pytest.fixture
def daemon_actor_ref() -> Iterator[ActorRef[DaemonActor]]:
    ref = DaemonActor.start()
    yield ref
    ref.stop()


def test_actor_thread_is_named_after_pykka_actor_class(
    regular_actor_ref: ActorRef[RegularActor],
) -> None:
    alive_threads = threading.enumerate()
    alive_thread_names = [t.name for t in alive_threads]
    named_correctly = [
        name.startswith(RegularActor.__name__) for name in alive_thread_names
    ]

    assert any(named_correctly)


def test_actor_thread_is_not_daemonic_by_default(
    regular_actor_ref: ActorRef[RegularActor],
) -> None:
    alive_threads = threading.enumerate()
    actor_threads = [t for t in alive_threads if t.name.startswith("RegularActor")]

    assert len(actor_threads) == 1
    assert not actor_threads[0].daemon


def test_actor_thread_is_daemonic_if_use_daemon_thread_flag_is_set(
    daemon_actor_ref: ActorRef[DaemonActor],
) -> None:
    alive_threads = threading.enumerate()
    actor_threads = [t for t in alive_threads if t.name.startswith("DaemonActor")]

    assert len(actor_threads) == 1
    assert actor_threads[0].daemon