-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable parametrization of Service instances within a single `Golem.ru…
…n_service()` call (#514) * enable parametrization of Service instances within a single `Golem.run_service()` call * add use of service parametrization in `simple-service-poc` * tests for `spawn_instances` * don't do anything if `num_instances` is an int < 1 (issue a warning though)
- Loading branch information
1 parent
2d1ef75
commit 740a02b
Showing
4 changed files
with
167 additions
and
32 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import itertools | ||
import sys | ||
import pytest | ||
from unittest.mock import Mock, patch, call | ||
from yapapi.services import Cluster, Service, ServiceError | ||
|
||
|
||
class _TestService(Service): | ||
pass | ||
|
||
|
||
def _get_cluster(): | ||
return Cluster(engine=Mock(), service_class=_TestService, payload=Mock()) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kwargs, calls, error", | ||
[ | ||
( | ||
{"num_instances": 1}, | ||
[call({})], | ||
None, | ||
), | ||
( | ||
{"num_instances": 3}, | ||
[call({}) for _ in range(3)], | ||
None, | ||
), | ||
( | ||
{"instance_params": [{}]}, | ||
[call({})], | ||
None, | ||
), | ||
( | ||
{"instance_params": [{"n": 1}, {"n": 2}]}, | ||
[call({"n": 1}), call({"n": 2})], | ||
None, | ||
), | ||
( | ||
# num_instances takes precedence | ||
{"num_instances": 2, "instance_params": [{} for _ in range(3)]}, | ||
[call({}), call({})], | ||
None, | ||
), | ||
( | ||
# num_instances takes precedence | ||
{"num_instances": 3, "instance_params": ({"n": i} for i in itertools.count(1))}, | ||
[call({"n": 1}), call({"n": 2}), call({"n": 3})], | ||
None, | ||
), | ||
( | ||
# num_instances takes precedence | ||
{"num_instances": 4, "instance_params": [{} for _ in range(3)]}, | ||
[call({}) for _ in range(3)], | ||
"`instance_params` iterable depleted after 3 spawned instances.", | ||
), | ||
( | ||
{"num_instances": 0}, | ||
[], | ||
None, | ||
), | ||
], | ||
) | ||
@pytest.mark.asyncio | ||
@pytest.mark.skipif(sys.version_info < (3, 8), reason="AsyncMock requires python 3.8+") | ||
async def test_spawn_instances(kwargs, calls, error): | ||
with patch("yapapi.services.Cluster.spawn_instance") as spawn_instance: | ||
cluster = _get_cluster() | ||
try: | ||
cluster.spawn_instances(**kwargs) | ||
except ServiceError as e: | ||
if error is not None: | ||
assert str(e) == error | ||
else: | ||
assert False, e | ||
else: | ||
assert error is None, f"Expected ServiceError: {error}" | ||
|
||
assert spawn_instance.mock_calls == calls |
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