Skip to content

Commit

Permalink
test: add timeout in matrix test & overridden DBs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasn committed Dec 9, 2024
1 parent dbff4de commit 8bf4889
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
16 changes: 7 additions & 9 deletions integration_test/matrix_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ void main() {
() => mockUpdateNotifications.notify(any()),
).thenAnswer((_) {});

getIt
..registerSingleton<UpdateNotifications>(mockUpdateNotifications)
..registerSingleton<UserActivityService>(UserActivityService());

final aliceDb = JournalDb(
overriddenFilename: 'alice_db.sqlite',
inMemoryDatabase: true,
Expand Down Expand Up @@ -115,18 +111,16 @@ void main() {
..createSync(recursive: true);
debugPrint('Created temporary docDir ${docDir.path}');

final mockSettingsDb = MockSettingsDb();

getIt
..registerSingleton<Directory>(docDir)
..registerSingleton<LoggingDb>(LoggingDb(inMemoryDatabase: true))
..registerSingleton<UpdateNotifications>(mockUpdateNotifications)
..registerSingleton<UserActivityService>(UserActivityService())
..registerSingleton<JournalDb>(JournalDb(inMemoryDatabase: true))
..registerSingleton<SettingsDb>(SettingsDb(inMemoryDatabase: true))
..registerSingleton<SecureStorage>(secureStorageMock);

when(() => mockSettingsDb.itemByKey(any())).thenAnswer((_) async => null);
when(() => mockSettingsDb.saveSettingsItem(any(), any()))
.thenAnswer((_) async => 0);
await Future<void>.delayed(const Duration(seconds: 5));
});

setUp(() {});
Expand All @@ -146,6 +140,8 @@ void main() {
dbName: 'Alice',
deviceDisplayName: 'Alice',
overriddenJournalDb: aliceDb,
overriddenLoggingDb: LoggingDb(inMemoryDatabase: true),
overriddenSettingsDb: SettingsDb(inMemoryDatabase: true),
);

await alice.login();
Expand All @@ -171,6 +167,8 @@ void main() {
dbName: 'Bob',
deviceDisplayName: 'Bob',
overriddenJournalDb: bobDb,
overriddenLoggingDb: LoggingDb(inMemoryDatabase: true),
overriddenSettingsDb: SettingsDb(inMemoryDatabase: true),
);

await bob.login();
Expand Down
12 changes: 8 additions & 4 deletions lib/features/sync/matrix/last_read.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import 'package:lotti/database/settings_db.dart';
import 'package:lotti/features/sync/matrix.dart';
import 'package:lotti/get_it.dart';

Future<void> setLastReadMatrixEventId(String eventId) =>
getIt<SettingsDb>().saveSettingsItem(
Future<void> setLastReadMatrixEventId(
String eventId,
SettingsDb? overriddenSettingsDb,
) =>
(overriddenSettingsDb ?? getIt<SettingsDb>()).saveSettingsItem(
lastReadMatrixEventId,
eventId,
);

Future<String?> getLastReadMatrixEventId() =>
getIt<SettingsDb>().itemByKey(lastReadMatrixEventId);
Future<String?> getLastReadMatrixEventId(SettingsDb? overriddenSettingsDb) =>
(overriddenSettingsDb ?? getIt<SettingsDb>())
.itemByKey(lastReadMatrixEventId);
8 changes: 7 additions & 1 deletion lib/features/sync/matrix/matrix_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:async';

import 'package:lotti/classes/config.dart';
import 'package:lotti/database/database.dart';
import 'package:lotti/database/logging_db.dart';
import 'package:lotti/database/settings_db.dart';
import 'package:lotti/features/sync/client_runner.dart';
import 'package:lotti/features/sync/matrix.dart';
import 'package:lotti/features/user_activity/state/user_activity_service.dart';
Expand All @@ -15,6 +17,8 @@ class MatrixService {
this.deviceDisplayName,
String? dbName,
JournalDb? overriddenJournalDb,
LoggingDb? overriddenLoggingDb,
SettingsDb? overriddenSettingsDb,
}) : keyVerificationController =
StreamController<KeyVerificationRunner>.broadcast(),
_client = createMatrixClient(dbName: dbName) {
Expand All @@ -27,11 +31,13 @@ class MatrixService {
await processNewTimelineEvents(
service: this,
overriddenJournalDb: overriddenJournalDb,
overriddenLoggingDb: overriddenLoggingDb,
overriddenSettingsDb: overriddenSettingsDb,
);
},
);

getLastReadMatrixEventId().then(
getLastReadMatrixEventId(overriddenSettingsDb).then(
(value) => lastReadEventContextId = value,
);

Expand Down
7 changes: 5 additions & 2 deletions lib/features/sync/matrix/timeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:lotti/database/database.dart';
import 'package:lotti/database/logging_db.dart';
import 'package:lotti/database/settings_db.dart';
import 'package:lotti/features/sync/matrix.dart';
import 'package:lotti/get_it.dart';
import 'package:lotti/utils/list_extension.dart';
Expand Down Expand Up @@ -59,8 +60,10 @@ Future<void> listenToTimelineEvents({
Future<void> processNewTimelineEvents({
required MatrixService service,
JournalDb? overriddenJournalDb,
LoggingDb? overriddenLoggingDb,
SettingsDb? overriddenSettingsDb,
}) async {
final loggingDb = getIt<LoggingDb>();
final loggingDb = overriddenLoggingDb ?? getIt<LoggingDb>();

try {
final lastReadEventContextId = service.lastReadEventContextId;
Expand Down Expand Up @@ -111,7 +114,7 @@ Future<void> processNewTimelineEvents({
try {
if (eventId.startsWith(r'$')) {
service.lastReadEventContextId = eventId;
await setLastReadMatrixEventId(eventId);
await setLastReadMatrixEventId(eventId, overriddenSettingsDb);
final loginState = service.client.onLoginStateChanged.value;
if (loginState == LoginState.loggedIn) {
await timeline.setReadMarker(eventId: eventId);
Expand Down

0 comments on commit 8bf4889

Please sign in to comment.