Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to register an additional name #175

Open
mardy opened this issue Jun 8, 2023 · 1 comment
Open

Add a method to register an additional name #175

mardy opened this issue Jun 8, 2023 · 1 comment

Comments

@mardy
Copy link

mardy commented Jun 8, 2023

Hi Martin! Long time no write, hope you are fine :-)

I'm testing a service that calls a method on an application (via D-Bus) as soon as the application's name is registered on the D-Bus session. I'm mocking the application using python-dbusmock, but it's not trivial because, for the way that mock objects are build, python-dbusmock first registers the service name (and it couldn't be otherwise, since we control the mock object over D-Bus), and only then we can add the objects/methods over D-Bus. This indeed causes a problem in my tests, because the service under tests will invoke the method on the application before the AddMethod() call has been done.

I thought of using a template, but since the object path, interface name and method name of the applications are not known in advance (they are read from the application's .desktop file), we would have to generate the template dynamically, and it makes the tests more complex then what they ought to be.

So I went for another solution, where I start the mock object under a different bus name (the original name with a "XXX" appended to it), add the application's method, and then register the real name. I do this last bit in this way:

        reg_interface, reg_method = ('test.mock', 'RegisterName')                                    
        mock_iface.AddMethod(reg_interface, reg_method, 's', '',
                             'self.bus_name.get_bus().request_name(args[0])')                        
        reg_iface = dbus.Interface(bus.get_object(reg_bus_name, object_path), reg_interface)
        reg_iface.RegisterName(bus_name)

This works fine, but I don't like that it uses internal variables of the mock object (that self.bus_name.get_bus() part in the RegisterName method's implementation). And it seems to me that this functionality might be useful to other people as well.

What do you think about adding a RegisterBusName method to the mock object? If you agree, I could create a PR, it doesn't sound too hard.

@martinpitt
Copy link
Owner

Hello @mardy! I'm good, thanks! I hope you as well 😁

Adding a RegisterBusName() helper to the mock object seems fine to me. That would indeed be cleaner than poking in the mock's innards. Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants