diff --git a/src/allow_failure_test.mbt b/src/allow_failure_test.mbt index c8d9918..3d652be 100644 --- a/src/allow_failure_test.mbt +++ b/src/allow_failure_test.mbt @@ -14,7 +14,8 @@ ///| async test "allow_failure ignored" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(allow_failure=true, fn() { raise Err }) @@ -32,7 +33,8 @@ async test "allow_failure ignored" { ///| async test "allow_failure waited" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() buf.write_object( try? @async.with_task_group(fn(root) { @@ -47,7 +49,8 @@ async test "allow_failure waited" { ///| async test "allow_failure no error" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() buf.write_object( try? @async.with_task_group(fn(root) { diff --git a/src/cancellation_test.mbt b/src/cancellation_test.mbt index 505bc63..3da84a5 100644 --- a/src/cancellation_test.mbt +++ b/src/cancellation_test.mbt @@ -14,7 +14,8 @@ ///| async test "manual cancel" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() let result = try? @async.with_task_group(fn(root) { let task = root.spawn(allow_failure=true, fn() { @@ -49,7 +50,8 @@ suberror Err derive(Show) ///| async test "error propagation" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() let result = try? @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -85,7 +87,8 @@ async test "error propagation" { ///| async test "multiple scope" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -151,7 +154,8 @@ async test "multiple scope" { ///| async test "recursive cancel" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() buf.write_object( try? @async.with_task_group(fn(root) { @@ -191,7 +195,8 @@ async test "recursive cancel" { ///| async test "spawn directly error" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() buf.write_object( try? @async.with_task_group(fn(root) { @@ -218,7 +223,8 @@ async test "spawn directly error" { ///| async test "error in cancellation" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -244,7 +250,8 @@ async test "error in cancellation" { ///| async test "immediately cancelled" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { let task = root.spawn(allow_failure=true, fn() { diff --git a/src/fs/walk_test.mbt b/src/fs/walk_test.mbt index e171121..f09517a 100644 --- a/src/fs/walk_test.mbt +++ b/src/fs/walk_test.mbt @@ -28,6 +28,7 @@ async test "walk sequential" { ///| async test "walk parallel" { + @async.sleep(300) let log = StringBuilder::new() @fs.walk("test_directory", max_concurrency=2, fn(path, _) { log.write_string("handling \{path}\n") diff --git a/src/group_defer_test.mbt b/src/group_defer_test.mbt index 8df1d97..9aa8ad5 100644 --- a/src/group_defer_test.mbt +++ b/src/group_defer_test.mbt @@ -14,7 +14,8 @@ ///| async test "group defer basic" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.add_defer(() => log.write_string("first group defer\n")) @@ -55,7 +56,8 @@ async test "group defer basic" { ///| async test "group defer error" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -98,7 +100,8 @@ async test "group defer error" { ///| async test "group defer cancelled" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { diff --git a/src/no_wait_test.mbt b/src/no_wait_test.mbt index 26cb3e2..e6f7aea 100644 --- a/src/no_wait_test.mbt +++ b/src/no_wait_test.mbt @@ -14,7 +14,8 @@ ///| async test "no_wait cancelled" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(no_wait=true, fn() { @@ -42,7 +43,8 @@ async test "no_wait cancelled" { ///| async test "no_wait normal exit" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(no_wait=true, fn() { diff --git a/src/protect_from_cancel_test.mbt b/src/protect_from_cancel_test.mbt index 7577719..486705c 100644 --- a/src/protect_from_cancel_test.mbt +++ b/src/protect_from_cancel_test.mbt @@ -14,7 +14,8 @@ ///| async test "protect_from_cancel" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -62,7 +63,8 @@ async test "protect_from_cancel" { ///| async test "protect_from_cancel wait" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -105,7 +107,8 @@ async test "protect_from_cancel wait" { ///| async test "protect_from_cancel with_timeout" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -137,7 +140,8 @@ async test "protect_from_cancel with_timeout" { ///| async test "protect_from_cancel fail" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -169,7 +173,8 @@ async test "protect_from_cancel fail" { ///| async test "protect_from_cancel nested1" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -203,7 +208,8 @@ async test "protect_from_cancel nested1" { ///| async test "protect_from_cancel nested2" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -243,7 +249,8 @@ async test "protect_from_cancel nested2" { ///| async test "protect_from_cancel in cancellation handler" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -276,7 +283,8 @@ async test "protect_from_cancel in cancellation handler" { ///| async test "cancel while scheduled" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -320,7 +328,8 @@ async test "cancel while scheduled" { ///| async test "error in async cancel" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { diff --git a/src/random_delay_test.mbt b/src/rate_limit_for_test.mbt similarity index 51% rename from src/random_delay_test.mbt rename to src/rate_limit_for_test.mbt index 1c50ee3..1ca2bf9 100644 --- a/src/random_delay_test.mbt +++ b/src/rate_limit_for_test.mbt @@ -13,25 +13,4 @@ // limitations under the License. ///| -/// add a random delay before every test, -/// so that there won't be a lot of timers expiring at the same time, -/// causing inaccurate result -async fn random_delay() -> Unit { - let rand = match rand_seed.val { - Some(r) => r - None => { - let now = @async.now() - let seed = @buffer.new() - for _ in 0..<4 { - seed.write_int64_le(now) - } - let rand = @random.Rand::chacha8(seed=seed.to_bytes()) - rand_seed.val = Some(rand) - rand - } - } - @async.sleep(rand.int(limit=100)) -} - -///| -let rand_seed : Ref[@random.Rand?] = @ref.new(None) +let rate_limit : @semaphore.Semaphore = @semaphore.Semaphore::new(20) diff --git a/src/retry_test.mbt b/src/retry_test.mbt index 1e3f9cc..726858e 100644 --- a/src/retry_test.mbt +++ b/src/retry_test.mbt @@ -14,7 +14,8 @@ ///| async test "retry immediate" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() let mut i = 0 @async.retry(Immediate, fn() { @@ -37,7 +38,8 @@ async test "retry immediate" { ///| async test "retry fixed" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -72,7 +74,8 @@ async test "retry fixed" { ///| async test "retry exponential" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -116,7 +119,8 @@ async test "retry exponential" { ///| async test "retry cancelled1" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() let mut i = 0 @async.with_timeout(500, fn() { @@ -142,7 +146,8 @@ async test "retry cancelled1" { ///| async test "retry cancelled2" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() let mut i = 0 @async.with_timeout(500, fn() { @@ -168,7 +173,8 @@ async test "retry cancelled2" { ///| async test "retry fatal_error" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() fn fatal_error(err) { not(err is @async.TimeoutError) diff --git a/src/return_immediately_test.mbt b/src/return_immediately_test.mbt index 9e9bbaf..b812a78 100644 --- a/src/return_immediately_test.mbt +++ b/src/return_immediately_test.mbt @@ -14,7 +14,8 @@ ///| async test "return_immediately basic" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -33,7 +34,8 @@ async test "return_immediately basic" { ///| async test "return_immediately nested" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -58,7 +60,8 @@ async test "return_immediately nested" { ///| async test "return_immediately error on cancel" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { diff --git a/src/spawn_loop_test.mbt b/src/spawn_loop_test.mbt index d826d7f..851e446 100644 --- a/src/spawn_loop_test.mbt +++ b/src/spawn_loop_test.mbt @@ -14,7 +14,8 @@ ///| async test "spawn_loop basic" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -43,7 +44,8 @@ async test "spawn_loop basic" { ///| async test "spawn_loop basic-error" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -71,7 +73,8 @@ async test "spawn_loop basic-error" { ///| async test "spawn_loop retry-exponential" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { diff --git a/src/spawn_test.mbt b/src/spawn_test.mbt index f024caa..11dbb51 100644 --- a/src/spawn_test.mbt +++ b/src/spawn_test.mbt @@ -14,7 +14,8 @@ ///| async test "spawn basic" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -50,7 +51,8 @@ async test "spawn basic" { ///| async test "spawn error1" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() let result = try? @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -68,7 +70,8 @@ async test "spawn error1" { ///| async test "spawn error2" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() let result = try? @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -86,7 +89,8 @@ async test "spawn error2" { ///| async test "spawn after cancelled 1" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -120,7 +124,8 @@ async test "spawn after cancelled 1" { ///| async test "spawn after cancelled 2" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() buf.write_object( try? @async.with_task_group(fn(root) { diff --git a/src/timer_test.mbt b/src/timer_test.mbt index 756a1be..ca1415d 100644 --- a/src/timer_test.mbt +++ b/src/timer_test.mbt @@ -14,7 +14,8 @@ ///| async test "basic_timer" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() let result = try? @async.with_task_group(fn(root) { root.spawn_bg(fn() { diff --git a/src/wait_test.mbt b/src/wait_test.mbt index debc5d9..f08b086 100644 --- a/src/wait_test.mbt +++ b/src/wait_test.mbt @@ -14,7 +14,8 @@ ///| async test "wait basic" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() buf.write_object( try? @async.with_task_group(fn(root) { @@ -46,7 +47,8 @@ async test "wait basic" { ///| async test "wait cancelled" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let buf = StringBuilder::new() @async.with_task_group(fn(root) { let task = root.spawn(fn() { @@ -77,7 +79,8 @@ async test "wait cancelled" { ///| async test "try_wait" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { let task = root.spawn(no_wait=true, fn() { diff --git a/src/with_timeout_test.mbt b/src/with_timeout_test.mbt index 1e51504..3915e24 100644 --- a/src/with_timeout_test.mbt +++ b/src/with_timeout_test.mbt @@ -14,7 +14,8 @@ ///| async test "with_timeout normal exit" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -42,7 +43,8 @@ async test "with_timeout normal exit" { ///| async test "with_timeout failure" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -72,7 +74,8 @@ async test "with_timeout failure" { ///| async test "with_timeout timeout" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -108,7 +111,8 @@ async test "with_timeout timeout" { ///| async test "with_timeout nested1" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -152,7 +156,8 @@ async test "with_timeout nested1" { ///| async test "with_timeout nested2" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -195,7 +200,8 @@ async test "with_timeout nested2" { ///| async test "with_timeout error_on_cancel" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -226,7 +232,8 @@ async test "with_timeout error_on_cancel" { ///| async test "with_timeout error-on-timeout" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -258,7 +265,8 @@ async test "with_timeout error-on-timeout" { ///| async test "with_timeout_opt normal exit" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) { @@ -289,7 +297,8 @@ async test "with_timeout_opt normal exit" { ///| async test "with_timeout_opt failure" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() @async.with_task_group(fn(root) { root.spawn_bg(fn() { @@ -319,7 +328,8 @@ async test "with_timeout_opt failure" { ///| async test "with_timeout_opt timeout" { - random_delay() + rate_limit.acquire() + defer rate_limit.release() let log = StringBuilder::new() log.write_object( try? @async.with_task_group(fn(root) {