-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1874 from nextcloud/refactor/neon_framework/maint…
…enance-mode-bloc
- Loading branch information
Showing
4 changed files
with
155 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/neon_framework/lib/src/blocs/maintenance_mode.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:logging/logging.dart'; | ||
import 'package:neon_framework/blocs.dart'; | ||
import 'package:neon_framework/models.dart'; | ||
import 'package:nextcloud/core.dart'; | ||
|
||
/// A Bloc checking if the server is in maintenance mode. | ||
sealed class MaintenanceModeBloc implements InteractiveBloc { | ||
factory MaintenanceModeBloc({ | ||
required Account account, | ||
}) = _MaintenanceModeBloc; | ||
|
||
/// Emits an event if the server is in maintenance mode. | ||
Stream<void> get onMaintenanceMode; | ||
} | ||
|
||
class _MaintenanceModeBloc extends InteractiveBloc implements MaintenanceModeBloc { | ||
_MaintenanceModeBloc({ | ||
required this.account, | ||
}) { | ||
unawaited(refresh()); | ||
} | ||
|
||
final Account account; | ||
final controller = StreamController<void>(); | ||
|
||
@override | ||
final log = Logger('MaintenanceModeBloc'); | ||
|
||
@override | ||
void dispose() { | ||
unawaited(controller.close()); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
late final onMaintenanceMode = controller.stream; | ||
|
||
@override | ||
Future<void> refresh() async { | ||
try { | ||
final status = await account.client.core.getStatus(); | ||
|
||
if (status.body.maintenance) { | ||
controller.add(null); | ||
} | ||
} on http.ClientException catch (error, stackTrace) { | ||
log.warning( | ||
'Error getting the server status.', | ||
error, | ||
stackTrace, | ||
); | ||
|
||
addError(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/neon_framework/test/maintenance_mode_bloc_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:neon_framework/src/blocs/maintenance_mode.dart'; | ||
import 'package:neon_framework/src/models/account.dart'; | ||
import 'package:neon_framework/testing.dart'; | ||
|
||
Account mockMaintenanceModeAccount() { | ||
return mockServer({ | ||
RegExp(r'/status\.php'): { | ||
'get': (match, queryParameters) { | ||
return Response( | ||
json.encode({ | ||
'installed': false, | ||
'maintenance': true, | ||
'needsDbUpgrade': false, | ||
'version': '', | ||
'versionstring': '', | ||
'edition': '', | ||
'productname': '', | ||
'extendedSupport': false, | ||
}), | ||
200, | ||
); | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
void main() { | ||
late Account account; | ||
late MaintenanceModeBloc bloc; | ||
|
||
setUpAll(() { | ||
final storage = MockNeonStorage(); | ||
when(() => storage.requestCache).thenReturn(null); | ||
}); | ||
|
||
setUp(() { | ||
account = mockMaintenanceModeAccount(); | ||
bloc = MaintenanceModeBloc( | ||
account: account, | ||
); | ||
}); | ||
|
||
tearDown(() { | ||
bloc.dispose(); | ||
}); | ||
|
||
test('refresh', () async { | ||
expect(bloc.onMaintenanceMode, emits(null)); | ||
await Future<void>.delayed(const Duration(milliseconds: 1)); | ||
await bloc.refresh(); | ||
}); | ||
} |