-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent concurrent stop / start attempts
Add a few more tests too
- Loading branch information
Showing
2 changed files
with
83 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,86 @@ | ||
import asyncio | ||
import pytest | ||
import sys | ||
|
||
import simperviser | ||
|
||
SLEEP_TIME = 0.5 | ||
SLEEP_WAIT_TIME = 0.7 | ||
|
||
def sleep(retcode=0, time=SLEEP_TIME): | ||
""" | ||
Sleep for {time} seconds & return code {retcode} | ||
""" | ||
return [ | ||
sys.executable, | ||
'-c', f'import sys, time; time.sleep({time}); sys.exit({retcode})' | ||
] | ||
|
||
@pytest.mark.asyncio | ||
async def test_start_success(): | ||
""" | ||
Start a process & check its running status | ||
""" | ||
proc = simperviser.SupervisedProcess( | ||
'/bin/bash', '-c', 'sleep 0.5', always_restart=False | ||
*sleep(0), always_restart=False | ||
) | ||
await proc.start() | ||
assert proc.running | ||
await asyncio.sleep(0.51) | ||
await asyncio.sleep(SLEEP_WAIT_TIME) | ||
assert not proc.running | ||
|
||
@pytest.mark.asyncio | ||
async def test_start_always_restarting(): | ||
""" | ||
Start a process & check it restarts even when it succeeds | ||
""" | ||
proc = simperviser.SupervisedProcess( | ||
*sleep(0), always_restart=True | ||
) | ||
await proc.start() | ||
assert proc.running | ||
first_pid = proc.pid | ||
await asyncio.sleep(SLEEP_WAIT_TIME) | ||
# Process should have restarted by now | ||
assert proc.running | ||
# Make sure it is a new process | ||
assert proc.pid != first_pid | ||
|
||
await proc.kill() | ||
assert not proc.running | ||
|
||
@pytest.mark.asyncio | ||
async def test_start_restarting(): | ||
async def test_start_fail_restarting(): | ||
""" | ||
Start a process that fails & make sure it restarts | ||
""" | ||
proc = simperviser.SupervisedProcess( | ||
'/bin/bash', '-c', 'sleep 0.5; exit 1', always_restart=False | ||
*sleep(1), always_restart=True | ||
) | ||
await proc.start() | ||
assert proc.running | ||
first_pid = proc.pid | ||
await asyncio.sleep(0.51) | ||
await asyncio.sleep(SLEEP_WAIT_TIME) | ||
# Process should have restarted by now | ||
assert proc.running | ||
# Make sure it is a new process | ||
assert proc.pid != first_pid | ||
assert proc.pid != first_pid | ||
|
||
await proc.kill() | ||
assert not proc.running | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_start_multiple_start(): | ||
""" | ||
Starting the same process multiple times should be a noop | ||
""" | ||
proc = simperviser.SupervisedProcess( | ||
*sleep(0), always_restart=True | ||
) | ||
await proc.start() | ||
assert proc.running | ||
first_pid = proc.pid | ||
await proc.start() | ||
assert proc.running | ||
assert proc.pid == first_pid |