langfun 0.1.2.dev202509250804__py3-none-any.whl → 0.1.2.dev202509260805__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.

@@ -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()