Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request dbusjs#87 from sdrik/fix-initial-signals-lost
Browse files Browse the repository at this point in the history
Don't loose signals when no previous method calls have been made.
  • Loading branch information
Tony Crisci authored Sep 18, 2021
2 parents 850786d + 4ac6862 commit ac7b17a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/client/proxy-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,25 @@ class ProxyObject {
return reject(err);
}
this._initXml(data);
resolve(this);

const nameOwnerMessage = new Message({
destination: 'org.freedesktop.DBus',
path: '/org/freedesktop/DBus',
interface: 'org.freedesktop.DBus',
member: 'GetNameOwner',
signature: 's',
body: [this.name]
});

this.bus.call(nameOwnerMessage)
.then((msg) => {
this.bus._nameOwners[this.name] = msg.body[0];
resolve(this);
})
.catch((err) => {
if (err.type === 'org.freedesktop.DBus.Error.NameHasNoOwner') { return resolve(this); }
return reject(err);
});
});
} else {
const introspectMessage = new Message({
Expand Down
72 changes: 72 additions & 0 deletions test/integration/signals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,56 @@ const TEST_NAME = 'org.test.signals';
const TEST_NAME2 = 'org.test.signals_name2';
const TEST_PATH = '/org/test/path';
const TEST_IFACE = 'org.test.iface';
const TEST_XML = `
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect">
<arg name="data" direction="out" type="s"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Peer">
<method name="GetMachineId">
<arg direction="out" name="machine_uuid" type="s"/>
</method>
<method name="Ping"/>
</interface>
<interface name="org.freedesktop.DBus.Properties">
<method name="Get">
<arg direction="in" type="s"/>
<arg direction="in" type="s"/>
<arg direction="out" type="v"/>
</method>
<method name="Set">
<arg direction="in" type="s"/>
<arg direction="in" type="s"/>
<arg direction="in" type="v"/>
</method>
<method name="GetAll">
<arg direction="in" type="s"/>
<arg direction="out" type="a{sv}"/>
</method>
<signal name="PropertiesChanged">
<arg type="s"/>
<arg type="a{sv}"/>
<arg type="as"/>
</signal>
</interface>
<interface name="org.test.iface">
<method name="EmitSignals"/>
<signal name="HelloWorld">
<arg type="s"/>
</signal>
<signal name="SignalMultiple">
<arg type="s"/>
<arg type="s"/>
</signal>
<signal name="SignalComplicated">
<arg type="v"/>
</signal>
</interface>
</node>
`;

const bus = dbus.sessionBus();
bus.on('error', (err) => {
Expand Down Expand Up @@ -190,3 +240,25 @@ test('regression #64: adding multiple listeners to a signal', async () => {
expect(cb2).toHaveBeenCalledTimes(1);
expect(cb3).toHaveBeenCalledTimes(2);
});

test('bug #XXX: signals dont get lost when no previous method calls have been made', async () => {
// clear the name owners cache from previous tests
bus._nameOwners = {};

// when providing XML data, no introspection call is made
const object = await bus.getProxyObject(TEST_NAME, TEST_PATH, TEST_XML);
const test = object.getInterface(TEST_IFACE);
const cb = jest.fn();

test.on('HelloWorld', cb);
test.on('SignalMultiple', cb);
test.on('SignalComplicated', cb);

// don't call EmitSignals through the proxy object
testIface.EmitSignals();

// allow signal handlers to run
await new Promise(resolve => { setTimeout(resolve, 0); });

expect(cb).toHaveBeenCalledTimes(3);
});

0 comments on commit ac7b17a

Please sign in to comment.