Skip to content

Commit

Permalink
Merge pull request #140 from eclipse-thingweb/fix-consume-method
Browse files Browse the repository at this point in the history
feat!: allow for consuming the same TD twice
  • Loading branch information
JKRhb authored May 19, 2024
2 parents b9f39b9 + b54450e commit 0949b81
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
8 changes: 7 additions & 1 deletion lib/src/core/implementation/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class ConsumedThing implements scripting_api.ConsumedThing {
}

/// Cleans up the resources used by this [ConsumedThing].
void destroy() {
bool destroy({bool external = true}) {
for (final observedProperty in _observedProperties.values) {
observedProperty.stop();
}
Expand All @@ -426,5 +426,11 @@ class ConsumedThing implements scripting_api.ConsumedThing {
subscribedEvent.stop();
}
_subscribedEvents.clear();

if (external) {
return servient.deregisterConsumedthing(this);
}

return false;
}
}
34 changes: 13 additions & 21 deletions lib/src/core/implementation/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Servient {
final List<ProtocolServer> _servers = [];
final Map<String, ProtocolClientFactory> _clientFactories = {};
final Map<String, ExposedThing> _things = {};
final Map<String, ConsumedThing> _consumedThings = {};
final Set<ConsumedThing> _consumedThings = {};

final ServerSecurityCallback? _serverSecurityCallback;

Expand Down Expand Up @@ -85,7 +85,7 @@ class Servient {
clientFactory.destroy();
}
_clientFactories.clear();
for (final consumedThing in _consumedThings.values) {
for (final consumedThing in _consumedThings) {
consumedThing.destroy();
}
_consumedThings.clear();
Expand Down Expand Up @@ -135,36 +135,28 @@ class Servient {
return true;
}

/// Removes and cleans up the resources of the [ConsumedThing] with the given
/// [id].
/// Removes and cleans up the resources of a [ConsumedThing].
///
/// If the [ConsumedThing] has not been registered before, `false` is
/// returned, otherwise `true`.
bool destroyConsumedThing(String id) {
final existingThing = _consumedThings.remove(id);

if (existingThing != null) {
existingThing.destroy();
return true;
}
bool destroyConsumedThing(ConsumedThing consumedThing) {
return consumedThing.destroy(external: false);
}

return false;
/// Deregisters the given [consumedThing].
///
/// If the [ConsumedThing] has not been registered before, `false` is
/// returned, otherwise `true`.
bool deregisterConsumedthing(ConsumedThing consumedThing) {
return _consumedThings.remove(consumedThing);
}

/// Adds a [ConsumedThing] to the servient if it hasn't been registered
/// before.
///
/// Returns `false` if the [thing] has already been registered, otherwise
/// `true`.
bool addConsumedThing(ConsumedThing thing) {
final id = thing.identifier;
if (_consumedThings.containsKey(id)) {
return false;
}

_consumedThings[id] = thing;
return true;
}
bool addConsumedThing(ConsumedThing thing) => _consumedThings.add(thing);

/// Returns an [ExposedThing] with the given [id] if it has been registered.
ExposedThing? thing(String id) => _things[id];
Expand Down
9 changes: 3 additions & 6 deletions lib/src/core/implementation/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ class WoT implements scripting_api.WoT {
ThingDescription thingDescription,
) async {
final newThing = ConsumedThing(_servient, thingDescription);
if (_servient.addConsumedThing(newThing)) {
return newThing;
} else {
final id = thingDescription.identifier;
throw DartWotException(id);
}
_servient.addConsumedThing(newThing);

return newThing;
}

/// Exposes a Thing based on a (partial) TD.
Expand Down
1 change: 0 additions & 1 deletion test/core/consumed_thing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ void main() {
);

await servient.shutdown();
expect(servient.destroyConsumedThing(parsedTd.identifier), false);
},
skip: true, // TODO: Replace with test with local server
);
Expand Down
8 changes: 4 additions & 4 deletions test/core/wot_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "package:test/test.dart";

void main() {
group("WoT should", () {
test("throw an execption when consuming the same TD twice", () async {
test("not throw an execption when consuming the same TD twice", () async {
const thingDescriptionJson = {
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "Test Thing",
Expand All @@ -22,9 +22,9 @@ void main() {

final wot = await Servient().start();

await wot.consume(thingDescription);
final result = wot.consume(thingDescription);
await expectLater(result, throwsA(isA<DartWotException>()));
final firstConsumedThing = await wot.consume(thingDescription);
final secondConsumedThing = await wot.consume(thingDescription);
expect(firstConsumedThing != secondConsumedThing, isTrue);
});

test(
Expand Down

0 comments on commit 0949b81

Please sign in to comment.