(self)
| 383 | |
| 384 | class TestAndroidRateLimit(unittest.TestCase): |
| 385 | def test_rate_limit(self): |
| 386 | # https://cs.android.com/android/platform/superproject/+/android-14.0.0_r1:system/logging/liblog/include/log/log_read.h;l=39 |
| 387 | PER_MESSAGE_OVERHEAD = 28 |
| 388 | |
| 389 | # https://developer.android.com/ndk/reference/group/logging |
| 390 | ANDROID_LOG_DEBUG = 3 |
| 391 | |
| 392 | # To avoid flooding the test script output, use a different tag rather |
| 393 | # than stdout or stderr. |
| 394 | tag = "python.rate_limit" |
| 395 | stream = TextLogStream(ANDROID_LOG_DEBUG, tag) |
| 396 | |
| 397 | # Make a test message which consumes 1 KB of the logcat buffer. |
| 398 | message = "Line {:03d} " |
| 399 | message += "." * ( |
| 400 | 1024 - PER_MESSAGE_OVERHEAD - len(tag) - len(message.format(0)) |
| 401 | ) + "\n" |
| 402 | |
| 403 | # To avoid depending on the performance of the test device, we mock the |
| 404 | # passage of time. |
| 405 | mock_now = time() |
| 406 | |
| 407 | def mock_time(): |
| 408 | # Avoid division by zero by simulating a small delay. |
| 409 | mock_sleep(0.0001) |
| 410 | return mock_now |
| 411 | |
| 412 | def mock_sleep(duration): |
| 413 | nonlocal mock_now |
| 414 | mock_now += duration |
| 415 | |
| 416 | # See _android_support.py. The default values of these parameters work |
| 417 | # well across a wide range of devices, but we'll use smaller values to |
| 418 | # ensure a quick and reliable test that doesn't flood the log too much. |
| 419 | MAX_KB_PER_SECOND = 100 |
| 420 | BUCKET_KB = 10 |
| 421 | with ( |
| 422 | patch("_android_support.MAX_BYTES_PER_SECOND", MAX_KB_PER_SECOND * 1024), |
| 423 | patch("_android_support.BUCKET_SIZE", BUCKET_KB * 1024), |
| 424 | patch("_android_support.sleep", mock_sleep), |
| 425 | patch("_android_support.time", mock_time), |
| 426 | ): |
| 427 | # Make sure the token bucket is full. |
| 428 | stream.write("Initial message to reset _prev_write_time") |
| 429 | mock_sleep(BUCKET_KB / MAX_KB_PER_SECOND) |
| 430 | line_num = 0 |
| 431 | |
| 432 | # Write BUCKET_KB messages, and return the rate at which they were |
| 433 | # accepted in KB per second. |
| 434 | def write_bucketful(): |
| 435 | nonlocal line_num |
| 436 | start = mock_time() |
| 437 | max_line_num = line_num + BUCKET_KB |
| 438 | while line_num < max_line_num: |
| 439 | stream.write(message.format(line_num)) |
| 440 | line_num += 1 |
| 441 | return BUCKET_KB / (mock_time() - start) |
| 442 |
nothing calls this directly
no test coverage detected