-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from AikidoSec/bugfix-multiple-reqs-on-start
Bugfix: remove multiple reqs on start
- Loading branch information
Showing
7 changed files
with
152 additions
and
58 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
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Exports create_interval""" | ||
|
||
|
||
def create_interval(event_scheduler, interval_in_secs, function, args): | ||
""" | ||
This function creates an interval which first runs "function" | ||
after waiting "interval_in_secs" seconds and after that keeps | ||
executing the function every "interval_in_secs" seconds. | ||
""" | ||
# Sleep interval_in_secs seconds before starting the loop : | ||
event_scheduler.enter( | ||
interval_in_secs, | ||
1, | ||
interval_loop, | ||
(event_scheduler, interval_in_secs, function, args), | ||
) | ||
|
||
|
||
def interval_loop(event_scheduler, interval_in_secs, function, args): | ||
""" | ||
This is the actual interval loop which executes and schedules the function | ||
""" | ||
# Execute function : | ||
function(*args) | ||
# Schedule the execution of the function in interval_in_secs seconds : | ||
event_scheduler.enter( | ||
interval_in_secs, | ||
1, | ||
interval_loop, | ||
(event_scheduler, interval_in_secs, function, args), | ||
) |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import pytest | ||
from .create_interval import create_interval, interval_loop | ||
from unittest.mock import Mock, call | ||
|
||
|
||
def test_create_interval_schedules_function(): | ||
# Arrange | ||
event_scheduler = Mock() | ||
function = Mock() | ||
args = (1, 2) | ||
interval_in_secs = 5 | ||
|
||
# Act | ||
create_interval(event_scheduler, interval_in_secs, function, args) | ||
|
||
# Assert | ||
event_scheduler.enter.assert_called_once_with( | ||
interval_in_secs, | ||
1, | ||
interval_loop, | ||
(event_scheduler, interval_in_secs, function, args), | ||
) | ||
|
||
|
||
def test_interval_loop_executes_function_and_reschedules(): | ||
# Arrange | ||
event_scheduler = Mock() | ||
function = Mock() | ||
args = (1, 2) | ||
interval_in_secs = 5 | ||
|
||
# Act | ||
interval_loop(event_scheduler, interval_in_secs, function, args) | ||
|
||
# Assert | ||
function.assert_called_once_with(*args) | ||
event_scheduler.enter.assert_called_once_with( | ||
interval_in_secs, | ||
1, | ||
interval_loop, | ||
(event_scheduler, interval_in_secs, function, args), | ||
) | ||
|
||
|
||
def test_multiple_calls_to_create_interval(): | ||
# Arrange | ||
event_scheduler = Mock() | ||
function = Mock() | ||
args = (1, 2) | ||
interval_in_secs = 5 | ||
|
||
# Act | ||
create_interval(event_scheduler, interval_in_secs, function, args) | ||
create_interval(event_scheduler, interval_in_secs, function, args) | ||
|
||
# Assert | ||
assert event_scheduler.enter.call_count == 2 | ||
assert event_scheduler.enter.call_args_list == [ | ||
call( | ||
interval_in_secs, | ||
1, | ||
interval_loop, | ||
(event_scheduler, interval_in_secs, function, args), | ||
), | ||
call( | ||
interval_in_secs, | ||
1, | ||
interval_loop, | ||
(event_scheduler, interval_in_secs, function, args), | ||
), | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |