|
1 | 1 | // ignore_for_file: invalid_use_of_internal_member
|
2 | 2 | // The lint above is okay, because we're using another Sentry package
|
| 3 | +import 'dart:async'; |
3 | 4 | import 'dart:convert';
|
4 | 5 | // backcompatibility for Flutter < 3.3
|
5 | 6 | // ignore: unnecessary_import
|
@@ -333,6 +334,158 @@ void main() {
|
333 | 334 | },
|
334 | 335 | );
|
335 | 336 |
|
| 337 | + test( |
| 338 | + 'loadStructuredBinaryData: does not create any spans and just forwords the call to the underlying assetbundle if disabled', |
| 339 | + () async { |
| 340 | + final sut = fixture.getSut(structuredDataTracing: false); |
| 341 | + final tr = fixture._hub.startTransaction( |
| 342 | + 'name', |
| 343 | + 'op', |
| 344 | + bindToScope: true, |
| 345 | + ); |
| 346 | + |
| 347 | + final data = await sut.loadStructuredBinaryData<String>( |
| 348 | + _testFileName, |
| 349 | + (value) async => utf8.decode( |
| 350 | + value.buffer.asUint8List(value.offsetInBytes, value.lengthInBytes), |
| 351 | + ), |
| 352 | + ); |
| 353 | + expect(data, 'Hello World!'); |
| 354 | + |
| 355 | + await tr.finish(); |
| 356 | + |
| 357 | + final tracer = (tr as SentryTracer); |
| 358 | + |
| 359 | + expect(tracer.children.length, 0); |
| 360 | + }, |
| 361 | + ); |
| 362 | + |
| 363 | + test( |
| 364 | + 'loadStructuredBinaryData: finish with errored span if loading fails', |
| 365 | + () async { |
| 366 | + final sut = fixture.getSut(throwException: true); |
| 367 | + final tr = fixture._hub.startTransaction( |
| 368 | + 'name', |
| 369 | + 'op', |
| 370 | + bindToScope: true, |
| 371 | + ); |
| 372 | + await expectLater( |
| 373 | + sut.loadStructuredBinaryData<String>( |
| 374 | + _testFileName, |
| 375 | + (value) async => utf8.decode( |
| 376 | + value.buffer |
| 377 | + .asUint8List(value.offsetInBytes, value.lengthInBytes), |
| 378 | + ), |
| 379 | + ), |
| 380 | + throwsA(isA<Exception>()), |
| 381 | + ); |
| 382 | + |
| 383 | + await tr.finish(); |
| 384 | + |
| 385 | + final tracer = (tr as SentryTracer); |
| 386 | + final span = tracer.children.first; |
| 387 | + |
| 388 | + expect(span.status, SpanStatus.internalError()); |
| 389 | + expect(span.finished, true); |
| 390 | + expect(span.throwable, isA<Exception>()); |
| 391 | + expect(span.context.operation, 'file.read'); |
| 392 | + expect( |
| 393 | + span.context.description, |
| 394 | + 'AssetBundle.loadStructuredBinaryData<String>: test.txt', |
| 395 | + ); |
| 396 | + }, |
| 397 | + ); |
| 398 | + |
| 399 | + test( |
| 400 | + 'loadStructuredBinaryData: finish with errored span if parsing fails', |
| 401 | + () async { |
| 402 | + final sut = fixture.getSut(throwException: false); |
| 403 | + final tr = fixture._hub.startTransaction( |
| 404 | + 'name', |
| 405 | + 'op', |
| 406 | + bindToScope: true, |
| 407 | + ); |
| 408 | + await expectLater( |
| 409 | + sut.loadStructuredBinaryData<String>( |
| 410 | + _testFileName, |
| 411 | + (value) async => throw Exception('error while parsing'), |
| 412 | + ), |
| 413 | + throwsA(isA<Exception>()), |
| 414 | + ); |
| 415 | + |
| 416 | + await tr.finish(); |
| 417 | + |
| 418 | + final tracer = (tr as SentryTracer); |
| 419 | + var span = tracer.children.first; |
| 420 | + |
| 421 | + expect(tracer.children.length, 2); |
| 422 | + |
| 423 | + expect(span.status, SpanStatus.internalError()); |
| 424 | + expect(span.finished, true); |
| 425 | + expect(span.throwable, isA<Exception>()); |
| 426 | + expect(span.context.operation, 'file.read'); |
| 427 | + expect( |
| 428 | + span.context.description, |
| 429 | + 'AssetBundle.loadStructuredBinaryData<String>: test.txt', |
| 430 | + ); |
| 431 | + |
| 432 | + span = tracer.children[1]; |
| 433 | + |
| 434 | + expect(span.status, SpanStatus.internalError()); |
| 435 | + expect(span.finished, true); |
| 436 | + expect(span.throwable, isA<Exception>()); |
| 437 | + expect(span.context.operation, 'serialize.file.read'); |
| 438 | + expect( |
| 439 | + span.context.description, |
| 440 | + 'parsing "resources/test.txt" to "String"', |
| 441 | + ); |
| 442 | + }, |
| 443 | + ); |
| 444 | + |
| 445 | + test( |
| 446 | + 'loadStructuredBinaryData: finish with successfully', |
| 447 | + () async { |
| 448 | + final sut = fixture.getSut(throwException: false); |
| 449 | + final tr = fixture._hub.startTransaction( |
| 450 | + 'name', |
| 451 | + 'op', |
| 452 | + bindToScope: true, |
| 453 | + ); |
| 454 | + |
| 455 | + await sut.loadStructuredBinaryData<String>( |
| 456 | + _testFileName, |
| 457 | + (value) async => utf8.decode( |
| 458 | + value.buffer.asUint8List(value.offsetInBytes, value.lengthInBytes), |
| 459 | + ), |
| 460 | + ); |
| 461 | + |
| 462 | + await tr.finish(); |
| 463 | + |
| 464 | + final tracer = (tr as SentryTracer); |
| 465 | + var span = tracer.children.first; |
| 466 | + |
| 467 | + expect(tracer.children.length, 2); |
| 468 | + |
| 469 | + expect(span.status, SpanStatus.ok()); |
| 470 | + expect(span.finished, true); |
| 471 | + expect(span.context.operation, 'file.read'); |
| 472 | + expect( |
| 473 | + span.context.description, |
| 474 | + 'AssetBundle.loadStructuredBinaryData<String>: test.txt', |
| 475 | + ); |
| 476 | + |
| 477 | + span = tracer.children[1]; |
| 478 | + |
| 479 | + expect(span.status, SpanStatus.ok()); |
| 480 | + expect(span.finished, true); |
| 481 | + expect(span.context.operation, 'serialize.file.read'); |
| 482 | + expect( |
| 483 | + span.context.description, |
| 484 | + 'parsing "resources/test.txt" to "String"', |
| 485 | + ); |
| 486 | + }, |
| 487 | + ); |
| 488 | + |
336 | 489 | test(
|
337 | 490 | 'evict call gets forwarded',
|
338 | 491 | () {
|
@@ -393,6 +546,20 @@ class TestAssetBundle extends CachingAssetBundle {
|
393 | 546 | bool throwException = false;
|
394 | 547 | String? evictKey;
|
395 | 548 |
|
| 549 | + @override |
| 550 | + // ignore: override_on_non_overriding_member |
| 551 | + Future<T> loadStructuredBinaryData<T>( |
| 552 | + String key, FutureOr<T> Function(ByteData data) parser) async { |
| 553 | + if (throwException) { |
| 554 | + throw Exception('exception thrown for testing purposes'); |
| 555 | + } |
| 556 | + if (key == _testFileName) { |
| 557 | + return parser(ByteData.view( |
| 558 | + Uint8List.fromList(utf8.encode('Hello World!')).buffer)); |
| 559 | + } |
| 560 | + return parser(ByteData(0)); |
| 561 | + } |
| 562 | + |
396 | 563 | @override
|
397 | 564 | Future<ByteData> load(String key) async {
|
398 | 565 | if (throwException) {
|
|
0 commit comments