Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Nov 18, 2024
1 parent 3df500d commit 21acda1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 60 deletions.
34 changes: 21 additions & 13 deletions flutter/example/integration_test/app_start_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
Expand All @@ -8,13 +9,15 @@ import 'package:sentry_flutter/src/frame_callback_handler.dart';
import 'package:sentry_flutter/src/integrations/native_app_start_handler.dart';
import 'package:sentry_flutter/src/integrations/native_app_start_integration.dart';

import '../../../dart/test/mocks/mock_transport.dart';

void main() async {
TestWidgetsFlutterBinding.ensureInitialized();

late _IntegrationFrameCallbackHandler frameCallbackHandler;
late SentryTransaction transaction;
final transport = MockTransport();

group('App start measurements', () {
group('App start measurement', () {
setUp(() async {
frameCallbackHandler = _IntegrationFrameCallbackHandler();

Expand All @@ -24,11 +27,7 @@ void main() async {
options.dsn = 'https://abc@def.ingest.sentry.io/1234567';
options.debug = true;
options.tracesSampleRate = 1.0;

options.beforeSendTransaction = (tx) {
transaction = tx;
return tx;
};
options.transport = transport;

final appStartIntegration = options.integrations.firstWhere(
(integration) => integration is NativeAppStartIntegration);
Expand All @@ -44,8 +43,7 @@ void main() async {
await Sentry.close();
});

testWidgets('app start measurements are processed and reported',
(WidgetTester tester) async {
testWidgets('is captured', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
Expand All @@ -58,11 +56,21 @@ void main() async {

await tester.pumpAndSettle();

expect(transaction.measurements, isNotEmpty);
expect(transaction.measurements['time_to_initial_display'], isNotNull);
expect(transaction.measurements['app_start_cold'], isNotNull);
final envelope = transport.envelopes.first;
expect(envelope.items[0].header.type, "transaction");
expect(await envelope.items[0].header.length(), greaterThan(0));

final txJson = utf8.decode(await envelope.items[0].dataFactory());
final txData = json.decode(txJson) as Map<String, dynamic>;

expect(txData["measurements"]["time_to_initial_display"]["value"],
isNotNull);
expect(txData["measurements"]["app_start_cold"]["value"], isNotNull);
});
}, skip: Platform.isMacOS);
},
skip: Platform.isMacOS
? 'App start measurement is not supported on this platform'
: false);
}

class _IntegrationFrameCallbackHandler implements FrameCallbackHandler {
Expand Down
97 changes: 50 additions & 47 deletions flutter/example/integration_test/profiling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,67 @@ import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

import '../../../dart/test/mocks/mock_transport.dart';

void main() {
final transport = MockTransport();

setUp(() async {
await SentryFlutter.init((options) {
// ignore: invalid_use_of_internal_member
options.automatedTestMode = true;
options.dsn = 'https://abc@def.ingest.sentry.io/1234567';
options.debug = true;
options.transport = transport;
options.tracesSampleRate = 1.0;
options.profilesSampleRate = 1.0;
group('Profiling', () {
setUp(() async {
await SentryFlutter.init((options) {
// ignore: invalid_use_of_internal_member
options.automatedTestMode = true;
options.dsn = 'https://abc@def.ingest.sentry.io/1234567';
options.debug = true;
options.transport = transport;
options.tracesSampleRate = 1.0;
options.profilesSampleRate = 1.0;
});
});
});

tearDown(() async {
await Sentry.close();
transport.reset();
});
tearDown(() async {
await Sentry.close();
transport.reset();
});

test('native binding is initialized', () async {
// ignore: invalid_use_of_internal_member
expect(SentryFlutter.native, isNotNull);
});
test('native binding is initialized', () async {
// ignore: invalid_use_of_internal_member
expect(SentryFlutter.native, isNotNull);
});

test('profile is captured', () async {
final tx = Sentry.startTransaction("name", "op");
await Future.delayed(const Duration(milliseconds: 1000));
await tx.finish();
expect(transport.calls, 1);
test('profile is captured', () async {
final tx = Sentry.startTransaction("name", "op");
await Future.delayed(const Duration(milliseconds: 1000));
await tx.finish();
expect(transport.calls, 1);

final envelope = transport.envelopes.first;
expect(envelope.items.length, 2);
expect(envelope.items[0].header.type, "transaction");
expect(await envelope.items[0].header.length(), greaterThan(0));
expect(envelope.items[1].header.type, "profile");
expect(await envelope.items[1].header.length(), greaterThan(0));
final envelope = transport.envelopes.first;
expect(envelope.items.length, 2);
expect(envelope.items[0].header.type, "transaction");
expect(await envelope.items[0].header.length(), greaterThan(0));
expect(envelope.items[1].header.type, "profile");
expect(await envelope.items[1].header.length(), greaterThan(0));

final txJson = utf8.decode(await envelope.items[0].dataFactory());
final txData = json.decode(txJson) as Map<String, dynamic>;
final txJson = utf8.decode(await envelope.items[0].dataFactory());
final txData = json.decode(txJson) as Map<String, dynamic>;

final profileJson = utf8.decode(await envelope.items[1].dataFactory());
final profileData = json.decode(profileJson) as Map<String, dynamic>;
final profileJson = utf8.decode(await envelope.items[1].dataFactory());
final profileData = json.decode(profileJson) as Map<String, dynamic>;

expect(txData["event_id"], isNotNull);
expect(txData["event_id"], profileData["transaction"]["id"]);
expect(txData["contexts"]["trace"]["trace_id"], isNotNull);
expect(txData["contexts"]["trace"]["trace_id"],
profileData["transaction"]["trace_id"]);
expect(profileData["debug_meta"]["images"], isNotEmpty);
expect(profileData["profile"]["thread_metadata"], isNotEmpty);
expect(profileData["profile"]["samples"], isNotEmpty);
expect(profileData["profile"]["stacks"], isNotEmpty);
expect(profileData["profile"]["frames"], isNotEmpty);
},
skip: (Platform.isMacOS || Platform.isIOS)
? false
: "Profiling is not supported on this platform");
expect(txData["event_id"], isNotNull);
expect(txData["event_id"], profileData["transaction"]["id"]);
expect(txData["contexts"]["trace"]["trace_id"], isNotNull);
expect(txData["contexts"]["trace"]["trace_id"],
profileData["transaction"]["trace_id"]);
expect(profileData["debug_meta"]["images"], isNotEmpty);
expect(profileData["profile"]["thread_metadata"], isNotEmpty);
expect(profileData["profile"]["samples"], isNotEmpty);
expect(profileData["profile"]["stacks"], isNotEmpty);
expect(profileData["profile"]["frames"], isNotEmpty);
},
skip: (Platform.isMacOS || Platform.isIOS)
? false
: "Profiling is not supported on this platform");
});
}

0 comments on commit 21acda1

Please sign in to comment.