Skip to content

Commit

Permalink
Add test to confirm can receive signals from client with no message bus
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Sep 22, 2023
1 parent 6234d48 commit 0715d7b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/src/dbus_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class DBusSignalStream extends Stream<DBusSignal> {
_client._findUniqueName(_rule.sender!);
}
_client._addMatch(_rule.toDBusString());
} else {
_client._connect();
}
}

Expand Down Expand Up @@ -1043,7 +1045,7 @@ class DBusClient {
}

var signal = DBusSignal(
sender: message.sender?.value ?? '',
sender: message.sender?.value,
path: message.path ?? DBusObjectPath.root,
interface: message.interface?.value ?? '',
name: message.member?.value ?? '',
Expand Down
19 changes: 19 additions & 0 deletions lib/src/dbus_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,25 @@ class DBusServer {
}
}

/// Emits a signal from the D-Bus server.
void emitSignal(
{required DBusObjectPath path,
required String interface,
required String name,
Iterable<DBusValue> values = const []}) {
var message = DBusMessage(DBusMessageType.signal,
flags: {DBusMessageFlag.noReplyExpected},
serial: _nextSerial,
path: path,
interface: DBusInterfaceName(interface),
member: DBusMemberName(name),
values: values.toList());
_nextSerial++;
for (var client in _clients) {
client.sendMessage(message);
}
}

/// Listens for connections on a Unix socket.
Future<DBusAddress> _listenUnixSocket(DBusAddress address) async {
var path = address.properties['path'];
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dbus_signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dbus_value.dart';
/// A D-Bus signal.
class DBusSignal {
/// Client that sent the signal.
final String sender;
final String? sender;

/// Path of the object emitting the signal.
final DBusObjectPath path;
Expand Down
30 changes: 30 additions & 0 deletions test/dbus_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5040,6 +5040,36 @@ void main() {
expect(node.toXml().toXmlString(), equals('<node/>'));
});

test('no message bus - subscribe signal', () async {
var server = DBusServer(messageBus: false);
var address =
await server.listenAddress(DBusAddress.unix(dir: Directory.systemTemp));
var client = DBusClient(address, messageBus: false);
addTearDown(() async {
await client.close();
await server.close();
});

var signals =
DBusSignalStream(client, interface: 'com.example.Test', name: 'Ping');
signals.listen(expectAsync1((signal) {
expect(signal.sender, isNull);
expect(signal.path, equals(DBusObjectPath('/')));
expect(signal.interface, equals('com.example.Test'));
expect(signal.name, equals('Ping'));
expect(signal.values, equals([DBusString('Hello'), DBusUint32(42)]));
}));

// Ensure client is connected.
await client.ping();

server.emitSignal(
path: DBusObjectPath('/'),
interface: 'com.example.Test',
name: 'Ping',
values: [DBusString('Hello'), DBusUint32(42)]);
});

test('introspect xml - empty', () {
expect(() => parseDBusIntrospectXml(''), throwsFormatException);
});
Expand Down

0 comments on commit 0715d7b

Please sign in to comment.