Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Sep 7, 2023
1 parent 4e0241f commit a58034f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 8 deletions.
3 changes: 3 additions & 0 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ class Hub {
) =>
_throwableToSpan.add(throwable, span, transaction);

@internal
ProfilerFactory? get profilerFactory => _profilerFactory;

@internal
set profilerFactory(ProfilerFactory? value) => _profilerFactory = value;

Expand Down
10 changes: 8 additions & 2 deletions dart/lib/src/hub_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ class HubAdapter implements Hub {
SentryId get lastEventId => Sentry.lastEventId;

@override
Future<SentryId> captureTransaction(SentryTransaction transaction,
{SentryTraceContextHeader? traceContext}) =>
Future<SentryId> captureTransaction(
SentryTransaction transaction, {
SentryTraceContextHeader? traceContext,
}) =>
Sentry.currentHub.captureTransaction(
transaction,
traceContext: traceContext,
Expand Down Expand Up @@ -172,6 +174,10 @@ class HubAdapter implements Hub {
set profilerFactory(ProfilerFactory? value) =>
Sentry.currentHub.profilerFactory = value;

@internal
@override
ProfilerFactory? get profilerFactory => Sentry.currentHub.profilerFactory;

@override
Scope get scope => Sentry.currentHub.scope;
}
4 changes: 4 additions & 0 deletions dart/lib/src/noop_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ class NoOpHub implements Hub {
@override
set profilerFactory(ProfilerFactory? value) {}

@internal
@override
ProfilerFactory? get profilerFactory => null;

@override
Scope get scope => Scope(_options);
}
5 changes: 4 additions & 1 deletion flutter/lib/src/profiling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class NativeProfilerFactory implements ProfilerFactory {
return;
}

if (Platform.isMacOS || Platform.isIOS) {
if (options.platformChecker.platform.isMacOS ||
options.platformChecker.platform.isIOS) {
// ignore: invalid_use_of_internal_member
hub.profilerFactory = NativeProfilerFactory(SentryNative());
}
Expand All @@ -53,6 +54,8 @@ class NativeProfilerFactory implements ProfilerFactory {
}
}

// 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;
Expand Down
34 changes: 29 additions & 5 deletions flutter/test/mocks.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import 'dart:async' as _i6;

import 'package:flutter/src/services/binary_messenger.dart' as _i5;
import 'package:flutter/src/services/message_codec.dart' as _i4;
import 'package:flutter/src/services/platform_channel.dart' as _i9;
import 'package:flutter/src/services/platform_channel.dart' as _i10;
import 'package:mockito/mockito.dart' as _i1;
import 'package:sentry/sentry.dart' as _i2;
import 'package:sentry/src/profiling.dart' as _i9;
import 'package:sentry/src/protocol.dart' as _i3;
import 'package:sentry/src/sentry_envelope.dart' as _i7;
import 'package:sentry/src/sentry_tracer.dart' as _i8;

import 'mocks.dart' as _i10;
import 'mocks.dart' as _i11;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
Expand Down Expand Up @@ -142,7 +143,6 @@ class MockTransport extends _i1.Mock implements _i2.Transport {
/// A class which mocks [SentryTracer].
///
/// See the documentation for Mockito's code generation for more information.
// ignore: invalid_use_of_internal_member
class MockSentryTracer extends _i1.Mock implements _i8.SentryTracer {
MockSentryTracer() {
_i1.throwOnMissingStub(this);
Expand Down Expand Up @@ -178,6 +178,22 @@ class MockSentryTracer extends _i1.Mock implements _i8.SentryTracer {
returnValueForMissingStub: null,
);
@override
set profiler(_i9.Profiler? _profiler) => super.noSuchMethod(
Invocation.setter(
#profiler,
_profiler,
),
returnValueForMissingStub: null,
);
@override
set profileInfo(_i9.ProfileInfo? _profileInfo) => super.noSuchMethod(
Invocation.setter(
#profileInfo,
_profileInfo,
),
returnValueForMissingStub: null,
);
@override
_i2.SentrySpanContext get context => (super.noSuchMethod(
Invocation.getter(#context),
returnValue: _FakeSentrySpanContext_0(
Expand Down Expand Up @@ -410,7 +426,7 @@ class MockSentryTracer extends _i1.Mock implements _i8.SentryTracer {
/// A class which mocks [MethodChannel].
///
/// See the documentation for Mockito's code generation for more information.
class MockMethodChannel extends _i1.Mock implements _i9.MethodChannel {
class MockMethodChannel extends _i1.Mock implements _i10.MethodChannel {
MockMethodChannel() {
_i1.throwOnMissingStub(this);
}
Expand Down Expand Up @@ -523,6 +539,14 @@ class MockHub extends _i1.Mock implements _i2.Hub {
),
) as _i3.SentryId);
@override
set profilerFactory(_i9.ProfilerFactory? value) => super.noSuchMethod(
Invocation.setter(
#profilerFactory,
value,
),
returnValueForMissingStub: null,
);
@override
_i6.Future<_i3.SentryId> captureEvent(
_i3.SentryEvent? event, {
dynamic stackTrace,
Expand Down Expand Up @@ -710,7 +734,7 @@ class MockHub extends _i1.Mock implements _i2.Hub {
#customSamplingContext: customSamplingContext,
},
),
returnValue: _i10.startTransactionShim(
returnValue: _i11.startTransactionShim(
name,
operation,
description: description,
Expand Down
46 changes: 46 additions & 0 deletions flutter/test/profiling_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@TestOn('vm')

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/profiling.dart';
import 'package:sentry_flutter/src/sentry_native.dart';
import 'package:sentry_flutter/src/sentry_native_channel.dart';
import 'mocks.dart';
import 'mocks.mocks.dart';
import 'sentry_flutter_test.dart';

void main() {
group('$NativeProfilerFactory', () {
Hub hubWithSampleRate(double profilesSampleRate) {
final o = SentryFlutterOptions(dsn: fakeDsn);
o.platformChecker = getPlatformChecker(platform: MockPlatform.iOs());
o.profilesSampleRate = profilesSampleRate;

final hub = MockHub();
when(hub.options).thenAnswer((_) => o);
return hub;
}

test('attachTo() respects sampling rate', () async {
var hub = hubWithSampleRate(0.0);
NativeProfilerFactory.attachTo(hub);
verifyNever(hub.profilerFactory = any);

hub = hubWithSampleRate(0.1);
NativeProfilerFactory.attachTo(hub);
verify(hub.profilerFactory = any);
});

test('creates a profiler', () async {
final nativeMock = TestMockSentryNative();
final sut = NativeProfilerFactory(nativeMock);
final profiler = sut.startProfiling(SentryTransactionContext(
'name',
'op',
));
expect(nativeMock.numberOfStartProfilingCalls, 1);
expect(profiler, isNotNull);
});
});
}
17 changes: 17 additions & 0 deletions flutter/test/sentry_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/integrations/integrations.dart';
import 'package:sentry_flutter/src/integrations/screenshot_integration.dart';
import 'package:sentry_flutter/src/profiling.dart';
import 'package:sentry_flutter/src/renderer/renderer.dart';
import 'package:sentry_flutter/src/sentry_native.dart';
import 'package:sentry_flutter/src/version.dart';
Expand Down Expand Up @@ -65,6 +66,7 @@ void main() {
(options) async {
options.dsn = fakeDsn;
options.devMode = true;
options.profilesSampleRate = 1.0;
integrations = options.integrations;
transport = options.transport;
sentryFlutterOptions = options;
Expand Down Expand Up @@ -100,6 +102,7 @@ void main() {
afterIntegration: OnErrorIntegration);

expect(SentryNative().nativeChannel, isNotNull);
expect(Sentry.currentHub.profilerFactory, isNull);

await Sentry.close();
}, testOn: 'vm');
Expand All @@ -113,6 +116,7 @@ void main() {
(options) async {
options.dsn = fakeDsn;
options.devMode = true;
options.profilesSampleRate = 1.0;
integrations = options.integrations;
transport = options.transport;
sentryFlutterOptions = options;
Expand Down Expand Up @@ -146,6 +150,8 @@ void main() {
afterIntegration: OnErrorIntegration);

expect(SentryNative().nativeChannel, isNotNull);
expect(Sentry.currentHub.profilerFactory,
isInstanceOf<NativeProfilerFactory>());

await Sentry.close();
}, testOn: 'vm');
Expand All @@ -159,6 +165,7 @@ void main() {
(options) async {
options.dsn = fakeDsn;
options.devMode = true;
options.profilesSampleRate = 1.0;
integrations = options.integrations;
transport = options.transport;
sentryFlutterOptions = options;
Expand Down Expand Up @@ -192,6 +199,8 @@ void main() {
afterIntegration: OnErrorIntegration);

expect(SentryNative().nativeChannel, isNotNull);
expect(Sentry.currentHub.profilerFactory,
isInstanceOf<NativeProfilerFactory>());

await Sentry.close();
}, testOn: 'vm');
Expand All @@ -205,6 +214,7 @@ void main() {
(options) async {
options.dsn = fakeDsn;
options.devMode = true;
options.profilesSampleRate = 1.0;
integrations = options.integrations;
transport = options.transport;
sentryFlutterOptions = options;
Expand Down Expand Up @@ -241,6 +251,7 @@ void main() {
afterIntegration: OnErrorIntegration);

expect(SentryNative().nativeChannel, isNull);
expect(Sentry.currentHub.profilerFactory, isNull);

await Sentry.close();
}, testOn: 'vm');
Expand All @@ -254,6 +265,7 @@ void main() {
(options) async {
options.dsn = fakeDsn;
options.devMode = true;
options.profilesSampleRate = 1.0;
integrations = options.integrations;
transport = options.transport;
sentryFlutterOptions = options;
Expand Down Expand Up @@ -290,6 +302,7 @@ void main() {
afterIntegration: OnErrorIntegration);

expect(SentryNative().nativeChannel, isNull);
expect(Sentry.currentHub.profilerFactory, isNull);

await Sentry.close();
}, testOn: 'vm');
Expand All @@ -303,6 +316,7 @@ void main() {
(options) async {
options.dsn = fakeDsn;
options.devMode = true;
options.profilesSampleRate = 1.0;
integrations = options.integrations;
transport = options.transport;
sentryFlutterOptions = options;
Expand Down Expand Up @@ -340,6 +354,7 @@ void main() {
afterIntegration: WidgetsFlutterBindingIntegration);

expect(SentryNative().nativeChannel, isNull);
expect(Sentry.currentHub.profilerFactory, isNull);

await Sentry.close();
});
Expand Down Expand Up @@ -429,6 +444,8 @@ void main() {
beforeIntegration: RunZonedGuardedIntegration,
afterIntegration: WidgetsFlutterBindingIntegration);

expect(Sentry.currentHub.profilerFactory, isNull);

await Sentry.close();
});

Expand Down

0 comments on commit a58034f

Please sign in to comment.