Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite the last_ping key again #255

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkgs/unified_analytics/lib/src/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class Initializer {
}) {
final now = sessionIdOverride ?? clock.now();
sessionFile.createSync(recursive: true);
sessionFile
.writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}}');
sessionFile.writeAsStringSync(
'{"session_id": ${now.millisecondsSinceEpoch}, "last_ping": null}');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation for why I think this is necessary:

}

/// This will check that there is a client ID populated in
Expand Down
15 changes: 15 additions & 0 deletions pkgs/unified_analytics/lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ class Session {
description: err.osError?.toString(),
));

// Fallback to setting the session id as the current time
_sessionId = now.millisecondsSinceEpoch;
// ignore: avoid_catching_errors
} on TypeError catch (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not catch TypeError, and instead be disciplined about never deleting fields from this JSON file.

final now = clock.now();
Initializer.createSessionFile(
sessionFile: sessionFile,
sessionIdOverride: now,
);

_errorHandler.log(Event.analyticsException(
workflow: 'Session._refreshSessionData',
error: err.runtimeType.toString(),
));

// Fallback to setting the session id as the current time
_sessionId = now.millisecondsSinceEpoch;
}
Expand Down
28 changes: 28 additions & 0 deletions pkgs/unified_analytics/test/error_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,34 @@ void main() {
expect(sessionFile.readAsStringSync(), isNotEmpty);
});

test('only sends one event for TypeError', () {
// Rewriting the session JSON file with valid JSON that does not contain
// the required keys will result in a TypeError
sessionFile.writeAsStringSync('{"wrong-key": 123}');
analytics.send(testEvent);

expect(
analytics.sentEvents.where(
(element) => element.eventName == DashEvent.analyticsException),
hasLength(1),
);
expect(sessionFile.existsSync(), isTrue);
expect(sessionFile.readAsStringSync(), isNotEmpty);

// Write the wrong json string again
sessionFile.writeAsStringSync('{"not-correct-key": 123}');
analytics.send(testEvent);

expect(
analytics.sentEvents.where(
(element) => element.eventName == DashEvent.analyticsException),
hasLength(1),
reason: 'Only the first error event should exist',
);
expect(sessionFile.existsSync(), isTrue);
expect(sessionFile.readAsStringSync(), isNotEmpty);
});

test('sends two unique errors', () {
// Begin with the session file empty, it should recreate the file
// and send an error event
Expand Down
6 changes: 4 additions & 2 deletions pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void main() {
final timestamp = clock.now().millisecondsSinceEpoch.toString();
expect(sessionFile.readAsStringSync(), 'contents');
analytics.userProperty.preparePayload();
expect(sessionFile.readAsStringSync(), '{"session_id": $timestamp}');
expect(sessionFile.readAsStringSync(),
'{"session_id": $timestamp, "last_ping": null}');

// Attempting to fetch the session id when malformed should also
// send an error event while parsing
Expand Down Expand Up @@ -199,7 +200,8 @@ void main() {
final timestamp = clock.now().millisecondsSinceEpoch.toString();
expect(sessionFile.existsSync(), false);
analytics.userProperty.preparePayload();
expect(sessionFile.readAsStringSync(), '{"session_id": $timestamp}');
expect(sessionFile.readAsStringSync(),
'{"session_id": $timestamp, "last_ping": null}');

// Attempting to fetch the session id when malformed should also
// send an error event while parsing
Expand Down
Loading