|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import functools |
| 15 | +import time |
16 | 16 | import unittest |
17 | 17 |
|
18 | 18 | from google.cloud import error_reporting |
|
22 | 22 | error_stats_service_pb2) |
23 | 23 | from google.protobuf.duration_pb2 import Duration |
24 | 24 |
|
25 | | -from test_utils.retry import RetryResult |
26 | 25 |
|
| 26 | +def setUpModule(): |
| 27 | + Config.CLIENT = error_reporting.Client() |
27 | 28 |
|
28 | | -class _ErrorStatsGaxApi(object): |
29 | | - """Helper mapping Error Reporting-related APIs |
30 | 29 |
|
31 | | - This class provides a small wrapper around making calls to the GAX |
| 30 | +class Config(object): |
| 31 | + """Run-time configuration to be modified at set-up. |
| 32 | +
|
| 33 | + This is a mutable stand-in to allow test set-up to modify |
| 34 | + global state. |
| 35 | + """ |
| 36 | + CLIENT = None |
| 37 | + |
| 38 | + |
| 39 | +def _list_groups(project): |
| 40 | + """List Error Groups from the last 60 seconds. |
| 41 | +
|
| 42 | + This class provides a wrapper around making calls to the GAX |
32 | 43 | API. It's used by the system tests to find the appropriate error group |
33 | 44 | to verify the error was successfully reported. |
34 | 45 |
|
35 | 46 | :type project: str |
36 | 47 | :param project: Google Cloud Project ID |
37 | 48 | """ |
38 | | - def __init__(self, project): |
39 | | - self._project = project |
40 | | - self._gax_api = error_stats_service_client.ErrorStatsServiceClient() |
41 | | - |
42 | | - def list_groups(self): |
43 | | - """Helper to list the groups that have had errors in the last hour.""" |
44 | | - project_name = self._gax_api.project_path(self._project) |
45 | | - time_range = error_stats_service_pb2.QueryTimeRange() |
46 | | - time_range.period = ( |
47 | | - error_stats_service_pb2.QueryTimeRange.PERIOD_1_HOUR |
48 | | - ) |
| 49 | + gax_api = error_stats_service_client.ErrorStatsServiceClient() |
| 50 | + project_name = gax_api.project_path(project) |
49 | 51 |
|
50 | | - duration = Duration() |
51 | | - duration.seconds = 60 * 60 |
| 52 | + time_range = error_stats_service_pb2.QueryTimeRange() |
| 53 | + time_range.period = ( |
| 54 | + error_stats_service_pb2.QueryTimeRange.PERIOD_1_HOUR |
| 55 | + ) |
52 | 56 |
|
53 | | - return self._gax_api.list_group_stats( |
54 | | - project_name, time_range, timed_count_duration=duration) |
| 57 | + duration = Duration() |
| 58 | + duration.seconds = 60 * 60 |
55 | 59 |
|
| 60 | + return gax_api.list_group_stats( |
| 61 | + project_name, time_range, timed_count_duration=duration) |
56 | 62 |
|
57 | | -def _is_incremented(initial, new): |
58 | | - """Helper to retry until new error is counted.""" |
59 | | - return new == initial + 1 |
| 63 | +ERROR_NAME = 'Stackdriver Error Reporting System Test' |
60 | 64 |
|
61 | 65 |
|
62 | 66 | class TestErrorReporting(unittest.TestCase): |
63 | 67 |
|
64 | | - def setUp(self): |
65 | | - self._client = error_reporting.Client() |
66 | | - self._error_name = 'Stackdriver Error Reporting System Test' |
67 | | - |
68 | 68 | def _simulate_exception(self): |
69 | 69 | """Simulates an exception to verify it was reported.""" |
70 | 70 | try: |
71 | | - raise RuntimeError(self._error_name) |
| 71 | + raise RuntimeError(ERROR_NAME) |
72 | 72 | except RuntimeError: |
73 | | - self._client.report_exception() |
| 73 | + Config.CLIENT.report_exception() |
74 | 74 |
|
75 | 75 | def _get_error_count(self): |
76 | 76 | """Counts the number of errors in the group of the test exception.""" |
77 | | - error_stats_api = _ErrorStatsGaxApi(self._client.project) |
78 | | - groups = error_stats_api.list_groups() |
| 77 | + groups = _list_groups(Config.CLIENT.project) |
79 | 78 | for group in groups: |
80 | | - if self._error_name in group.representative.message: |
| 79 | + if ERROR_NAME in group.representative.message: |
81 | 80 | return group.count |
82 | 81 |
|
83 | 82 | def test_report_exception(self): |
84 | | - """Verifies the exception reported increases the group count by one.""" |
85 | 83 | # If test has never run, group won't exist until we report first |
86 | 84 | # exception, so first simulate it just to create the group |
87 | 85 | self._simulate_exception() |
| 86 | + time.sleep(2) |
88 | 87 |
|
89 | 88 | initial_count = self._get_error_count() |
90 | 89 | self._simulate_exception() |
91 | 90 |
|
92 | | - is_incremented = functools.partial(_is_incremented, initial_count) |
93 | | - retry_get_count = RetryResult(is_incremented)(self._get_error_count) |
94 | | - new_count = retry_get_count() |
| 91 | + time.sleep(2) |
| 92 | + new_count = self._get_error_count() |
95 | 93 |
|
96 | 94 | self.assertEqual(new_count, initial_count + 1) |
0 commit comments