Skip to content

Commit

Permalink
Merge branch 'feat/logging/cloudwatch' into feat-logging-isLoggable
Browse files Browse the repository at this point in the history
  • Loading branch information
khatruong2009 authored Oct 4, 2023
2 parents cba88e3 + 6640039 commit c01df68
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
if (!pluginConfig.enable) {
_timer?.stop();
}
Amplify.Hub.listen(HubChannel.Auth, (AuthHubEvent event) async {
if (event.type == AuthHubEventType.signedOut ||
event.type == AuthHubEventType.userDeleted ||
event.type == AuthHubEventType.sessionExpired) {
await _clearLogs();
}
});
}

/// An [AWSLoggerPlugin] to use only for testing.
Expand All @@ -110,7 +117,15 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
_logStore = logStore,
_remoteLoggingConstraintProvider = remoteLoggingConstraintProvider,
_logStreamProvider = logStreamProvider,
_client = client;
_client = client {
Amplify.Hub.listen(HubChannel.Auth, (AuthHubEvent event) async {
if (event.type == AuthHubEventType.signedOut ||
event.type == AuthHubEventType.userDeleted ||
event.type == AuthHubEventType.sessionExpired) {
await _clearLogs();
}
});
}

final CloudWatchPluginConfig _pluginConfig;
final CloudWatchLogsClient _client;
Expand Down Expand Up @@ -381,6 +396,10 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin

@override
String get runtimeTypeName => 'CloudWatchLoggerPlugin';

Future<void> _clearLogs() async {
await _logStore.clear();
}
}

extension on QueuedItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,93 @@ void main() {
);
});
});

group('clear logs on Hub events', () {
final hubEventController = StreamController<AuthHubEvent>.broadcast();
final events = [
AuthHubEvent.sessionExpired,
AuthHubEvent.signedOut,
AuthHubEvent.userDeleted,
];
Amplify.Hub.addChannel(HubChannel.Auth, hubEventController.stream);
setUp(() {
mockCloudWatchLogsClient = MockCloudWatchLogsClient();
mockQueuedItemStore = MockQueuedItemStore();
mockCloudWatchLogStreamProvider = MockCloudWatchLogStreamProvider();
plugin = CloudWatchLoggerPlugin.testPlugin(
client: mockCloudWatchLogsClient,
pluginConfig: pluginConfig,
logStore: mockQueuedItemStore,
logStreamProvider: mockCloudWatchLogStreamProvider,
);
});
tearDownAll(hubEventController.close);

test('clear logs on expected auth hub events', () async {
when(
() => mockQueuedItemStore.addItem(
any(),
any(),
enableQueueRotation: false,
),
).thenAnswer((_) async => {});

when(() => mockQueuedItemStore.isFull(pluginConfig.localStoreMaxSizeInMB))
.thenReturn(false);

plugin.enable();
for (final event in events) {
await expectLater(
plugin.handleLogEntry(errorLog),
completes,
);
hubEventController.add(event());
}
await Future<void>.delayed(Duration.zero);

verify(
() => mockQueuedItemStore.addItem(
any(),
any(),
enableQueueRotation: false,
),
).called(events.length);

verify(
() => mockQueuedItemStore.clear(),
).called(events.length);
});

test('does not clear logs on unexpected hub events', () async {
when(
() => mockQueuedItemStore.addItem(
any(),
any(),
enableQueueRotation: false,
),
).thenAnswer((_) async => {});

when(() => mockQueuedItemStore.isFull(pluginConfig.localStoreMaxSizeInMB))
.thenReturn(false);

plugin.enable();
await expectLater(
plugin.handleLogEntry(errorLog),
completes,
);
hubEventController.add(AuthHubEvent.signedIn(MockAuhtUser()));
await Future<void>.delayed(Duration.zero);

verify(
() => mockQueuedItemStore.addItem(
any(),
any(),
enableQueueRotation: false,
),
).called(1);
verifyNever(
() => mockQueuedItemStore.clear(),
);
});
});
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:amplify_core/amplify_core.dart';
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart'
show CloudWatchLogStreamProvider, RemoteLoggingConstraintProvider;
import 'package:aws_logging_cloudwatch/src/queued_item_store/queued_item_store.dart';
Expand All @@ -16,3 +17,5 @@ class MockCloudWatchLogStreamProvider extends Mock

class MockRemoteLoggingConstraintProvider extends Mock
implements RemoteLoggingConstraintProvider {}

class MockAuhtUser extends Mock implements AuthUser {}

0 comments on commit c01df68

Please sign in to comment.