-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add timeout to datasources (#3598)
* Added timeout using signals in the datasource invoke function, when the timeout is hit it raises a TimeoutException. * Added code in the dehydrate to properly dehydrate caught exceptions, and simplified it to be more readable. Signed-off-by: Ryan Blakley <[email protected]> Signed-off-by: Ryan Blakley <[email protected]>
- Loading branch information
1 parent
4b8f720
commit 4d3a4a0
Showing
8 changed files
with
216 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from insights.core import dr | ||
from insights.core.plugins import datasource | ||
from insights.core.spec_factory import DatasourceProvider, RegistryPoint, SpecSet | ||
|
||
|
||
class RegistrySpecs(SpecSet): | ||
simple_spec = RegistryPoint() | ||
first_spec_with_dep = RegistryPoint() | ||
second_spec_with_dep = RegistryPoint() | ||
|
||
|
||
@datasource() | ||
def simple_spec_imp(broker): | ||
return DatasourceProvider("some data", "/path_1") | ||
|
||
|
||
@datasource() | ||
def dependency_ds(broker): | ||
return DatasourceProvider("dependency data", "/path_2") | ||
|
||
|
||
@datasource(dependency_ds) | ||
def first_spec_with_dep_imp(broker): | ||
return DatasourceProvider("some data", "/path_3") | ||
|
||
|
||
@datasource(dependency_ds) | ||
def second_spec_with_dep_imp(broker): | ||
return DatasourceProvider("some data", "/path_4") | ||
|
||
|
||
class DefaultSpecs(RegistrySpecs): | ||
simple_spec = simple_spec_imp | ||
first_spec_with_dep = first_spec_with_dep_imp | ||
second_spec_with_dep = second_spec_with_dep_imp | ||
|
||
|
||
def test_is_registry_point(): | ||
assert dr.is_registry_point(RegistrySpecs.simple_spec) | ||
assert not dr.is_registry_point(DefaultSpecs.simple_spec) | ||
|
||
|
||
def test_get_registry_points_simple(): | ||
specs = dr.get_registry_points(DefaultSpecs.simple_spec) | ||
assert len(specs) == 1 | ||
spec = list(specs)[0] | ||
assert spec == RegistrySpecs.simple_spec | ||
|
||
|
||
def test_get_registry_points_multiple_specs(): | ||
specs = dr.get_registry_points(dependency_ds) | ||
assert len(specs) == 2 | ||
for spec in specs: | ||
assert spec in [RegistrySpecs.first_spec_with_dep, RegistrySpecs.second_spec_with_dep] |
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,80 @@ | ||
import time | ||
|
||
from insights.core.dr import run | ||
from insights.core.plugins import TimeoutException, datasource, make_info, rule | ||
from insights.core.spec_factory import DatasourceProvider, RegistryPoint, SpecSet, foreach_execute | ||
|
||
|
||
class Specs(SpecSet): | ||
spec_ds_timeout_1 = RegistryPoint() | ||
spec_ds_timeout_2 = RegistryPoint() | ||
spec_ds_timeout_default = RegistryPoint() | ||
spec_foreach_ds_timeout_1 = RegistryPoint(multi_output=True) | ||
|
||
|
||
@datasource(timeout=1) | ||
def foreach_ds_timeout_1(broker): | ||
time.sleep(2) | ||
return ['test1', 'test2', 'test3'] | ||
|
||
|
||
@datasource(timeout=1) | ||
def ds_timeout_1(broker): | ||
time.sleep(2) | ||
return DatasourceProvider('foo', "test_ds_timeout_1") | ||
|
||
|
||
@datasource(timeout=3) | ||
def ds_timeout_2(broker): | ||
time.sleep(1) | ||
return DatasourceProvider('foo', "test_ds_timeout_2") | ||
|
||
|
||
@datasource() | ||
def ds_timeout_default(broker): | ||
time.sleep(1) | ||
return DatasourceProvider('foo', "test_ds_timeout_def") | ||
|
||
|
||
class TestSpecs(Specs): | ||
spec_ds_timeout_1 = ds_timeout_1 | ||
spec_ds_timeout_2 = ds_timeout_2 | ||
spec_ds_timeout_default = ds_timeout_default | ||
spec_foreach_ds_timeout_1 = foreach_execute(foreach_ds_timeout_1, "/usr/bin/echo %s") | ||
|
||
|
||
@rule(Specs.spec_ds_timeout_2, Specs.spec_ds_timeout_default) | ||
def timeout_datasource_no_timeout(ds_to_2, ds_to_def): | ||
return make_info('INFO_1') | ||
|
||
|
||
@rule(Specs.spec_ds_timeout_1) | ||
def timeout_datasource_hit(ds_to_1): | ||
return make_info('INFO_2') | ||
|
||
|
||
@rule(Specs.spec_foreach_ds_timeout_1) | ||
def timeout_foreach_datasource_hit(foreach_ds_to_1): | ||
return make_info('INFO_2') | ||
|
||
|
||
def test_timeout_datasource_no_hit(): | ||
broker = run(timeout_datasource_no_timeout) | ||
assert timeout_datasource_no_timeout in broker | ||
assert Specs.spec_ds_timeout_2 not in broker.exceptions | ||
|
||
|
||
def test_timeout_datasource_hit_def(): | ||
broker = run(timeout_datasource_hit) | ||
assert timeout_datasource_hit in broker | ||
assert Specs.spec_ds_timeout_1 in broker.exceptions | ||
exs = broker.exceptions[Specs.spec_ds_timeout_1] | ||
assert [ex for ex in exs if isinstance(ex, TimeoutException) and str(ex) == "Datasource spec insights.tests.datasources.test_datasource_timeout.TestSpecs.spec_ds_timeout_1 timed out after 1 seconds!"] | ||
|
||
|
||
def test_timeout_foreach_datasource_hit_def(): | ||
broker = run(timeout_foreach_datasource_hit) | ||
assert timeout_foreach_datasource_hit in broker | ||
assert Specs.spec_foreach_ds_timeout_1 in broker.exceptions | ||
exs = broker.exceptions[Specs.spec_foreach_ds_timeout_1] | ||
assert [ex for ex in exs if isinstance(ex, TimeoutException) and str(ex) == "Datasource spec insights.tests.datasources.test_datasource_timeout.foreach_ds_timeout_1 timed out after 1 seconds!"] |
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