Skip to content

Commit

Permalink
update deprecated member use in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeroberts-wk committed Jun 21, 2024
1 parent 2349754 commit dc0722c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export 'src/api/open_telemetry.dart'
trace,
traceContext,
traceSync,
traceSyncContext;
traceContextSync;
export 'src/api/propagation/extractors/text_map_getter.dart' show TextMapGetter;
export 'src/api/propagation/injectors/text_map_setter.dart' show TextMapSetter;
export 'src/api/propagation/text_map_propagator.dart' show TextMapPropagator;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/api/open_telemetry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import 'dart:async';

import 'package:meta/meta.dart';

import '../../api.dart' as api;
import 'propagation/noop_text_map_propagator.dart';
import 'trace/noop_tracer_provider.dart';

import '../../api.dart' as api;
import '../experimental_api.dart' show globalContextManager, ZoneContext;
import 'context/context_manager.dart';
import 'context/zone_context.dart';

final api.TracerProvider _noopTracerProvider = NoopTracerProvider();
final api.TextMapPropagator _noopTextMapPropagator = NoopTextMapPropagator();
Expand Down
28 changes: 14 additions & 14 deletions test/integration/open_telemetry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void main() {
sdk.SpanLimits());
late Span span;

api.traceSync('syncTrace', () {
span = api.Context.current.span as Span;
api.traceContextSync('syncTrace', (context) {
span = api.spanFromContext(context) as Span;
}, tracer: tracer);

expect(span, isNotNull);
Expand All @@ -44,8 +44,8 @@ void main() {
final spans = <Span>[];

for (var i = 0; i < 5; i++) {
api.traceSync('syncTrace', () {
spans.add(api.Context.current.span as Span);
api.traceContextSync('syncTrace', (context) {
spans.add(api.spanFromContext(context) as Span);
}, tracer: tracer);
}

Expand All @@ -67,8 +67,8 @@ void main() {
late Span span;

expect(
() => api.traceSync('syncTrace', () {
span = api.Context.current.span as Span;
() => api.traceContextSync('syncTrace', (context) {
span = api.spanFromContext(context) as Span;
throw Exception('Oh noes!');
}, tracer: tracer),
throwsException);
Expand All @@ -95,8 +95,8 @@ void main() {
sdk.SpanLimits());
late Span span;

await api.trace('asyncTrace', () async {
span = api.Context.current.span as Span;
await api.traceContext('asyncTrace', (context) async {
span = api.spanFromContext(context) as Span;
}, tracer: tracer);

expect(
Expand All @@ -116,8 +116,8 @@ void main() {
final spans = <Span>[];

for (var i = 0; i < 5; i++) {
await api.trace('asyncTrace', () async {
spans.add(api.Context.current.span as Span);
await api.traceContext('asyncTrace', (context) async {
spans.add(api.spanFromContext(context) as Span);
}, tracer: tracer);
}

Expand All @@ -139,8 +139,8 @@ void main() {
late Span span;

try {
await api.trace('asyncTrace', () async {
span = api.Context.current.span as Span;
await api.traceContext('asyncTrace', (context) async {
span = api.spanFromContext(context) as Span;
throw Exception('Oh noes!');
}, tracer: tracer);
} catch (e) {
Expand Down Expand Up @@ -171,8 +171,8 @@ void main() {
late Span span;

try {
await api.trace('asyncTrace', () async {
span = api.Context.current.span as Span;
await api.traceContext('asyncTrace', (context) async {
span = api.spanFromContext(context) as Span;
return Future.error(Exception('Oh noes!'));
}, tracer: tracer);
} catch (e) {
Expand Down
25 changes: 13 additions & 12 deletions test/unit/api/context/context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,36 @@ void main() {
sdk.SpanLimits(),
sdk.DateTimeTimeProvider().now);

group('get Span', () {
group('spanFromContext', () {
test('returns Span when exists', () {
final childContext = api.Context.current.withSpan(testSpan);
final context =
api.contextWithSpan(globalContextManager.active, testSpan);

expect(childContext.span, same(testSpan));
expect(api.spanFromContext(context), same(testSpan));
});

test('returns an invalid Span when a Span does not exist in the Context',
() {
final context = api.Context.current;
final context = globalContextManager.active;

expect(context.span, isA<NonRecordingSpan>());
expect(context.span.spanContext.isValid, isFalse);
expect(api.spanFromContext(context), isA<NonRecordingSpan>());
expect(api.spanContextFromContext(context).isValid, isFalse);
});
});

group('get SpanContext', () {
group('spanContextFromContext', () {
test('returns SpanContext when Span exists', () {
final testContext = api.Context.current.withSpan(testSpan);
final testContext =
api.contextWithSpan(globalContextManager.active, testSpan);

expect(testContext.spanContext, same(testSpanContext));
expect(api.spanContextFromContext(testContext), same(testSpanContext));
});

test(
'returns an invalid SpanContext when a Span does not exist in the Context',
() {
final testContext = api.Context.current;

expect(testContext.spanContext.isValid, isFalse);
expect(api.spanContextFromContext(globalContextManager.active).isValid,
isFalse);
});
});
}
29 changes: 16 additions & 13 deletions test/unit/sdk/trace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,42 @@ import 'package:opentelemetry/src/api/open_telemetry.dart';
import 'package:test/test.dart';

void main() {
test('traceSync returns value', () {
final value = traceSync('foo', () => 'bar');
test('traceContextSync returns value', () {
final value = traceContextSync('foo', (_) => 'bar');
expect(value, 'bar');
});

test('traceSync throws exception', () {
test('traceContextSync throws exception', () {
final bang = Exception('bang!');
expect(() => traceSync('foo', () => throw bang), throwsA(bang));
expect(() => traceContextSync('foo', (_) => throw bang), throwsA(bang));
});

test('traceSync wants you to use trace instead', () {
expect(() => traceSync('foo', () async => ''), throwsArgumentError);
expect(() => traceSync('foo', Future.value), throwsArgumentError);
test('traceContextSync wants you to use trace instead', () {
expect(() => traceContextSync('foo', (_) async => ''), throwsArgumentError);
expect(() => traceContextSync('foo', Future.value), throwsArgumentError);
});

test('trace returns future value', () async {
await expectLater(trace('foo', () async => 'bar'), completion('bar'));
await expectLater(
trace('foo', () => Future.value('bazz')), completion('bazz'));
traceContext('foo', (_) async => 'bar'), completion('bar'));
await expectLater(
traceContext('foo', (_) => Future.value('baz')), completion('baz'));
});

test('trace throws future error', () async {
// Exception thrown from synchronous code in async function.
final bang = Exception('bang!');
await expectLater(trace('foo', () async => throw bang), throwsA(bang));
await expectLater(
traceContext('foo', (_) async => throw bang), throwsA(bang));

// Exception thrown from asynchronous code in async function.
final buzz = Exception('buzz!!');
await expectLater(
trace('foo', () async => Future.error(buzz)), throwsA(buzz));
traceContext('foo', (_) async => Future.error(buzz)), throwsA(buzz));

// Exception thrown from asynchronous code in async function.
final bazz = Exception('bazz!!');
await expectLater(trace('foo', () => Future.error(bazz)), throwsA(bazz));
final baz = Exception('baz!!');
await expectLater(
traceContext('foo', (_) => Future.error(baz)), throwsA(baz));
});
}

0 comments on commit dc0722c

Please sign in to comment.