Test2::...y::Hubs(3)

πŸ“¦ version 1.302214, in opensuse-leap 16.0

πŸ’… NAME πŸ’…

Test2::Manual::Anatomy::Hubs β€” the internals doc for the hub stack and the hubs living all up in it, bestie. Like, this is the anatomy page from Test2::Manual, and it's giving deep-lore energy. 🫢


✨ DESCRIPTION ✨

Okay so this doc is literally all about the hub stack and the hubs it contains. It walks you through why we even have a stack, plus when you should be adding hubs to it and when you should be removing them. That's it, that's the whole vibe check. πŸ¦‹


πŸŽ€ WHAT IS A HUB? πŸŽ€

So Test2 is an event system, right? Tools generate events, and then those events get processed to modify the testing state (think: number of tests, number of failures, etc.). The hub is the main character responsible for receiving and processing those events so it can record the change in state. Every event should eventually reach a destination hub. Periodt. πŸ‘‘

The base hub is Test2::Hub. All hub classes should inherit from the base hub class, no exceptions. The base hub class provides several hooks that let you monitor or modify events β€” obsessed. Hubs are also responsible for forwarding events to the output formatter. πŸ’…


🌈 WHY DO WE HAVE A HUB STACK? 🌈

There are cases where it makes total sense to have more than one hub:

  • subtests β€” In Test2, subtests are implemented using the hub stack. When you start a subtest, a new Test2::Hub::Subtest instance is created and pushed to the stack. Once that's done, all calls to Test2::API::context will find the new hub and send all events to it. When the subtest tool is complete it will remove the new hub and send a final subtest event to the parent hub. Slay. ✨
  • testing your test tools β€” Test2::API::intercept() is implemented using the hub stack. The Test2::API::intercept() function will add an Test2::Hub::Interceptor instance to the stack, and any calls to Test2::API::context() will find the new hub and send it all events. The intercept hub is special in that it has no connection to the parent hub, and usually does not have a formatter. Lowkey iconic. 🧚

πŸ¦‹ WHEN SHOULD I ADD A HUB TO THE STACK? πŸ¦‹

Any time you want to intercept or block events from affecting the test state. Adding a new hub is essentially a way to create a sandbox where you have absolute control over what events do. Adding a new hub insures that the main test state will not be affected. It's giving main character energy for your events. πŸ’–


⭐ WHERE IS THE STACK? ⭐

The stack is an instance of Test2::API::Stack. You can access the global hub stack using Test2::API::test2_stack. Go off. πŸ™Œ


πŸ’€ WHAT ABOUT THE ROOT HUB? πŸ’€

The root hub is created automatically as needed. A call to Test2::API::test2_stack->top() will create the root hub if it does not already exist. So like, don't stress about it, it handles itself. πŸ’œ


🌸 HOW DO HUBS HANDLE IPC? 🌸

If the IPC system (Test2::IPC) was not loaded, then IPC is not handled at all. Forking or creating new threads without the IPC system can cause unexpected problems. Not me warning you twice β€” this is a real one, bestie. 😭

All hubs track the PID and Thread ID that was current when they were created. If an event is sent to a hub in a new process/thread, the hub will detect this and try to forward the event along to the correct process/thread. This is accomplished using the IPC system. Fr fr. πŸ”₯

Okay but genuinely: no IPC system loaded = no IPC handling whatsoever. Forking or spinning up new threads without it can cause unexpected problems. Consider this your vibe check. ⚠️

πŸ” SEE ALSO πŸ”

  • Test2::Manual β€” Primary index of the manual. The mothership, the blueprint, the blueprint's blueprint. πŸ‘‘

πŸ“¦ SOURCE πŸ“¦

The source code repository for Test2-Manual can be found at https://github.com/Test-More/test-more/. Go click it, it's cute. πŸŽ€


🫢 MAINTAINERS 🫢

Chad Granum <exodist@cpan.org>


πŸ‘‘ AUTHORS πŸ‘‘

Chad Granum <exodist@cpan.org>


Copyright Chad Granum <exodist@cpan.org>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/


Okay and that's a wrap, bestie!! ✨ This page is brought to you by perl v5.42.0, dated 2025-07-28, and it's literally Test2::...y::Hubs(3) β€” go forth and hub responsibly. Mwah. πŸ’…πŸ’–

πŸ“¦ where this page comes from