Skip to content

Commit 4372789

Browse files
committed
✨ add error handling tests for stream value retrieval in storage containers
1 parent f4b4eb5 commit 4372789

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

packages/hyper_storage/test/hyper_storage_container_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,41 @@ void main() {
808808
await container2.close();
809809
await backend2.close();
810810
});
811+
812+
test('stream() handles errors during value retrieval', () async {
813+
// Create a backend that will throw an error
814+
final errorBackend = ContainerErrorThrowingBackend();
815+
await errorBackend.init();
816+
final errorContainer = HyperStorageContainer(backend: errorBackend, name: 'test');
817+
818+
final stream = errorContainer.stream<String>('errorKey');
819+
final errors = <Object>[];
820+
821+
final subscription = stream.listen(
822+
(_) {},
823+
onError: errors.add,
824+
);
825+
826+
await Future.delayed(Duration(milliseconds: 50));
827+
await subscription.cancel();
828+
829+
expect(errors, isNotEmpty);
830+
expect(errors.first, isA<Exception>());
831+
832+
await errorContainer.close();
833+
await errorBackend.close();
834+
});
811835
});
812836
});
813837
}
838+
839+
// Backend that throws errors for testing error handling in containers
840+
class ContainerErrorThrowingBackend extends InMemoryBackend {
841+
@override
842+
Future<String?> getString(String key) async {
843+
if (key.endsWith('errorKey')) {
844+
throw Exception('Test error during getString in container');
845+
}
846+
return super.getString(key);
847+
}
848+
}

packages/hyper_storage/test/hyper_storage_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,43 @@ void main() {
11621162
expect(values2, contains('value2'));
11631163
expect(values2, isNot(contains('updated1')));
11641164
});
1165+
1166+
test('stream() handles errors during value retrieval', () async {
1167+
// Create a backend that will throw an error
1168+
final errorBackend = ErrorThrowingBackend();
1169+
final storage = await HyperStorage.newInstance(backend: errorBackend);
1170+
1171+
final stream = storage.stream<String>('errorKey');
1172+
final errors = <Object>[];
1173+
1174+
final subscription = stream.listen(
1175+
(_) {},
1176+
onError: errors.add,
1177+
);
1178+
1179+
await Future.delayed(Duration(milliseconds: 50));
1180+
await subscription.cancel();
1181+
1182+
expect(errors, isNotEmpty);
1183+
expect(errors.first, isA<Exception>());
1184+
1185+
await storage.close();
1186+
});
11651187
});
11661188
});
11671189
}
11681190

1191+
// Backend that throws errors for testing error handling
1192+
class ErrorThrowingBackend extends InMemoryBackend {
1193+
@override
1194+
Future<String?> getString(String key) async {
1195+
if (key == 'errorKey') {
1196+
throw Exception('Test error during getString');
1197+
}
1198+
return super.getString(key);
1199+
}
1200+
}
1201+
11691202
// Custom container implementation for testing objectContainer
11701203
class UserContainer extends SerializableStorageContainer<User> {
11711204
UserContainer({

0 commit comments

Comments
 (0)