From 4c8f2bf1f56e1549802b93c646066cb4d340e820 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Mon, 18 Sep 2023 17:03:05 +0200 Subject: [PATCH] test: profiler integration test --- .../integration_test/profiling_test.dart | 64 +++++++++++++++++++ flutter/example/pubspec.yaml | 1 + flutter/lib/src/profiling.dart | 3 - 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 flutter/example/integration_test/profiling_test.dart diff --git a/flutter/example/integration_test/profiling_test.dart b/flutter/example/integration_test/profiling_test.dart new file mode 100644 index 0000000000..daa15861dc --- /dev/null +++ b/flutter/example/integration_test/profiling_test.dart @@ -0,0 +1,64 @@ +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; +import '../../../dart/test/mocks/mock_transport.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + final transport = MockTransport(); + + setUp(() async { + await SentryFlutter.init((options) { + // ignore: invalid_use_of_internal_member + options.devMode = 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(); + }); + + 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); + + 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; + + final profileJson = utf8.decode(await envelope.items[1].dataFactory()); + final profileData = json.decode(profileJson) as Map; + + 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); + }); +} diff --git a/flutter/example/pubspec.yaml b/flutter/example/pubspec.yaml index c4596f70b8..cf63be629c 100644 --- a/flutter/example/pubspec.yaml +++ b/flutter/example/pubspec.yaml @@ -36,6 +36,7 @@ dev_dependencies: sdk: flutter flutter_test: sdk: flutter + test: ^1.21.1 flutter: uses-material-design: true diff --git a/flutter/lib/src/profiling.dart b/flutter/lib/src/profiling.dart index da7b09b005..8a0d3f7def 100644 --- a/flutter/lib/src/profiling.dart +++ b/flutter/lib/src/profiling.dart @@ -50,9 +50,6 @@ class NativeProfilerFactory implements ProfilerFactory { return NativeProfiler(_native, startTime, context.traceId, _clock); } } - -// TODO this may move to the native code in the future - instead of unit-testing, -// do an integration test once https://github.com/getsentry/sentry-dart/issues/1605 is done. // ignore: invalid_use_of_internal_member class NativeProfiler implements Profiler { final SentryNative _native;