Skip to content

Commit

Permalink
chore: black .
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and BobTheBuidler committed Nov 22, 2024
1 parent 939605e commit c47ce31
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions tests/test_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,38 @@

@pytest.mark.asyncio_cooperative
async def test_counter_lock():
counter = CounterLock(name='test')
counter = CounterLock(name="test")
assert await counter.wait_for(0)
assert counter._name == "test"


@pytest.mark.asyncio_cooperative
async def test_counterlock_initialization():
counter = CounterLock(start_value=5)
assert counter.value == 5


@pytest.mark.asyncio_cooperative
async def test_counterlock_set():
counter = CounterLock(start_value=0)
counter.set(10)
assert counter.value == 10


@pytest.mark.asyncio_cooperative
async def test_counterlock_wait_for():
counter = CounterLock(start_value=0)

async def waiter():
await counter.wait_for(5)
return 'done'
return "done"

waiter_task = asyncio.create_task(waiter())
await asyncio.sleep(0.1)
counter.set(5)
result = await waiter_task
assert result == 'done'
assert result == "done"


@pytest.mark.asyncio_cooperative
async def test_counterlock_concurrent_waiters():
Expand All @@ -49,24 +53,28 @@ async def waiter(index):
await asyncio.gather(*tasks)
assert results == [0, 1, 2]


@pytest.mark.asyncio_cooperative
async def test_counterlock_increment_only():
counter = CounterLock(start_value=5)
with pytest.raises(ValueError):
counter.set(3)


@pytest.mark.asyncio_cooperative
async def test_counterlock_large_value():
counter = CounterLock(start_value=0)
large_value = 10**6
counter.set(large_value)
assert counter.value == large_value


@pytest.mark.asyncio_cooperative
async def test_counterlock_zero_value():
counter = CounterLock(start_value=0)
assert counter.value == 0


@pytest.mark.asyncio_cooperative
async def test_counterlock_exception_handling():
counter = CounterLock(start_value=0)
Expand All @@ -82,6 +90,7 @@ async def waiter():
result = await waiter()
assert result == "Intentional error"


@pytest.mark.asyncio_cooperative
async def test_simultaneous_set_and_wait():
counter = CounterLock(start_value=0)
Expand All @@ -96,17 +105,20 @@ async def waiter(index):
await asyncio.gather(*tasks)
assert results == [0, 1, 2, 3, 4]


@pytest.mark.asyncio_cooperative
async def test_reentrant_set():
counter = CounterLock(start_value=0)
counter.set(5)
counter.set(10) # Reentrant set
assert counter.value == 10


def test_counterlock_negative_start_value():
with pytest.raises(ValueError):
CounterLock(start_value=-1)


@pytest.mark.asyncio_cooperative
async def test_immediate_set_and_wait():
counter = CounterLock(start_value=5)
Expand All @@ -117,19 +129,21 @@ async def waiter():
result = await waiter()
assert result is True


@pytest.mark.asyncio_cooperative
async def test_delayed_set():
counter = CounterLock(start_value=0)

async def waiter():
await counter.wait_for(5)
return 'done'
return "done"

waiter_task = asyncio.create_task(waiter())
await asyncio.sleep(0.5) # Delay before setting the counter
counter.set(5)
result = await waiter_task
assert result == 'done'
assert result == "done"


@pytest.mark.asyncio_cooperative
async def test_multiple_sets():
Expand All @@ -146,6 +160,7 @@ async def waiter(index):
await asyncio.gather(*tasks)
assert results == [0, 1, 2]


@pytest.mark.asyncio_cooperative
async def test_custom_error_handling():
counter = CounterLock(start_value=0)
Expand All @@ -161,16 +176,17 @@ async def waiter():
result = await waiter()
assert result == "Custom error"


@pytest.mark.asyncio_cooperative
async def test_external_interruptions():
counter = CounterLock(start_value=0)

async def waiter():
await counter.wait_for(5)
return 'done'
return "done"

waiter_task = asyncio.create_task(waiter())
await asyncio.sleep(0.1)
waiter_task.cancel()
await asyncio.sleep(0.1)
assert counter.value == 0
assert counter.value == 0

0 comments on commit c47ce31

Please sign in to comment.