
UtilsTest.pudu
Pudu56 lines2.7 KB
1/** @Test.Utils.Suite — templates, shared state, and linked tokens */2module PuduLangHttpClient.Utils.UtilsTest34import Std.Concurrent.Cancel as Cancel5import Std.Concurrent as Concurrent6import Std.Io as Io7import Std.Test as Test8import PuduLangHttpClient.Constants.Defaults as Defaults9import PuduLangHttpClient.Utils.Shared as Shared10import PuduLangHttpClient.Utils.Template as Template11import PuduLangHttpClient.Utils.Tokens as Tokens121314fn main() -> Int {15 let counter = Shared.shared(0)16 let workers = [1, 2, 3, 4, 5, 6, 7, 8].map(|_n: Int| match Concurrent.start(fn() -> () {17 var index = 018 while index < 100 {19 Shared.update(&counter, |count: Int| count + 1)20 index = index + 121 }22 }) {23 case Ok(started) => started24 case Err(problem) => panic(show(problem))25 })26 let _joined = Concurrent.joinAll(&workers)27 let parent = Cancel.token()28 let other = Cancel.token()29 let linked = Tokens.linked(&[parent, other], Defaults.INFINITE)30 let quiet = Cancel.stopped(&linked)31 let _cancelled = Cancel.cancel(&other, "other stopped")32 let child = Tokens.linked(&[Cancel.token()], Defaults.INFINITE)33 let _own = Cancel.cancel(&child, "child only")34 let expiring = Tokens.linked(&[Cancel.expiring(10000)], 5)35 let _waited = Concurrent.sleep(20)36 let inherited = Tokens.linked(&[Cancel.expiring(5)], 10000)37 let checks = Test.suite("Utils", &[38 Test.equals("slots are filled once, in order", &Template.fill("<1> and <2> but not <3>", &["a", "<2>"]), &"a and <2> but not <3>"),39 Test.equals("concurrent updates are never lost", &Shared.current(&counter), &800),40 Test.equals("a change answers what its step computed", &Shared.change(&counter, |count: Int| (count + 1, count * 2)), &1600),41 Test.that("a linked token is quiet until a parent fires", !quiet && Cancel.stopped(&linked)),42 Test.equals("a linked token reports the parent's reason", &Cancel.reason(&linked), &Some(Cancel.Requested("other stopped"))),43 Test.not("cancelling a linked token leaves its parents running", Cancel.stopped(&parent)),44 Test.that("a linked token fires on its own", Cancel.stopped(&child)),45 Test.that("a linked token's own timeout fires before a parent's later deadline", Cancel.stopped(&expiring)),46 Test.that("a parent's earlier deadline is inherited", Cancel.remaining(&inherited) != None && Cancel.remaining(&inherited) != Some(10000)),47 Test.equals("an infinite timeout without a deadline leaves none", &Cancel.remaining(&Tokens.linked(&[Cancel.token()], Defaults.INFINITE)), &None),48 Test.that("a negative timeout fires at once", Cancel.stopped(&Tokens.linked(&[], -5)))49 ])50 let ran = Test.run(&checks)51 for failure in Test.failuresOf(&ran) {52 let _reported = Io.writeErrorLine(failure)53 }54 Test.report(&ran)55}56