Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved context unit testing #1749

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
# Run on both success and failure, but only if coverage.xml exists.
if: ${{ hashFiles('coverage.xml') != '' && (steps.tests.outcome == 'success' || steps.tests.outcome == 'failure') }}
if: ${{ (success() || failure()) && ((steps.tests.outcome == 'success' || steps.tests.outcome == 'failure') && hashFiles('coverage.xml') != '') }}
with:
slug: AcademySoftwareFoundation/rez
files: 'coverage.xml'
Expand Down
4 changes: 4 additions & 0 deletions src/rez/resolved_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ def __call__(self, state):
reason = ("fail limit reached: aborted after %d failures"
% state.num_fails)
return SolverCallbackReturn.fail, reason
print(self.time_limit)
if self.time_limit != -1:
secs = time.time() - self.start_time
print(self.start_time)
print(time.time())
print(secs)
if secs > self.time_limit:
return SolverCallbackReturn.abort, "time limit exceeded"
if self.callback:
Expand Down
77 changes: 76 additions & 1 deletion src/rez/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
"""
test resolved contexts
"""
import sys

from rez.tests.util import restore_os_environ, restore_sys_path, TempdirMixin, \
TestBase
from rez.resolved_context import ResolvedContext
from rez.resolved_context import get_lock_request, PatchLock, ResolvedContext
from rez.bundle_context import bundle_context
from rez.bind import hello_world
from rez.solver import SolverCallbackReturn, SolverState
from rez.utils.platform_ import platform_
from rez.utils.filesystem import is_subdirectory
from rez.utils.formatting import PackageRequest
from rez.version import Version
import unittest
import unittest.mock
import subprocess
import platform
import shutil
Expand Down Expand Up @@ -250,6 +256,75 @@ def test_orderer_package_argument(self):
resolved = [x.qualified_package_name for x in r.resolved_packages]
self.assertEqual(resolved, ['python-2.7.0'])

def test_callback_1(self):
def solver_callback(solver_state: SolverState):
if solver_state.num_fails > 999:
solver_callback_return = SolverCallbackReturn.fail
abort_reason = 'Too many fails'
else:
solver_callback_return = SolverCallbackReturn.keep_going
abort_reason = 'No reason'
return solver_callback_return, abort_reason

callback = ResolvedContext.Callback(max_fails=1, time_limit=2, callback=solver_callback, buf=sys.stdout)
solve_state = SolverState(3, 1, None)
callback_result = callback(solve_state)
assert callback_result[0] == SolverCallbackReturn.fail

def test_callback_2(self):
def solver_callback(solver_state: SolverState):
if solver_state.num_fails > 999:
solver_callback_return = SolverCallbackReturn.fail
abort_reason = 'Too many fails'
else:
solver_callback_return = SolverCallbackReturn.keep_going
abort_reason = 'No reason'
return solver_callback_return, abort_reason

callback = ResolvedContext.Callback(max_fails=999, time_limit=0, callback=solver_callback, buf=sys.stdout)
solve_state = SolverState(3, 1, None)
with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback') as mock_callback:
# Mock start time to be Jan 1, 1970.
mock_callback.start_time.return_value = 0
callback_result = callback(solve_state)
assert callback_result[0] == SolverCallbackReturn.abort

def test_get_lock_request_1(self):
pkg = 'foo'
version = Version('1.2.1')
patch_lock = PatchLock.lock_3
expected_lock_request = PackageRequest('~foo-1.2')

lock_request = get_lock_request(pkg, version, patch_lock)
self.assertEqual(lock_request, expected_lock_request)

def test_get_lock_request_2(self):
pkg = 'foo'
version = Version('1.2.1')
patch_lock = PatchLock.lock_3
expected_lock_request = PackageRequest('foo-1.2')

lock_request = get_lock_request(pkg, version, patch_lock, weak=False)
self.assertEqual(lock_request, expected_lock_request)

def test_get_lock_request_3(self):
pkg = 'foo'
version = Version('1.2.1')
patch_lock = PatchLock.lock
expected_lock_request = PackageRequest('foo==1.2.1')

lock_request = get_lock_request(pkg, version, patch_lock, weak=False)
self.assertEqual(lock_request, expected_lock_request)

def test_get_lock_request_4(self):
pkg = 'foo'
version = Version('1.2.1')
patch_lock = PatchLock.no_lock
expected_lock_request = None

lock_request = get_lock_request(pkg, version, patch_lock)
self.assertEqual(lock_request, expected_lock_request)


if __name__ == '__main__':
unittest.main()
Loading