
LoggingTest.pudu
Pudu60 lines3.2 KB
1/** @Test.Behaviors.Logging.Suite — structured events for requests, streams, and notifications */2module PuduLangMediator.Behaviors.LoggingTest34import Std.Io as Io5import Std.Test as Test6import PuduLangLog.Configuration as Configuration7import PuduLangLog as Log8import PuduLangLog.Sinks.Memory as Memory9import PuduLangMediator.Behaviors.Logging as Logging10import PuduLangMediator.Context as Context11import PuduLangMediator.Mediator as Mediator12import PuduLangMediator as Messaging13import PuduLangMediator.Notification as Notification14import PuduLangMediator.Registration as Registration15import PuduLangMediator.Request as Request16import PuduLangMediator.Stream as Stream171819fn main() -> Int {20 let memory = Memory.create()21 let logger = Configuration.create().minimumLevel(Log.Debug).writeTo(Memory.sink(&memory)).createLogger()22 let recorder = Logging.of(logger)23 let price: Request.Kind[Str, Int, Str] = Request.kind("prices.get")24 let ticks: Stream.Kind[Int, Int, Str] = Stream.kind("ticks")25 let changed: Notification.Kind[Int, Str] = Notification.kind("prices.changed")26 let mediator = match Mediator.build([27 Registration.openBehavior(recorder),28 Registration.openStreamBehavior(recorder),29 Registration.openNotificationHandler("log", recorder),30 Registration.handler(&price, |item: Str, _context: Context.Context| if item == "gold" { Ok(1900) } else { Messaging.raise("unknown item") }),31 Registration.streamHandler(&ticks, fn(count: Int, _context: Context.Context, sink: Stream.Sink[Int]) -> Messaging.Outcome[(), Str] {32 Stream.each(&[1, 2, 3, 4].slice(0, count), sink)33 }),34 Registration.notificationHandler(&changed, "noop", |_value: Int, _context: Context.Context| Ok(()))35 ]) {36 case Ok(found) => found37 case Err(invalid) => panic(Mediator.explain(&invalid))38 }39 let found = Mediator.send(&mediator, &price, "gold")40 let missing = Mediator.send(&mediator, &price, "lead")41 let streamed = Mediator.collect(&mediator, &ticks, 3)42 let published = Mediator.publish(&mediator, &changed, 5)43 let messages = Memory.messages(&memory)44 let checks = Test.suite("Behaviors.Logging", &[45 Test.equals("logging changes no outcome", &(found, missing, streamed, published), &(Ok(1900), Err(Messaging.Raised("unknown item")), Ok([1, 2, 3]), Ok(()))),46 Test.equals("every start is a debug event", &Memory.withTemplate(&memory, Logging.STARTED).length(), &3),47 Test.that("a start names the kind and its shape", messages[0] == "Handling \"prices.get\" (\"query\")"),48 Test.equals("a success is an information event", &Memory.withTemplate(&memory, Logging.SUCCEEDED).map(|event: Log.Event| event.level), &[Log.Information]),49 Test.equals("a failure is a warning event", &Memory.withTemplate(&memory, Logging.FAILED).map(|event: Log.Event| event.level), &[Log.Warning]),50 Test.that("a failure names what went wrong", messages[3].contains("The handler failed: \\\"unknown item\\\"")),51 Test.that("a stream reports how many items it emitted", messages[5].startsWith("Streamed \"ticks\": 3 items in ")),52 Test.equals("a publication is heard", &Memory.withTemplate(&memory, Logging.HEARD).length(), &1)53 ])54 let ran = Test.run(&checks)55 for failure in Test.failuresOf(&ran) {56 let _reported = Io.writeErrorLine(failure)57 }58 Test.report(&ran)59}60