Skip to content

Commit

Permalink
Merge pull request #7 from caruhome/bug-with-incorrect-time
Browse files Browse the repository at this point in the history
Pass elapsed time to hooks instead of time of first call
  • Loading branch information
philippbachmann08 authored Jan 31, 2020
2 parents 8afff01 + 69aa017 commit a5beb33
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
include_package_data=True,
install_requires=["paho-mqtt>=1.4.0", "backoff>=1.8.1", "pysm>=0.3.7"],
extras_require={
"dev": ["pytest", "pytest-mock", "boto3", "ipdb", "freezegun", "coverage"],
"dev": ["pytest", "pytest-mock", "boto3", "ipdb", "coverage"],
"sentry": ["sentry-sdk"],
},
entry_points={"console_scripts": ["upparat=upparat.cli:main"]},
Expand Down
8 changes: 5 additions & 3 deletions src/upparat/hooks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import subprocess
import threading
import time
from queue import Queue
from timeit import default_timer

import pysm

Expand Down Expand Up @@ -34,12 +34,14 @@ def _hook(hook, stop_event, inbox: Queue, args: list):
max_retries = settings.hooks.max_retries
retry_interval = settings.hooks.retry_interval

first_call = int(time.time())
first_call_timer = default_timer()

while retry < max_retries and not stop_event.is_set():
time_elapsed = int(default_timer() - first_call_timer)

# universal_newlines=True and bufsize 1 means line buffered
with subprocess.Popen(
[hook, str(first_call), str(retry)] + args,
[hook, str(time_elapsed), str(retry)] + args,
stdout=subprocess.PIPE,
universal_newlines=True,
bufsize=1,
Expand Down
18 changes: 6 additions & 12 deletions tests/hooks/hooks_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import subprocess
from pathlib import Path

import freezegun

from upparat.config import settings
from upparat.events import HOOK
from upparat.events import HOOK_MESSAGE
Expand All @@ -17,12 +15,6 @@
COMMAND_FILE = (Path(__file__).parent / "test.sh").as_posix()


def _freeze_time_threading(*args, **kwargs):
f = freezegun.freeze_time(*args, **kwargs)
f.ignore = tuple(set(f.ignore) - {"threading"})
return f


def _subprocess_mock(mocker, exit_codes, stdout: list):
mock = mocker.patch("upparat.hooks.subprocess.Popen")

Expand Down Expand Up @@ -55,15 +47,17 @@ def test_stop_event(mocker):

def test_subprocess_args(mocker):
mock = _subprocess_mock(mocker, [0], [])
default_timer = mocker.Mock(side_effect=[0, 10])
mocker.patch("upparat.hooks.default_timer", default_timer)

elapsed = 10 # 10 - 0
command = "noop"
start_time = 1520294400 # "2018-03-06"
retry_count = 0

with _freeze_time_threading("2018-03-06"):
run_hook(command, mocker.MagicMock(), args=None, join=True)
run_hook(command, mocker.MagicMock(), args=None, join=True)

mock.assert_called_once_with(
[command, str(start_time), str(retry_count)],
[command, str(elapsed), str(retry_count)],
bufsize=1,
stdout=subprocess.PIPE,
universal_newlines=True,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/upparat/hooks/download.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Check if download is allowed right now.
#
# $1: timestamp from first call
# $1: time elapsed since the first call
# $2: retry count
# $3: meta from job document
# $4: file location
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/upparat/hooks/install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Installs the provided file
#
# $1: timestamp from first call
# $1: time elapsed since the first call
# $2: retry count
# $3: meta from job document
# $4: file location
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/upparat/hooks/restart.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Installs the provided file
#
# $1: timestamp from first call
# $1: time elapsed since the first call
# $2: retry count
# $3: meta from job document

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/upparat/hooks/version.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Gets the system version
#
# $1: timestamp from first call
# $1: time elapsed since the first call
# $2: retry count
# $3: meta from job document

Expand Down

0 comments on commit a5beb33

Please sign in to comment.