Skip to content

Commit

Permalink
Use DirectEventSubmitter by default in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Mar 25, 2024
1 parent 2782f14 commit c933705
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/src/analytics/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import 'package:wiredash/src/core/version.dart';
import 'package:wiredash/src/core/wiredash_widget.dart';

// Required
// TODO send events every 30 seconds to the server (or 5min?)
// TODO drop event if server responds 400 (code 2200) MISSING TEST
// TODO keep events if server responds 400 (code 2201) MISSING TEST
// TODO keep events for any other server error MISSING TEST
// TODO ignore corrupt events on disk (users might edit it on web)
// TODO Write documentation
// TODO export analytics
// TODO Write documentation

// Important
// TODO send events to server on app close
// TODO send events every 30 seconds to the server (or 5min?)
// TODO drop event when API credentials are obviously wrong
// TODO allow resetting of the analyticsId
// TODO send events directly on web
Expand Down
8 changes: 4 additions & 4 deletions lib/src/analytics/event_submitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import 'package:wiredash/src/core/network/send_events_request.dart';
///
/// Implementations are
/// - [DirectEventSubmitter] for immediate submission of events (usually web)
/// - [PendingEventSubmitter] for batching events within a certain time frame (usually mobile)
/// - [DebounceEventSubmitter] for batching events within a certain time frame (usually mobile)
abstract class EventSubmitter {
/// Submits all pending events in [SharedPreferences] to the backend
Future<void> submitEvents();
}

class DirectEventSubmitter extends PendingEventSubmitter {
class DirectEventSubmitter extends DebounceEventSubmitter {
DirectEventSubmitter({
required super.eventStore,
required super.api,
required super.projectId,
}) : super(throttleDuration: Duration.zero);
}

class PendingEventSubmitter implements EventSubmitter {
class DebounceEventSubmitter implements EventSubmitter {
final AnalyticsEventStore eventStore;
final WiredashApi api;
final String Function() projectId;

final Duration throttleDuration;

PendingEventSubmitter({
DebounceEventSubmitter({
required this.eventStore,
required this.api,
required this.projectId,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/services/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void registerProdWiredashServices(WiredashServices sl) {
);
}

return PendingEventSubmitter(
return DebounceEventSubmitter(
api: sl.api,
eventStore: sl.eventStore,
projectId: () => sl.wiredashWidget.projectId,
Expand Down
42 changes: 34 additions & 8 deletions test/util/robot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:wiredash/src/_feedback.dart';
import 'package:wiredash/src/_ps.dart';
import 'package:wiredash/src/_wiredash_internal.dart';
import 'package:wiredash/src/_wiredash_ui.dart';
import 'package:wiredash/src/analytics/event_submitter.dart';
import 'package:wiredash/src/core/wiredash_widget.dart';

// ignore: unused_import
Expand Down Expand Up @@ -146,11 +147,13 @@ class WiredashTestRobot {
FutureOr<void> Function()? afterPump,
List<LocalizationsDelegate> appLocalizationsDelegates = const [],
bool useDirectFeedbackSubmitter = true,
bool useDirectEventSubmitter = true,
bool wrapWithWiredash = true,
}) async {
setupMocks();
WiredashServices.debugServicesCreator = () => createMockServices(
useDirectFeedbackSubmitter: useDirectFeedbackSubmitter,
useDirectEventSubmitter: useDirectEventSubmitter,
);
addTearDown(() => WiredashServices.debugServicesCreator = null);

Expand Down Expand Up @@ -634,27 +637,50 @@ class WiredashMockServices {
MockWiredashApi get mockApi => services.api as MockWiredashApi;
}

WiredashServices createMockServices({bool useDirectFeedbackSubmitter = false}) {
WiredashServices createMockServices({
bool useDirectFeedbackSubmitter = false,
bool useDirectEventSubmitter = false,
}) {
return WiredashServices.setup((services) {
registerProdWiredashServices(services);

// Don't do actual http calls
services.inject<WiredashApi>((_) {
// depend on the widget (secret/project)
services.wiredashWidget;
return MockWiredashApi.fake();
});
services.inject<WiredashApi>(
(_) {
// depend on the widget (secret/project)
services.wiredashWidget;
return MockWiredashApi.fake();
},
);

// Let the widget behave as in production
services.inject<TestDetector>((_) => _OverlookFakeAsync());

if (useDirectFeedbackSubmitter) {
// replace submitter, because for testing we always want to submit directly
services.inject<FeedbackSubmitter>(
(locator) => DirectFeedbackSubmitter(services.api),
(_) => DirectFeedbackSubmitter(services.api),
);
} else {
assert(
services.feedbackSubmitter.runtimeType == RetryingFeedbackSubmitter,
);
}

if (useDirectEventSubmitter) {
services.inject<EventSubmitter>(
(_) {
print('create direct submitter');
return DirectEventSubmitter(
projectId: () => services.wiredashWidget.projectId,
eventStore: services.eventStore,
api: services.api,
);
},
dispose: (submitter) => print('dispose ${submitter.instanceName}'),
);
} else {
assert(services.feedbackSubmitter is RetryingFeedbackSubmitter);
assert(services.eventSubmitter.runtimeType == DebounceEventSubmitter);
}
});
}
Expand Down

0 comments on commit c933705

Please sign in to comment.