Skip to content

Commit

Permalink
handling memory error
Browse files Browse the repository at this point in the history
* removed the use of `test.expect_no_error` inside the timeout utility:
  - done "manually", allowing to handle data passed to the thread
  - consequence: DOCS need an update because there isn't any extra assertion anymore, now.

* tested on cw with 3.6, 3.8 wihtout imports, 3.8 with imports: work as expected.

* removed a comment in the framework about the timeout (deprecated info)

* added the default message for timeout ssertion (codewars#4)

Still need to upddate the tests output (and maybe the tests files)
  • Loading branch information
Blind4Basics committed Jun 29, 2021
1 parent 072ae38 commit 77ef949
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
51 changes: 27 additions & 24 deletions codewars_test/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,7 @@ def assert_approx_equals(
expect(abs((actual - expected) / div) < margin, message, allow_raise)


'''
Usage:
@describe('describe text')
def describe1():
@it('it text')
def it1():
# some test cases...
'''



def _timed_block_factory(opening_text):
Expand Down Expand Up @@ -126,28 +119,38 @@ def wrapper(func):
it = _timed_block_factory('IT')


'''
Timeout utility
Usage:
@timeout(sec)
def some_tests():
any code block...
Note: Timeout value can be a float.
'''


def timeout(sec):
def timeout(sec, user_msg=""):

def wrapper(func):
from multiprocessing import Process
from multiprocessing import Process, Value
msg = 'Should not throw any exceptions inside timeout'

def wrapped():
expect_no_error(msg, func)
process = Process(target=wrapped)
def wrapped(finished):
try:
func()
finished.value = 1.0
except BaseException as e:
finished.value = 1.0
fail("{}: {}".format(msg or "Unexpected exception", repr(e)))

finished = Value('d',0.0)
# needed to know if the process crashed without any "feedback" and before any
# assertion has been done in the wrapped function or the wrapper (happens if
# the heap memory explodes)

process = Process(target=wrapped, args=(finished,))
process.start()
process.join(sec)

if process.is_alive():
fail('Exceeded time limit of {:.3f} seconds'.format(sec))
msg = 'Exceeded time limit of {:.3f} seconds'.format(sec)
if user_msg:
msg += ': ' + user_msg
fail(msg)
process.terminate()
process.join()
return wrapper
elif not finished.value:
fail('Something went wrong: the process running the function crashed without feedback (probably saturating the available memory)')

return wrapper
2 changes: 0 additions & 2 deletions tests/fixtures/timeout_passing.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@

<PASSED::>Test Passed

<PASSED::>Test Passed

<COMPLETEDIN::>17.19

0 comments on commit 77ef949

Please sign in to comment.