langfun 0.1.2.dev202509250804__py3-none-any.whl → 0.1.2.dev202509270803__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langfun might be problematic. Click here for more details.
- langfun/core/llms/__init__.py +2 -0
- langfun/core/llms/openai.py +57 -0
- langfun/env/__init__.py +4 -5
- langfun/env/base_environment.py +41 -34
- langfun/env/base_feature.py +10 -1
- langfun/env/base_sandbox.py +27 -26
- langfun/env/base_test.py +106 -33
- langfun/env/event_handlers/__init__.py +12 -0
- langfun/env/event_handlers/base.py +271 -0
- langfun/env/event_handlers/event_logger.py +415 -0
- langfun/env/event_handlers/event_logger_test.py +289 -0
- langfun/env/interface.py +103 -380
- langfun/env/interface_test.py +3 -3
- langfun/env/load_balancers_test.py +3 -15
- langfun/env/test_utils.py +12 -26
- {langfun-0.1.2.dev202509250804.dist-info → langfun-0.1.2.dev202509270803.dist-info}/METADATA +1 -1
- {langfun-0.1.2.dev202509250804.dist-info → langfun-0.1.2.dev202509270803.dist-info}/RECORD +20 -16
- {langfun-0.1.2.dev202509250804.dist-info → langfun-0.1.2.dev202509270803.dist-info}/WHEEL +0 -0
- {langfun-0.1.2.dev202509250804.dist-info → langfun-0.1.2.dev202509270803.dist-info}/licenses/LICENSE +0 -0
- {langfun-0.1.2.dev202509250804.dist-info → langfun-0.1.2.dev202509270803.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
# Copyright 2025 The Langfun Authors
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Environment event logger."""
|
|
15
|
+
|
|
16
|
+
import re
|
|
17
|
+
import time
|
|
18
|
+
from typing import Annotated
|
|
19
|
+
from langfun.env.event_handlers import base
|
|
20
|
+
import pyglove as pg
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class EventLogger(pg.Object, base.EventHandler):
|
|
24
|
+
"""Event handler for logging debugger."""
|
|
25
|
+
|
|
26
|
+
regex: Annotated[
|
|
27
|
+
str | list[str] | None,
|
|
28
|
+
(
|
|
29
|
+
'One or a list of regular expressions to filter event messages. '
|
|
30
|
+
'If None, no filtering will be applied.'
|
|
31
|
+
)
|
|
32
|
+
] = None
|
|
33
|
+
|
|
34
|
+
error_only: Annotated[
|
|
35
|
+
bool,
|
|
36
|
+
(
|
|
37
|
+
'If True, log events with errors only.'
|
|
38
|
+
)
|
|
39
|
+
] = False
|
|
40
|
+
|
|
41
|
+
sandbox_status: Annotated[
|
|
42
|
+
bool,
|
|
43
|
+
(
|
|
44
|
+
'If True, log events for sandbox status changes.'
|
|
45
|
+
)
|
|
46
|
+
] = True
|
|
47
|
+
|
|
48
|
+
feature_status: Annotated[
|
|
49
|
+
bool,
|
|
50
|
+
(
|
|
51
|
+
'If True, log events for feature setup/teardown updates.'
|
|
52
|
+
)
|
|
53
|
+
] = True
|
|
54
|
+
|
|
55
|
+
session_status: Annotated[
|
|
56
|
+
bool,
|
|
57
|
+
(
|
|
58
|
+
'If True, log events for session start/end status update.'
|
|
59
|
+
)
|
|
60
|
+
] = True
|
|
61
|
+
|
|
62
|
+
housekeep_status: Annotated[
|
|
63
|
+
bool,
|
|
64
|
+
(
|
|
65
|
+
'If True, log housekeeping events.'
|
|
66
|
+
)
|
|
67
|
+
] = True
|
|
68
|
+
|
|
69
|
+
stats_report_interval: Annotated[
|
|
70
|
+
float | None,
|
|
71
|
+
(
|
|
72
|
+
'The minimum interval in seconds for reporting the environment '
|
|
73
|
+
'stats. If None, stats will not be reported.'
|
|
74
|
+
)
|
|
75
|
+
] = 300.0
|
|
76
|
+
|
|
77
|
+
def _on_bound(self) -> None:
|
|
78
|
+
super()._on_bound()
|
|
79
|
+
|
|
80
|
+
regex_exps = self.regex
|
|
81
|
+
if isinstance(regex_exps, str):
|
|
82
|
+
regex_exps = [regex_exps]
|
|
83
|
+
elif regex_exps is None:
|
|
84
|
+
regex_exps = []
|
|
85
|
+
self._regex_exps = [re.compile(x) for x in regex_exps]
|
|
86
|
+
self._last_stats_report_time = None
|
|
87
|
+
|
|
88
|
+
def _format_message(
|
|
89
|
+
self,
|
|
90
|
+
message: str,
|
|
91
|
+
error: BaseException | None,
|
|
92
|
+
) -> str:
|
|
93
|
+
if error is not None:
|
|
94
|
+
message = f'{message} with error: {error}'
|
|
95
|
+
return message
|
|
96
|
+
|
|
97
|
+
def _keep(
|
|
98
|
+
self,
|
|
99
|
+
message: str,
|
|
100
|
+
error: BaseException | None,
|
|
101
|
+
) -> bool:
|
|
102
|
+
if error is None and self.error_only:
|
|
103
|
+
return False
|
|
104
|
+
if self._regex_exps and all(
|
|
105
|
+
not exp.match(message) for exp in self._regex_exps
|
|
106
|
+
):
|
|
107
|
+
return False
|
|
108
|
+
return True
|
|
109
|
+
|
|
110
|
+
def on_environment_start(
|
|
111
|
+
self,
|
|
112
|
+
environment: base.Environment,
|
|
113
|
+
duration: float,
|
|
114
|
+
error: BaseException | None
|
|
115
|
+
) -> None:
|
|
116
|
+
"""Called when the environment is started."""
|
|
117
|
+
self._print(
|
|
118
|
+
f'[{environment.id}] environment started '
|
|
119
|
+
f'(duration={duration:.2f} seconds)',
|
|
120
|
+
error=error,
|
|
121
|
+
color='green',
|
|
122
|
+
styles=['bold'],
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
def on_environment_housekeep(
|
|
126
|
+
self,
|
|
127
|
+
environment: base.Environment,
|
|
128
|
+
counter: int,
|
|
129
|
+
duration: float,
|
|
130
|
+
error: BaseException | None
|
|
131
|
+
) -> None:
|
|
132
|
+
"""Called when the environment is housekeeping."""
|
|
133
|
+
if self.housekeep_status:
|
|
134
|
+
self._print(
|
|
135
|
+
f'[{environment.id}] environment housekeeping complete'
|
|
136
|
+
f'(counter={counter}, duration={duration:.2f} seconds)',
|
|
137
|
+
error=error,
|
|
138
|
+
color='green',
|
|
139
|
+
)
|
|
140
|
+
if (self.stats_report_interval is not None and
|
|
141
|
+
(self._last_stats_report_time is None
|
|
142
|
+
or time.time() - self._last_stats_report_time
|
|
143
|
+
> self.stats_report_interval)):
|
|
144
|
+
self._write_log(
|
|
145
|
+
f'[{environment.id}] environment stats: {environment.stats()}',
|
|
146
|
+
color='magenta',
|
|
147
|
+
error=None,
|
|
148
|
+
)
|
|
149
|
+
self._last_stats_report_time = time.time()
|
|
150
|
+
|
|
151
|
+
def on_environment_shutdown(
|
|
152
|
+
self,
|
|
153
|
+
environment: base.Environment,
|
|
154
|
+
lifetime: float,
|
|
155
|
+
error: BaseException | None
|
|
156
|
+
) -> None:
|
|
157
|
+
"""Called when the environment is shutdown."""
|
|
158
|
+
self._print(
|
|
159
|
+
f'[{environment.id}] environment shutdown '
|
|
160
|
+
f'(lifetime={lifetime:.2f} seconds)',
|
|
161
|
+
error=error,
|
|
162
|
+
color='green',
|
|
163
|
+
styles=['bold'],
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def on_sandbox_start(
|
|
167
|
+
self,
|
|
168
|
+
environment: base.Environment,
|
|
169
|
+
sandbox: base.Sandbox,
|
|
170
|
+
duration: float,
|
|
171
|
+
error: BaseException | None
|
|
172
|
+
) -> None:
|
|
173
|
+
if self.sandbox_status:
|
|
174
|
+
self._print(
|
|
175
|
+
f'[{sandbox.id}] sandbox started '
|
|
176
|
+
f'(duration={duration:.2f} seconds)',
|
|
177
|
+
error=error,
|
|
178
|
+
color='white',
|
|
179
|
+
styles=['bold'],
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
def on_sandbox_status_change(
|
|
183
|
+
self,
|
|
184
|
+
environment: base.Environment,
|
|
185
|
+
sandbox: base.Sandbox,
|
|
186
|
+
old_status: base.Sandbox.Status,
|
|
187
|
+
new_status: base.Sandbox.Status,
|
|
188
|
+
span: float
|
|
189
|
+
) -> None:
|
|
190
|
+
if self.sandbox_status:
|
|
191
|
+
self._print(
|
|
192
|
+
f'[{sandbox.id}] {old_status.value} '
|
|
193
|
+
f'({span:.2f} seconds) -> {new_status.value}',
|
|
194
|
+
error=None,
|
|
195
|
+
color='white',
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
def on_sandbox_shutdown(
|
|
199
|
+
self,
|
|
200
|
+
environment: base.Environment,
|
|
201
|
+
sandbox: base.Sandbox,
|
|
202
|
+
lifetime: float,
|
|
203
|
+
error: BaseException | None
|
|
204
|
+
) -> None:
|
|
205
|
+
if self.sandbox_status:
|
|
206
|
+
self._print(
|
|
207
|
+
f'[{sandbox.id}] sandbox shutdown '
|
|
208
|
+
f'(lifetime={lifetime:.2f} seconds)',
|
|
209
|
+
error=error,
|
|
210
|
+
color='white',
|
|
211
|
+
styles=['bold'],
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
def on_sandbox_housekeep(
|
|
215
|
+
self,
|
|
216
|
+
environment: base.Environment,
|
|
217
|
+
sandbox: base.Sandbox,
|
|
218
|
+
counter: int,
|
|
219
|
+
duration: float,
|
|
220
|
+
error: BaseException | None
|
|
221
|
+
) -> None:
|
|
222
|
+
"""Called when a sandbox feature is housekeeping."""
|
|
223
|
+
if self.sandbox_status and self.housekeep_status:
|
|
224
|
+
self._print(
|
|
225
|
+
f'[{sandbox.id}] sandbox housekeeping complete '
|
|
226
|
+
f'(counter={counter}, duration={duration:.2f} seconds)',
|
|
227
|
+
error=error,
|
|
228
|
+
color='white',
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
def on_feature_setup(
|
|
232
|
+
self,
|
|
233
|
+
environment: base.Environment,
|
|
234
|
+
sandbox: base.Sandbox,
|
|
235
|
+
feature: base.Feature,
|
|
236
|
+
duration: float,
|
|
237
|
+
error: BaseException | None
|
|
238
|
+
) -> None:
|
|
239
|
+
"""Called when a sandbox feature is setup."""
|
|
240
|
+
if self.feature_status:
|
|
241
|
+
self._print(
|
|
242
|
+
f'[{sandbox.id}/<idle>/{feature.name}] feature setup complete '
|
|
243
|
+
f'(duration={duration:.2f} seconds)',
|
|
244
|
+
error=error,
|
|
245
|
+
color='white',
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
def on_feature_teardown(
|
|
249
|
+
self,
|
|
250
|
+
environment: base.Environment,
|
|
251
|
+
sandbox: base.Sandbox,
|
|
252
|
+
feature: base.Feature,
|
|
253
|
+
duration: float,
|
|
254
|
+
error: BaseException | None
|
|
255
|
+
) -> None:
|
|
256
|
+
"""Called when a sandbox feature is teardown."""
|
|
257
|
+
if self.feature_status:
|
|
258
|
+
self._print(
|
|
259
|
+
f'[{sandbox.id}/<idle>/{feature.name}] feature teardown complete '
|
|
260
|
+
f'(duration={duration:.2f} seconds)',
|
|
261
|
+
error=error,
|
|
262
|
+
color='white',
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
def on_feature_setup_session(
|
|
266
|
+
self,
|
|
267
|
+
environment: base.Environment,
|
|
268
|
+
sandbox: base.Sandbox,
|
|
269
|
+
feature: base.Feature,
|
|
270
|
+
session_id: str | None,
|
|
271
|
+
duration: float,
|
|
272
|
+
error: BaseException | None
|
|
273
|
+
) -> None:
|
|
274
|
+
"""Called when a sandbox feature is setup."""
|
|
275
|
+
if self.feature_status:
|
|
276
|
+
self._print(
|
|
277
|
+
f'[{sandbox.id}/{session_id or "<idle>"}/{feature.name}] '
|
|
278
|
+
f'feature setup complete (duration={duration:.2f} seconds)',
|
|
279
|
+
error=error,
|
|
280
|
+
color='yellow',
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
def on_feature_teardown_session(
|
|
284
|
+
self,
|
|
285
|
+
environment: base.Environment,
|
|
286
|
+
sandbox: base.Sandbox,
|
|
287
|
+
feature: base.Feature,
|
|
288
|
+
session_id: str,
|
|
289
|
+
duration: float,
|
|
290
|
+
error: BaseException | None
|
|
291
|
+
) -> None:
|
|
292
|
+
"""Called when a sandbox feature is teardown."""
|
|
293
|
+
if self.feature_status:
|
|
294
|
+
self._print(
|
|
295
|
+
f'[{sandbox.id}/{session_id}>/{feature.name}] '
|
|
296
|
+
f'feature teardown complete (duration={duration:.2f} seconds)',
|
|
297
|
+
error=error,
|
|
298
|
+
color='yellow',
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
def on_feature_housekeep(
|
|
302
|
+
self,
|
|
303
|
+
environment: base.Environment,
|
|
304
|
+
sandbox: base.Sandbox,
|
|
305
|
+
feature: base.Feature,
|
|
306
|
+
counter: int,
|
|
307
|
+
duration: float,
|
|
308
|
+
error: BaseException | None
|
|
309
|
+
) -> None:
|
|
310
|
+
"""Called when a sandbox feature is housekeeping."""
|
|
311
|
+
if self.feature_status and self.housekeep_status:
|
|
312
|
+
self._print(
|
|
313
|
+
f'[{sandbox.id}/<idle>/{feature.name}] feature housekeeping complete '
|
|
314
|
+
f'(counter={counter}, (duration={duration:.2f} seconds)',
|
|
315
|
+
error=error,
|
|
316
|
+
color='white',
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
def on_session_start(
|
|
320
|
+
self,
|
|
321
|
+
environment: base.Environment,
|
|
322
|
+
sandbox: base.Sandbox,
|
|
323
|
+
session_id: str,
|
|
324
|
+
duration: float,
|
|
325
|
+
error: BaseException | None
|
|
326
|
+
) -> None:
|
|
327
|
+
"""Called when a sandbox session starts."""
|
|
328
|
+
if self.session_status:
|
|
329
|
+
self._print(
|
|
330
|
+
f'[{sandbox.id}/{session_id}] session started '
|
|
331
|
+
f'(duration={duration:.2f} seconds)',
|
|
332
|
+
error=error,
|
|
333
|
+
color='blue',
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
def on_session_end(
|
|
337
|
+
self,
|
|
338
|
+
environment: base.Environment,
|
|
339
|
+
sandbox: base.Sandbox,
|
|
340
|
+
session_id: str,
|
|
341
|
+
lifetime: float,
|
|
342
|
+
error: BaseException | None
|
|
343
|
+
) -> None:
|
|
344
|
+
"""Called when a sandbox session ends."""
|
|
345
|
+
if self.session_status:
|
|
346
|
+
self._print(
|
|
347
|
+
f'[{sandbox.id}/{session_id}] session ended '
|
|
348
|
+
f'(lifetime={lifetime:.2f} seconds)',
|
|
349
|
+
error=error,
|
|
350
|
+
color='blue',
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
def on_sandbox_activity(
|
|
354
|
+
self,
|
|
355
|
+
name: str,
|
|
356
|
+
environment: base.Environment,
|
|
357
|
+
sandbox: base.Sandbox,
|
|
358
|
+
feature: base.Feature | None,
|
|
359
|
+
session_id: str | None,
|
|
360
|
+
duration: float,
|
|
361
|
+
error: BaseException | None,
|
|
362
|
+
**kwargs
|
|
363
|
+
) -> None:
|
|
364
|
+
"""Called when a sandbox activity is performed."""
|
|
365
|
+
del environment
|
|
366
|
+
log_id = f'{sandbox.id}/{session_id or "<idle>"}'
|
|
367
|
+
if feature is not None:
|
|
368
|
+
log_id = f'{log_id}/{feature.name}'
|
|
369
|
+
|
|
370
|
+
color = 'yellow' if session_id is None else 'cyan'
|
|
371
|
+
self._print(
|
|
372
|
+
f'[{log_id}] call {name!r} '
|
|
373
|
+
f'(duration={duration:.2f} seconds, kwargs={kwargs}) ',
|
|
374
|
+
error,
|
|
375
|
+
color=color
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
def _print(
|
|
379
|
+
self,
|
|
380
|
+
message: str,
|
|
381
|
+
error: BaseException | None,
|
|
382
|
+
color: str | None = None,
|
|
383
|
+
styles: list[str] | None = None,
|
|
384
|
+
):
|
|
385
|
+
message = self._format_message(message, error)
|
|
386
|
+
if not self._keep(message, error):
|
|
387
|
+
return
|
|
388
|
+
self._write_log(message, error, color, styles)
|
|
389
|
+
|
|
390
|
+
def _write_log(
|
|
391
|
+
self,
|
|
392
|
+
message: str,
|
|
393
|
+
error: BaseException | None,
|
|
394
|
+
color: str | None = None,
|
|
395
|
+
styles: list[str] | None = None,
|
|
396
|
+
):
|
|
397
|
+
if error is not None:
|
|
398
|
+
pg.logging.error(pg.colored(message, 'red', styles=styles))
|
|
399
|
+
else:
|
|
400
|
+
pg.logging.info(pg.colored(message, color, styles=styles))
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
class ConsoleEventLogger(EventLogger):
|
|
404
|
+
"""Event handler for console debugger."""
|
|
405
|
+
|
|
406
|
+
def _write_log(
|
|
407
|
+
self,
|
|
408
|
+
message: str,
|
|
409
|
+
error: BaseException | None,
|
|
410
|
+
color: str | None = None,
|
|
411
|
+
styles: list[str] | None = None,
|
|
412
|
+
):
|
|
413
|
+
print(
|
|
414
|
+
pg.colored(message, color if error is None else 'red', styles=styles)
|
|
415
|
+
)
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# Copyright 2025 The Langfun Authors
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import contextlib
|
|
16
|
+
import io
|
|
17
|
+
from typing import Iterator
|
|
18
|
+
import unittest
|
|
19
|
+
|
|
20
|
+
from langfun.env import interface
|
|
21
|
+
from langfun.env import test_utils
|
|
22
|
+
from langfun.env.event_handlers import event_logger as event_logger_lib
|
|
23
|
+
import pyglove as pg
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class EventLoggerTest(unittest.TestCase):
|
|
27
|
+
|
|
28
|
+
LOGGER_CLS = event_logger_lib.EventLogger
|
|
29
|
+
|
|
30
|
+
@contextlib.contextmanager
|
|
31
|
+
def _capture_logs(self, test_name: str) -> Iterator[io.StringIO]:
|
|
32
|
+
try:
|
|
33
|
+
stream = io.StringIO()
|
|
34
|
+
with pg.logging.redirect_stream(
|
|
35
|
+
stream, name=f'{self.LOGGER_CLS.__name__}.{test_name}'
|
|
36
|
+
):
|
|
37
|
+
yield stream
|
|
38
|
+
finally:
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
def _test_logger(
|
|
42
|
+
self,
|
|
43
|
+
*,
|
|
44
|
+
test_name: str,
|
|
45
|
+
expected_substrings: list[str],
|
|
46
|
+
unexpected_substrings: list[str],
|
|
47
|
+
error_only: bool = False,
|
|
48
|
+
regex: str | None = None,
|
|
49
|
+
sandbox_status: bool = True,
|
|
50
|
+
session_status: bool = True,
|
|
51
|
+
feature_status: bool = True,
|
|
52
|
+
housekeep_status: bool = True,
|
|
53
|
+
stats_report_interval: float = 1.0,
|
|
54
|
+
):
|
|
55
|
+
event_logger = self.LOGGER_CLS(
|
|
56
|
+
error_only=error_only,
|
|
57
|
+
regex=regex,
|
|
58
|
+
sandbox_status=sandbox_status,
|
|
59
|
+
session_status=session_status,
|
|
60
|
+
feature_status=feature_status,
|
|
61
|
+
housekeep_status=housekeep_status,
|
|
62
|
+
stats_report_interval=stats_report_interval,
|
|
63
|
+
)
|
|
64
|
+
env = test_utils.TestingEnvironment(
|
|
65
|
+
features={
|
|
66
|
+
'test_feature1': test_utils.TestingFeature(housekeep_interval=0),
|
|
67
|
+
'test_feature2': test_utils.TestingFeature(housekeep_interval=None),
|
|
68
|
+
},
|
|
69
|
+
pool_size=2,
|
|
70
|
+
outage_grace_period=0,
|
|
71
|
+
outage_retry_interval=0,
|
|
72
|
+
housekeep_interval=1.0,
|
|
73
|
+
sandbox_keepalive_interval=1.0,
|
|
74
|
+
event_handlers=[event_logger],
|
|
75
|
+
)
|
|
76
|
+
with self._capture_logs(test_name) as stream:
|
|
77
|
+
with env:
|
|
78
|
+
with env.sandbox('session1') as sb:
|
|
79
|
+
self.assertEqual(sb.test_feature1.num_shell_calls(), 4)
|
|
80
|
+
|
|
81
|
+
with self.assertRaises(interface.SandboxStateError):
|
|
82
|
+
with env.sandbox('session2') as sb:
|
|
83
|
+
sb.shell('echo "bar"', raise_error=RuntimeError)
|
|
84
|
+
|
|
85
|
+
stdout = stream.getvalue()
|
|
86
|
+
for substring in expected_substrings:
|
|
87
|
+
self.assertIn(substring, stdout)
|
|
88
|
+
for substring in unexpected_substrings:
|
|
89
|
+
self.assertNotIn(substring, stdout)
|
|
90
|
+
|
|
91
|
+
def test_all_flags_on(self):
|
|
92
|
+
return self._test_logger(
|
|
93
|
+
test_name='test_all_flags_on',
|
|
94
|
+
expected_substrings=[
|
|
95
|
+
'environment started',
|
|
96
|
+
'environment shutdown',
|
|
97
|
+
'environment housekeeping',
|
|
98
|
+
'environment stats',
|
|
99
|
+
'sandbox started',
|
|
100
|
+
'-> acquired',
|
|
101
|
+
'sandbox shutdown',
|
|
102
|
+
'sandbox housekeeping',
|
|
103
|
+
'feature setup complete',
|
|
104
|
+
'feature teardown complete',
|
|
105
|
+
'/test_feature1] feature housekeeping',
|
|
106
|
+
'session started',
|
|
107
|
+
'session ended',
|
|
108
|
+
'call \'shell\'',
|
|
109
|
+
'RuntimeError',
|
|
110
|
+
],
|
|
111
|
+
unexpected_substrings=[
|
|
112
|
+
'/test_feature2] feature housekeeping',
|
|
113
|
+
],
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def test_error_only(self):
|
|
117
|
+
return self._test_logger(
|
|
118
|
+
test_name='test_error_only',
|
|
119
|
+
expected_substrings=[
|
|
120
|
+
'session ended',
|
|
121
|
+
'call \'shell\'',
|
|
122
|
+
'RuntimeError',
|
|
123
|
+
],
|
|
124
|
+
unexpected_substrings=[
|
|
125
|
+
'environment started',
|
|
126
|
+
'environment shutdown',
|
|
127
|
+
'environment housekeeping',
|
|
128
|
+
'sandbox started',
|
|
129
|
+
'-> acquired',
|
|
130
|
+
'sandbox shutdown',
|
|
131
|
+
'feature setup complete',
|
|
132
|
+
'feature teardown complete',
|
|
133
|
+
'session started',
|
|
134
|
+
],
|
|
135
|
+
error_only=True,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
def test_regex(self):
|
|
139
|
+
return self._test_logger(
|
|
140
|
+
test_name='test_regex',
|
|
141
|
+
expected_substrings=[
|
|
142
|
+
'environment started',
|
|
143
|
+
'environment shutdown',
|
|
144
|
+
'environment housekeeping',
|
|
145
|
+
],
|
|
146
|
+
unexpected_substrings=[
|
|
147
|
+
'sandbox started',
|
|
148
|
+
'-> acquired',
|
|
149
|
+
'sandbox shutdown',
|
|
150
|
+
'feature setup complete',
|
|
151
|
+
'feature teardown complete',
|
|
152
|
+
'session started',
|
|
153
|
+
'session ended',
|
|
154
|
+
'call \'shell\'',
|
|
155
|
+
'RuntimeError',
|
|
156
|
+
],
|
|
157
|
+
regex='.*environment.*',
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
def test_sandbox_status_off(self):
|
|
161
|
+
return self._test_logger(
|
|
162
|
+
test_name='test_sandbox_status_off',
|
|
163
|
+
expected_substrings=[
|
|
164
|
+
'environment started',
|
|
165
|
+
'environment shutdown',
|
|
166
|
+
'environment housekeeping',
|
|
167
|
+
'feature setup complete',
|
|
168
|
+
'feature teardown complete',
|
|
169
|
+
'feature housekeeping',
|
|
170
|
+
'session started',
|
|
171
|
+
'session ended',
|
|
172
|
+
'call \'shell\'',
|
|
173
|
+
'RuntimeError',
|
|
174
|
+
],
|
|
175
|
+
unexpected_substrings=[
|
|
176
|
+
'sandbox started',
|
|
177
|
+
'-> acquired',
|
|
178
|
+
'sandbox shutdown',
|
|
179
|
+
'sandbox housekeeping',
|
|
180
|
+
],
|
|
181
|
+
sandbox_status=False,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def test_feature_status_off(self):
|
|
185
|
+
return self._test_logger(
|
|
186
|
+
test_name='test_feature_status_off',
|
|
187
|
+
expected_substrings=[
|
|
188
|
+
'environment started',
|
|
189
|
+
'environment shutdown',
|
|
190
|
+
'environment housekeeping',
|
|
191
|
+
'environment stats',
|
|
192
|
+
'sandbox started',
|
|
193
|
+
'-> acquired',
|
|
194
|
+
'sandbox shutdown',
|
|
195
|
+
'sandbox housekeeping',
|
|
196
|
+
'session started',
|
|
197
|
+
'session ended',
|
|
198
|
+
'call \'shell\'',
|
|
199
|
+
'RuntimeError',
|
|
200
|
+
],
|
|
201
|
+
unexpected_substrings=[
|
|
202
|
+
'feature setup complete',
|
|
203
|
+
'feature teardown complete',
|
|
204
|
+
'feature housekeeping',
|
|
205
|
+
],
|
|
206
|
+
feature_status=False,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
def test_session_status_off(self):
|
|
210
|
+
return self._test_logger(
|
|
211
|
+
test_name='test_session_status_off',
|
|
212
|
+
expected_substrings=[
|
|
213
|
+
'environment started',
|
|
214
|
+
'environment shutdown',
|
|
215
|
+
'environment stats',
|
|
216
|
+
'environment housekeeping',
|
|
217
|
+
'sandbox started',
|
|
218
|
+
'-> acquired',
|
|
219
|
+
'sandbox shutdown',
|
|
220
|
+
'sandbox housekeeping',
|
|
221
|
+
'feature setup complete',
|
|
222
|
+
'feature teardown complete',
|
|
223
|
+
'feature housekeeping',
|
|
224
|
+
'call \'shell\'',
|
|
225
|
+
'RuntimeError',
|
|
226
|
+
],
|
|
227
|
+
unexpected_substrings=[
|
|
228
|
+
'session started',
|
|
229
|
+
'session ended',
|
|
230
|
+
],
|
|
231
|
+
session_status=False,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
def test_housekeep_status_off(self):
|
|
235
|
+
return self._test_logger(
|
|
236
|
+
test_name='test_housekeep_status_off',
|
|
237
|
+
expected_substrings=[
|
|
238
|
+
'environment started',
|
|
239
|
+
'environment shutdown',
|
|
240
|
+
'environment stats',
|
|
241
|
+
'sandbox started',
|
|
242
|
+
'-> acquired',
|
|
243
|
+
'sandbox shutdown',
|
|
244
|
+
'feature setup complete',
|
|
245
|
+
'feature teardown complete',
|
|
246
|
+
'session started',
|
|
247
|
+
'session ended',
|
|
248
|
+
'call \'shell\'',
|
|
249
|
+
'RuntimeError',
|
|
250
|
+
],
|
|
251
|
+
unexpected_substrings=[
|
|
252
|
+
'environment housekeeping',
|
|
253
|
+
'sandbox housekeeping',
|
|
254
|
+
'feature housekeeping',
|
|
255
|
+
],
|
|
256
|
+
housekeep_status=False,
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
def test_no_stats_report(self):
|
|
260
|
+
return self._test_logger(
|
|
261
|
+
test_name='test_housekeep_status_off',
|
|
262
|
+
expected_substrings=[
|
|
263
|
+
'environment started',
|
|
264
|
+
'environment shutdown',
|
|
265
|
+
'environment housekeeping',
|
|
266
|
+
],
|
|
267
|
+
unexpected_substrings=[
|
|
268
|
+
'environment stats',
|
|
269
|
+
],
|
|
270
|
+
stats_report_interval=None,
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class ConsoleEventLoggerTest(EventLoggerTest):
|
|
275
|
+
|
|
276
|
+
LOGGER_CLS = event_logger_lib.ConsoleEventLogger
|
|
277
|
+
|
|
278
|
+
@contextlib.contextmanager
|
|
279
|
+
def _capture_logs(self, test_name: str) -> Iterator[io.StringIO]:
|
|
280
|
+
try:
|
|
281
|
+
stream = io.StringIO()
|
|
282
|
+
with contextlib.redirect_stdout(stream):
|
|
283
|
+
yield stream
|
|
284
|
+
finally:
|
|
285
|
+
pass
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
if __name__ == '__main__':
|
|
289
|
+
unittest.main()
|