Net::DB...nnection(3)
π Net::DBus::Test::MockConnection β the perl manual page (perl v5.42.0 vibes)
okay so bestie, we're reading the Net::DBus::Test::MockConnection(3) page today and honestly? it's a whole mood. it's giving "fake it till you make it" but for message buses β¨
π NAME
Net::DBus::Test::MockConnection β literally fakes a connection to the bus so you can do unit testing π«Ά
πΈ SYNOPSIS
use Net::DBus;
my $bus = Net::DBus->test
# Register a service, and the objec to be tested
use MyObject
my $service = $bus->export_service("org.example.MyService");
my $object = MyObject->new($service);
# Acquire the service & do tests
my $remote_service = $bus->get_service('org.example.MyService');
my $remote_object = $service->get_object("/org/example/MyObjct");
# This traverses the mock connection, eventually
# invoking 'testSomething' on the $object above.
$remote_object->testSomething()
(no, we did not fix the typos, they're iconic and they're staying π copy it exactly as is, iykyk)
π DESCRIPTION
so this object is out here providing a fake implementation of Net::DBus::Binding::Connection, which lets you mock up a pure "in-memory" message bus. lowkey genius. the whole point is to make it easy to write unit tests for services that would otherwise have to call out to some other object on a live message bus β which, no cap, is a whole hassle π
it's the companion to the Net::DBus::Test::MockObject module, and that module is literally how you supply the fake objects on the fake bus. bestie duo behavior fr fr π
π¦ METHODS
my $con = Net::DBus::Test::MockConnection->new()β creates a fresh new mock connection object instance. ngl you usually don't need to make instances of this directly β instead just use the"test"method on theNet::DBusobject to get yourself a handle to a test bus. slay.$con->send($message)β sends a message over the mock connection. here's the tea on what happens next β:- if the message is a method call, it gets dispatched straight to any corresponding mock object that's registered
- if it's an error or a method return, it becomes available as a return value for the
"send_with_reply_and_block"method - if it's a signal, it gets queued up for processing by the
"dispatch"method
$bus->request_name($service_name)β pretends to send a request to the bus registering the well-known name given in the$service_nameparameter. in reality? it's just a no-op. it's giving "yes bestie you're registered" energy while doing absolutely nothing πmy $reply = $con->send_with_reply_and_block($msg)β sends a message over the mock connection and waits for a reply.$msgshould be an instance of"Net::DBus::Binding::Message::MethodCall", and the returned$replywill be an instance of"Net::DBus::Binding::Message::MethodReturn". also possible: an error gets thrown, and that thrown error is blessed into the"Net::DBus::Error"class π so like, be ready for that.$con->dispatch;β dispatches any pending messages in the incoming queue to their message handlers. test suites should call this whenever they anticipate there are pending signals to be dealt with. it's the "go handle your business" method π$con->add_filter($coderef);β adds a filter to the connection that gets invoked whenever a message is received. the$coderefshould be a reference to a subroutine, and that sub returns a true value if the message should be filtered out, or a false value if normal message dispatch should be performed. gatekeeping, but make it code β$bus->add_match($rule)β registers a signal match rule with the bus controller, letting matching broadcast signals be routed to this client. in reality? another no-op, just vibing and giving the impression that the match was successfully registered β¨$bus->remove_match($rule)β unregisters a signal match rule with the bus controller, preventing further broadcast signals from being routed to this client. in reality? you guessed it β a no-op giving the impression that the match was successfully unregistered π$con->register_object_path($path, \&handler)β registers a handler for messages whose path matches whatever's in the$pathparameter. the supplied code reference gets invoked with two parameters: the connection object the message was received on, and the message to be processed (an instance of the"Net::DBus::Binding::Message"class). two parameters, no more, no less π«Ά$con->register_fallback($path, \&handler)β same deal, but for messages whose path starts with the prefix given in$path. the code reference again gets invoked with two parameters: the connection object the message was received on, and the message to be processed (an instance of the"Net::DBus::Binding::Message"class). it's the catch-all bestie π$con->unregister_object_path($path)β unregisters the handler associated with the object path$path. that handler would previously have been registered with the"register_object_path"or"register_fallback"methods. closure era π¦my $msg = $con->make_error_message($replyto, $name, $description)β creates a new message representing an error that occurred while handling the method call object passed in as the$replytoparameter. the$nameparameter is the formal name of the error condition, and$descriptionis a short piece of text giving more specific info about the error. very "here's what went wrong, bestie" πmy $call = $con->make_method_call_message( $service_name, $object_path, $interface, $method_name);β creates a message representing a call on the object located at the path$object_pathwithin the client owning the well-known name given by$service_name. the method to be invoked has the name$method_namewithin the interface specified by the$interfaceparameter. four params, all accounted for πmy $msg = $con->make_method_return_message($replyto)β creates a message representing a reply to the method call message passed in the$replytoparameter. the "ok bestie i heard you" of messages πmy $msg = $con->make_signal_message($object_path, $interface, $signal_name);β creates a new message representing a signal [to be] emitted by the object located under the path given by$object_path. the name of the signal is given by$signal_name, and it's scoped to the interface given by$interface. shout it from the rooftops, but politely, within the right interface π£
π BUGS
it doesn't completely replicate the API of Net::DBus::Binding::Connection β merely enough to make the high level bindings work in a test scenario. so like, it's not a full clone, it's a vibe match. don't come for it π
π§ AUTHOR
Daniel P. Berrange
β COPYRIGHT
Copyright (C) 2005-2009 Daniel P. Berrange
π₯ SEE ALSO
Net::DBusNet::DBus::Test::MockObjectNet::DBus::Binding::Connection- <http://www.mockobjects.com/Faq.html>
and that's the whole page bestie, no notes π
this has been Net::DB...nnection(3) from the User Contributed Perl Documentation, perl v5.42.0, dated 2019-12-17 β mwah, go write some tests πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Net-DBus 1.2.0-160000.2.2 Β· GPL-2.0-or-later
- opensuse-leap 16.0: perl-Net-DBus 1.2.0-160000.2.2 Β· GPL-2.0-or-later
- opensuse-leap 16.0: perl-Net-DBus 1.2.0-160000.2.2 Β· GPL-2.0-or-later
- opensuse-leap 16.0: perl-Net-DBus 1.2.0-160000.2.2 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms