Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7a9ea7d
setup sentry hive library
denrase Oct 10, 2023
2fa0411
Add SentryBox with first test
denrase Oct 10, 2023
071dd1e
test added spans
denrase Oct 10, 2023
bee44de
Merge branch 'main' into feat/hive
denrase Oct 16, 2023
5881d84
Add sentry hive impl
denrase Oct 16, 2023
296e25e
introduce base box to avoid duplication
denrase Oct 16, 2023
6ea6914
implement lazy box test
denrase Oct 17, 2023
e35a82d
expose sentry hive and updata documentation
denrase Oct 17, 2023
22bed30
add hive to flutter example app
denrase Oct 17, 2023
e5c2b9b
test throwing in base_box, cleanup
denrase Oct 23, 2023
7a41832
test lazy box error cases
denrase Oct 23, 2023
ee12365
test sentry hive impl failures
denrase Oct 23, 2023
f72c0a8
run fix & format
denrase Oct 23, 2023
8fa3182
fix test failure race condition
denrase Oct 23, 2023
08cfc16
add hive workflow, update other workflows
denrase Oct 23, 2023
69d6404
Merge branch 'main' into feat/hive
denrase Oct 23, 2023
f5dd006
add changelog entry
denrase Oct 23, 2023
9237bee
remove path+provider from dev_dependencies
denrase Oct 23, 2023
887adb0
add coverage dev dependency
denrase Oct 23, 2023
ea89cf6
fix deprecation warnings
denrase Oct 23, 2023
8c7e7cc
Merge branch 'main' into feat/hive
denrase Oct 30, 2023
5f7c196
fix changelog
denrase Oct 30, 2023
2ca2783
move span wrapping on own class to avoid duplication
denrase Oct 30, 2023
380c8a2
add sentry box collection
denrase Oct 30, 2023
71d3f8e
test added spans, fix imports
denrase Oct 31, 2023
d286b62
test throwing code
denrase Oct 31, 2023
1f9c37f
Merge branch 'main' into feat/hive
denrase Oct 31, 2023
9e02a29
fix changelog
denrase Oct 31, 2023
e888f6f
Merge branch 'main' into feat/hive
denrase Nov 6, 2023
22a1637
change dbSystem value
denrase Nov 6, 2023
d959c80
Merge branch 'main' into feat/hive
denrase Nov 6, 2023
158f93f
change db system
denrase Nov 6, 2023
aeed961
Merge branch 'main' into feat/hive
denrase Nov 13, 2023
f99d255
remove unneccessary async & await
denrase Nov 13, 2023
da8309f
Merge branch 'main' into feat/hive
buenaflor Nov 15, 2023
7119d74
Fix analyze
buenaflor Nov 15, 2023
34ea841
Formatting
buenaflor Nov 15, 2023
2db8de9
Add changelog symlink
buenaflor Nov 15, 2023
fccc34c
Ignore hive coverage
buenaflor Nov 15, 2023
19ebd4b
Create symlink for dartdoc_options
buenaflor Nov 15, 2023
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
Prev Previous commit
Next Next commit
implement lazy box test
  • Loading branch information
denrase committed Oct 17, 2023
commit 6ea69148481ff6b574a1b394fdf812e1a101c4b0
3 changes: 2 additions & 1 deletion dart/lib/src/sentry_trace_origins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ class SentryTraceOrigins {

static const autoDbHive = 'auto.db.hive';
static const autoDbHiveOpenDatabase = 'auto.db.hive.open_database';
static const autoDbHiveBaseBox = 'auto.db.hive.base_box';
static const autoDbHiveBoxBase = 'auto.db.hive.box_base';
static const autoDbHiveLazyBox = 'auto.db.hive.lazy_box';
}
1 change: 0 additions & 1 deletion hive/lib/src/sentry_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class SentryBox<E> extends SentryBoxBase<E> implements Box<E> {
}

@override
// TODO: implement values
Iterable<E> get values => _box.values;

@override
Expand Down
3 changes: 2 additions & 1 deletion hive/lib/src/sentry_box_base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import 'package:hive/hive.dart';
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'sentry_hive_impl.dart';

Expand Down Expand Up @@ -152,7 +153,7 @@ class SentryBoxBase<E> implements BoxBase<E> {
);

// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbHiveBaseBox;
span?.origin = SentryTraceOrigins.autoDbHiveBoxBase;

span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem);
span?.setData(SentryHiveImpl.dbNameKey, name);
Expand Down
42 changes: 39 additions & 3 deletions hive/lib/src/sentry_lazy_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,60 @@
import 'package:hive/hive.dart';
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'sentry_hive_impl.dart';

import 'sentry_box_base.dart';

///
class SentryLazyBox<E> extends SentryBoxBase<E> implements LazyBox<E> {

final LazyBox<E> _lazyBox;
final Hub _hub;

///
SentryLazyBox(this._lazyBox, @internal Hub hub) : super(_lazyBox, hub);
SentryLazyBox(this._lazyBox, @internal this._hub) : super(_lazyBox, _hub);

@override
Future<E?> get(key, {E? defaultValue}) {
return _lazyBox.get(key, defaultValue: defaultValue);
return _asyncWrapInSpan('get', () async {
return await _lazyBox.get(key, defaultValue: defaultValue);
});
}

@override
Future<E?> getAt(int index) {
return _lazyBox.getAt(index);
return _asyncWrapInSpan('getAt', () async {
return _lazyBox.getAt(index);
});
}

// Helper

Future<T> _asyncWrapInSpan<T>(String description, Future<T> Function() execute) async {
final currentSpan = _hub.getSpan();
final span = currentSpan?.startChild(
SentryHiveImpl.dbOp,
description: description,
);

// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbHiveLazyBox;

span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem);
span?.setData(SentryHiveImpl.dbNameKey, name);

try {
final result = await execute();
span?.status = SpanStatus.ok();

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();

rethrow;
} finally {
await span?.finish();
}
}
}
3 changes: 0 additions & 3 deletions hive/test/common.dart

This file was deleted.

24 changes: 24 additions & 0 deletions hive/test/person.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:hive/hive.dart';

@HiveType(typeId: 0)
class Person extends HiveObject {
@HiveField(0)
final String name;

Person(this.name);
}

class PersonAdapter extends TypeAdapter<Person> {
@override
final typeId = 0;

@override
Person read(BinaryReader reader) {
return Person(reader.read() as String);
}

@override
void write(BinaryWriter writer, Person obj) {
writer.write(obj.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import 'dart:io';
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_hive/sentry_hive.dart';
import 'package:sentry_hive/src/sentry_box_base.dart';
import 'package:sentry_hive/src/sentry_hive_impl.dart';
import 'package:test/test.dart';
import 'package:sentry/src/sentry_tracer.dart';

import 'mocks/mocks.mocks.dart';
import 'person.dart';

void main() {

Expand All @@ -19,7 +20,7 @@ void main() {
expect(span?.context.description, description);
expect(span?.status, SpanStatus.ok());
// ignore: invalid_use_of_internal_member
expect(span?.origin, SentryTraceOrigins.autoDbHiveBaseBox);
expect(span?.origin, SentryTraceOrigins.autoDbHiveBoxBase);
expect(span?.data[SentryHiveImpl.dbSystemKey], SentryHiveImpl.dbSystem);
expect(span?.data[SentryHiveImpl.dbNameKey], Fixture.dbName);
}
Expand Down Expand Up @@ -107,28 +108,7 @@ void main() {
});
}

@HiveType(typeId: 0)
class Person extends HiveObject {
@HiveField(0)
final String name;

Person(this.name);
}

class PersonAdapter extends TypeAdapter<Person> {
@override
final typeId = 0;

@override
Person read(BinaryReader reader) {
return Person(reader.readString());
}

@override
void write(BinaryWriter writer, Person obj) {
writer.write(obj.name);
}
}

class Fixture {
late final Box<Person> box;
Expand All @@ -141,8 +121,9 @@ class Fixture {

Future<void> setUp() async {
Hive.init(Directory.systemTemp.path);
Hive.registerAdapter(PersonAdapter());

if (!Hive.isAdapterRegistered(0)) {
Hive.registerAdapter(PersonAdapter());
}
box = await Hive.openBox(dbName);
}

Expand All @@ -153,8 +134,8 @@ class Fixture {
}
}

Future<SentryBox<Person>> getSut() async {
return SentryBox(box, hub);
Future<SentryBoxBase<Person>> getSut() async {
return SentryBoxBase(box, hub);
}

SentrySpan? getCreatedSpan() {
Expand Down
94 changes: 94 additions & 0 deletions hive/test/sentry_lazy_box_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@TestOn('vm')

import 'dart:io';

import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/sentry_tracer.dart';
import 'package:sentry_hive/src/sentry_hive_impl.dart';
import 'package:sentry_hive/src/sentry_lazy_box.dart';
import 'package:test/test.dart';

import 'mocks/mocks.mocks.dart';
import 'person.dart';

void main() {
void verifySpan(String description, SentrySpan? span) {
expect(span?.context.operation, SentryHiveImpl.dbOp);
expect(span?.context.description, description);
expect(span?.status, SpanStatus.ok());
// ignore: invalid_use_of_internal_member
expect(span?.origin, SentryTraceOrigins.autoDbHiveLazyBox);
expect(span?.data[SentryHiveImpl.dbSystemKey], SentryHiveImpl.dbSystem);
expect(span?.data[SentryHiveImpl.dbNameKey], Fixture.dbName);
}

group('adds span', () {

late Fixture fixture;

setUp(() async {
fixture = Fixture();
await fixture.setUp();

when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
});

tearDown(() async {
await fixture.tearDown();
});

test('get adds span', () async {
final sut = await fixture.getSut();

await sut.put('fixture-key', Person('John Malkovich'));
await sut.get('fixture-key');

verifySpan('get', fixture.getCreatedSpan());
});

test('getAt adds span', () async {
final sut = await fixture.getSut();

await sut.add(Person('John Malkovich'));
await sut.getAt(0);

verifySpan('getAt', fixture.getCreatedSpan());
});
});
}

class Fixture {
late final LazyBox<Person> box;
final options = SentryOptions();
final hub = MockHub();
static final dbName = 'people';

final _context = SentryTransactionContext('name', 'operation');
late final tracer = SentryTracer(_context, hub);

Future<void> setUp() async {
Hive.init(Directory.systemTemp.path);
if (!Hive.isAdapterRegistered(0)) {
Hive.registerAdapter(PersonAdapter());
}
box = await Hive.openLazyBox(dbName);
}

Future<void> tearDown() async {
if (box.isOpen) {
await box.deleteFromDisk();
await box.close();
}
}

Future<SentryLazyBox<Person>> getSut() async {
return SentryLazyBox(box, hub);
}

SentrySpan? getCreatedSpan() {
return tracer.children.last;
}
}