Skip to content

Commit

Permalink
nested tokens optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
pomponchik committed Aug 6, 2024
1 parent 728fcfe commit 73a52e2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cantok/tokens/abstract/abstract_token.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sys import getrefcount
from abc import ABC, abstractmethod
from threading import RLock
from typing import List, Dict, Awaitable, Optional, Union, Any
from typing import List, Dict, Tuple, Awaitable, Optional, Union, Any


from cantok.errors import CancellationError
Expand All @@ -15,11 +15,22 @@ class AbstractToken(ABC):
rollback_if_nondirect_polling = False

def __init__(self, *tokens: 'AbstractToken', cancelled: bool = False) -> None:
from cantok import DefaultToken
from cantok import DefaultToken, SimpleToken

self.cached_report: Optional[CancellationReport] = None
self.tokens: List[AbstractToken] = [token for token in tokens if not isinstance(token, DefaultToken)]
self._cancelled: bool = cancelled
self.tokens: List[AbstractToken] = []

for token in tokens:
if isinstance(token, DefaultToken):
pass
#elif token._cancelled:
# self.cancel()
# self.tokens = []
# break
else:
self.tokens.append(token)

self.lock: RLock = RLock()

def __repr__(self) -> str:
Expand Down Expand Up @@ -58,9 +69,12 @@ def __add__(self, item: 'AbstractToken') -> 'AbstractToken':

nested_tokens = []
container_token: Optional[AbstractToken] = None
cancel_result: bool = False

for token in self, item:
if isinstance(token, SimpleToken) and getrefcount(token) < 6:
if token._cancelled:
return SimpleToken(cancelled=True)
elif isinstance(token, SimpleToken) and getrefcount(token) < 6:
nested_tokens.extend(token.tokens)
elif isinstance(token, DefaultToken):
pass
Expand Down
37 changes: 37 additions & 0 deletions tests/units/tokens/abstract/test_abstract_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,40 @@ def test_superpower_is_more_important_than_cache(first_token_fabric, second_toke
assert isinstance(report, CancellationReport)
assert report.from_token is token
assert report.cause == CancelCause.CANCELLED


@pytest.mark.parametrize(
'token_fabric',
ALL_TOKENS_FABRICS,
)
def test_just_neste_temp_simple_token_to_another_token(token_fabric):
token = token_fabric(SimpleToken())

assert len(token.tokens) == 1
assert isinstance(token.tokens[0], SimpleToken)
assert token


@pytest.mark.parametrize(
'token_fabric',
ALL_TOKENS_FABRICS,
)
def test_any_token_plus_temp_cancelled_simple_token_gives_cancelled_simple_token(token_fabric):
token = token_fabric() + SimpleToken(cancelled=True)

assert isinstance(token, SimpleToken)
assert len(token.tokens) == 0
assert not token


@pytest.mark.parametrize(
'token_fabric',
ALL_TOKENS_FABRICS,
)
def test_any_token_plus_temp_cancelled_simple_token_gives_cancelled_simple_token(token_fabric):
simple_token = SimpleToken(cancelled=True)
token = token_fabric() + simple_token

assert isinstance(token, SimpleToken)
assert len(token.tokens) == 0
assert not token
5 changes: 5 additions & 0 deletions tests/units/tokens/test_condition_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,8 @@ def test_condition_function_is_more_important_than_cache():
assert isinstance(report, CancellationReport)
assert report.from_token is token
assert report.cause == CancelCause.SUPERPOWER


def test_zero_condition_token_report_is_about_superpower():
for report in ConditionToken(lambda: True).get_report(True), ConditionToken(lambda: True).get_report(False):
assert report.cause == CancelCause.SUPERPOWER
5 changes: 5 additions & 0 deletions tests/units/tokens/test_counter_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,8 @@ def test_not_quasitemp_counter_token_plus_not_temp_simple_token_reverse():
assert isinstance(token.tokens[1], CounterToken)
assert token.tokens[1] is counter_token
assert token.tokens[0] is simple_token


def test_zero_counter_token_report_is_about_superpower():
for report in CounterToken(0).get_report(True), CounterToken(0).get_report(False):
assert report.cause == CancelCause.SUPERPOWER
8 changes: 8 additions & 0 deletions tests/units/tokens/test_simple_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,11 @@ def test_sum_of_not_temp_condition_token_and_not_temp_timeout_token_throw_temp_s
assert token.tokens[1] is timeout_token
assert isinstance(token.tokens[1], TimeoutToken)
assert token.tokens[1].timeout == 1


def test_temp_timeout_token_plus_temp_cancelled_simple_token():
token = TimeoutToken(1) + SimpleToken(cancelled=True)

assert isinstance(token, SimpleToken)
assert not token
assert len(token.tokens) == 0
5 changes: 5 additions & 0 deletions tests/units/tokens/test_timeout_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,8 @@ def test_timeout_is_more_important_than_cache():
assert isinstance(report, CancellationReport)
assert report.from_token is token
assert report.cause == CancelCause.SUPERPOWER


def test_zero_timeout_token_report_is_about_superpower():
for report in TimeoutToken(0).get_report(True), TimeoutToken(0).get_report(False):
assert report.cause == CancelCause.SUPERPOWER

0 comments on commit 73a52e2

Please sign in to comment.