|
| 1 | +// Copyright 2021-2022 Workiva. |
| 2 | +// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information |
| 3 | + |
| 4 | +@TestOn('vm') |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:fixnum/fixnum.dart'; |
| 8 | +import 'package:logging/logging.dart'; |
| 9 | +import 'package:mocktail/mocktail.dart'; |
| 10 | +import 'package:opentelemetry/sdk.dart' as sdk; |
| 11 | +import 'package:opentelemetry/src/experimental_sdk.dart' as sdk; |
| 12 | +import 'package:opentelemetry/src/sdk/logs/log_record_limit.dart'; |
| 13 | +import 'package:opentelemetry/src/sdk/logs/processors/simple_log_record_processor.dart'; |
| 14 | +import 'package:test/test.dart'; |
| 15 | + |
| 16 | +import '../../../mocks.dart'; |
| 17 | + |
| 18 | +void main() { |
| 19 | + late sdk.LogRecordExporter exporter; |
| 20 | + late sdk.LogRecordProcessor processor; |
| 21 | + |
| 22 | + setUp(() { |
| 23 | + exporter = MockLogRecordExporter(); |
| 24 | + processor = SimpleLogRecordProcessor(exporter: exporter); |
| 25 | + when(() => exporter.export(any())).thenAnswer((_) async => sdk.ExportResult(code: sdk.ExportResultCode.success)); |
| 26 | + when(() => exporter.shutdown()).thenAnswer((_) => Future.value()); |
| 27 | + }); |
| 28 | + |
| 29 | + test('executes export', () { |
| 30 | + final logRecord = sdk.LogRecord( |
| 31 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 32 | + logRecordLimits: LogRecordLimitsImpl(), |
| 33 | + ); |
| 34 | + |
| 35 | + processor.onEmit(logRecord); |
| 36 | + |
| 37 | + verify(() => exporter.export([logRecord])).called(1); |
| 38 | + }); |
| 39 | + |
| 40 | + test('executes export and fail', () async { |
| 41 | + var errorMessage = ''; |
| 42 | + Logger.root.onRecord.listen((data) { |
| 43 | + errorMessage = data.message; |
| 44 | + }); |
| 45 | + final logRecord = sdk.LogRecord( |
| 46 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 47 | + logRecordLimits: LogRecordLimitsImpl(), |
| 48 | + ); |
| 49 | + |
| 50 | + when(() => exporter.export(any())).thenAnswer((_) async => sdk.ExportResult(code: sdk.ExportResultCode.failed)); |
| 51 | + |
| 52 | + processor.onEmit(logRecord); |
| 53 | + |
| 54 | + await Future.delayed(const Duration(milliseconds: 50)); |
| 55 | + |
| 56 | + expect(errorMessage, 'SimpleLogRecordProcessor: log record export failed'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('shutdown exporters on forced flush', () async { |
| 60 | + await processor.shutdown(); |
| 61 | + |
| 62 | + verify(exporter.shutdown).called(1); |
| 63 | + }); |
| 64 | + |
| 65 | + test('forceFlush waits for all pending exports to complete', () async { |
| 66 | + when(() => exporter.export(any())).thenAnswer((_) async { |
| 67 | + await Future.delayed(const Duration(seconds: 1)); |
| 68 | + return sdk.ExportResult(code: sdk.ExportResultCode.success); |
| 69 | + }); |
| 70 | + |
| 71 | + // Emit two log records, creating two pending exports. |
| 72 | + processor.onEmit( |
| 73 | + sdk.LogRecord( |
| 74 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 75 | + logRecordLimits: LogRecordLimitsImpl(), |
| 76 | + timeProvider: FakeTimeProvider(now: Int64(123))), |
| 77 | + ); |
| 78 | + await Future.delayed(Duration(milliseconds: 50)); |
| 79 | + processor.onEmit( |
| 80 | + sdk.LogRecord( |
| 81 | + instrumentationScope: sdk.InstrumentationScope('library_name', 'library_version', 'url://schema', []), |
| 82 | + logRecordLimits: LogRecordLimitsImpl(), |
| 83 | + timeProvider: FakeTimeProvider(now: Int64(123))), |
| 84 | + ); |
| 85 | + expect((processor as SimpleLogRecordProcessor).exportsCompletion.length, 2); |
| 86 | + // Ensure the exports are pending. |
| 87 | + final flushFuture = processor.forceFlush(); |
| 88 | + expect(flushFuture, completes); |
| 89 | + }); |
| 90 | +} |
0 commit comments