|
| 1 | +@TestOn('vm') |
| 2 | +library; |
| 3 | + |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:mockito/mockito.dart'; |
| 6 | +import 'package:sentry_flutter/src/thread_info_collector.dart'; |
| 7 | +import 'package:sentry_flutter/src/isolate_helper.dart'; |
| 8 | +import 'package:sentry/src/span_data_convention.dart'; |
| 9 | +import 'package:sentry/src/protocol/sentry_span.dart'; |
| 10 | +import 'package:sentry/src/sentry_span_context.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + late _Fixture fixture; |
| 14 | + |
| 15 | + setUp(() { |
| 16 | + fixture = _Fixture(); |
| 17 | + }); |
| 18 | + |
| 19 | + group('ThreadInfoCollector', () { |
| 20 | + test('sets main thread name when in root isolate', () async { |
| 21 | + fixture.mockHelper.setIsRootIsolate(true); |
| 22 | + fixture.mockHelper.setIsolateName("main(debug)"); |
| 23 | + |
| 24 | + final collector = fixture.getSut(); |
| 25 | + final span = fixture.createMockSpan(); |
| 26 | + |
| 27 | + await collector.onSpanStarted(span); |
| 28 | + |
| 29 | + final setDataCalls = span.setDataCalls; |
| 30 | + expect(setDataCalls.length, equals(2)); |
| 31 | + |
| 32 | + final threadIdCall = setDataCalls |
| 33 | + .firstWhere((call) => call.key == SpanDataConvention.threadId); |
| 34 | + final threadNameCall = setDataCalls |
| 35 | + .firstWhere((call) => call.key == SpanDataConvention.threadName); |
| 36 | + |
| 37 | + expect(threadIdCall.value, equals('main'.hashCode.toString())); |
| 38 | + expect(threadNameCall.value, equals('main')); |
| 39 | + }); |
| 40 | + |
| 41 | + test('adds thread information when isolate has name', () async { |
| 42 | + fixture.mockHelper.setIsRootIsolate(false); |
| 43 | + fixture.mockHelper.setIsolateName('worker-thread'); |
| 44 | + |
| 45 | + final collector = fixture.getSut(); |
| 46 | + final span = fixture.createMockSpan(); |
| 47 | + |
| 48 | + await collector.onSpanStarted(span); |
| 49 | + |
| 50 | + final setDataCalls = span.setDataCalls; |
| 51 | + expect(setDataCalls.length, equals(2)); |
| 52 | + |
| 53 | + final threadIdCall = setDataCalls |
| 54 | + .firstWhere((call) => call.key == SpanDataConvention.threadId); |
| 55 | + final threadNameCall = setDataCalls |
| 56 | + .firstWhere((call) => call.key == SpanDataConvention.threadName); |
| 57 | + |
| 58 | + expect(threadIdCall.value, equals('worker-thread'.hashCode.toString())); |
| 59 | + expect(threadNameCall.value, equals('worker-thread')); |
| 60 | + }); |
| 61 | + |
| 62 | + test('onSpanFinished is no-op', () async { |
| 63 | + fixture.mockHelper.setIsRootIsolate(false); |
| 64 | + final collector = fixture.getSut(); |
| 65 | + final span = fixture.createMockSpan(); |
| 66 | + |
| 67 | + await collector.onSpanFinished(span, DateTime.now()); |
| 68 | + expect(span.setDataCalls, isEmpty); |
| 69 | + }); |
| 70 | + |
| 71 | + test('gets thread info dynamically for each span', () async { |
| 72 | + fixture.mockHelper.setIsRootIsolate(false); |
| 73 | + fixture.mockHelper.setIsolateName('dynamic-test'); |
| 74 | + |
| 75 | + final collector = fixture.getSut(); |
| 76 | + final span = fixture.createMockSpan(); |
| 77 | + |
| 78 | + await collector.onSpanStarted(span); |
| 79 | + final firstCallCount = span.setDataCalls.length; |
| 80 | + |
| 81 | + final span2 = fixture.createMockSpan(); |
| 82 | + await collector.onSpanStarted(span2); |
| 83 | + |
| 84 | + // Should have same number of calls for both spans (thread info collected fresh each time) |
| 85 | + expect(span2.setDataCalls.length, equals(firstCallCount)); |
| 86 | + |
| 87 | + // Both spans should have thread info when isolate has a name |
| 88 | + expect(firstCallCount, equals(2)); |
| 89 | + }); |
| 90 | + |
| 91 | + test('uses provided isolate name correctly', () async { |
| 92 | + fixture.mockHelper.setIsRootIsolate(false); |
| 93 | + fixture.mockHelper.setIsolateName('custom-isolate-name'); |
| 94 | + final collector = fixture.getSut(); |
| 95 | + final span = fixture.createMockSpan(); |
| 96 | + |
| 97 | + await collector.onSpanStarted(span); |
| 98 | + |
| 99 | + // Find thread data calls |
| 100 | + String? threadId; |
| 101 | + String? threadName; |
| 102 | + for (final call in span.setDataCalls) { |
| 103 | + if (call.key == SpanDataConvention.threadId) { |
| 104 | + threadId = call.value as String?; |
| 105 | + } |
| 106 | + if (call.key == SpanDataConvention.threadName) { |
| 107 | + threadName = call.value as String?; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + expect(threadName, equals('custom-isolate-name')); |
| 112 | + expect(threadId, equals('custom-isolate-name'.hashCode.toString())); |
| 113 | + }); |
| 114 | + |
| 115 | + test('no thread info when isolate name is null', () async { |
| 116 | + fixture.mockHelper.setIsRootIsolate(false); |
| 117 | + fixture.mockHelper.setIsolateName(null); |
| 118 | + final collector = fixture.getSut(); |
| 119 | + final span = fixture.createMockSpan(); |
| 120 | + |
| 121 | + await collector.onSpanStarted(span); |
| 122 | + |
| 123 | + // When isolate name is null, no thread data should be set |
| 124 | + expect(span.setDataCalls, isEmpty); |
| 125 | + }); |
| 126 | + |
| 127 | + test('no thread info when isolate name is empty', () async { |
| 128 | + fixture.mockHelper.setIsRootIsolate(false); |
| 129 | + fixture.mockHelper.setIsolateName(''); |
| 130 | + final collector = fixture.getSut(); |
| 131 | + final span = fixture.createMockSpan(); |
| 132 | + |
| 133 | + await collector.onSpanStarted(span); |
| 134 | + |
| 135 | + // When isolate name is empty, no thread data should be set |
| 136 | + expect(span.setDataCalls, isEmpty); |
| 137 | + }); |
| 138 | + }); |
| 139 | +} |
| 140 | + |
| 141 | +class _Fixture { |
| 142 | + late _MockIsolateHelper mockHelper; |
| 143 | + |
| 144 | + _Fixture() { |
| 145 | + mockHelper = _MockIsolateHelper(); |
| 146 | + // Set default return values to avoid null errors |
| 147 | + mockHelper.setIsRootIsolate(false); |
| 148 | + mockHelper.setIsolateName(null); |
| 149 | + } |
| 150 | + |
| 151 | + ThreadInfoCollector getSut() { |
| 152 | + return ThreadInfoCollector(mockHelper); |
| 153 | + } |
| 154 | + |
| 155 | + _MockSpan createMockSpan() { |
| 156 | + return _MockSpan(); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +class _MockIsolateHelper extends Mock implements IsolateHelper { |
| 161 | + bool _isRootIsolate = false; |
| 162 | + String? _isolateName; |
| 163 | + |
| 164 | + @override |
| 165 | + bool isRootIsolate() => _isRootIsolate; |
| 166 | + |
| 167 | + @override |
| 168 | + String? getIsolateName() => _isolateName; |
| 169 | + |
| 170 | + void setIsRootIsolate(bool value) => _isRootIsolate = value; |
| 171 | + void setIsolateName(String? value) => _isolateName = value; |
| 172 | +} |
| 173 | + |
| 174 | +class _MockSpan extends Mock implements SentrySpan { |
| 175 | + final SentrySpanContext _context = SentrySpanContext(operation: 'test'); |
| 176 | + final List<_SetDataCall> setDataCalls = []; |
| 177 | + |
| 178 | + @override |
| 179 | + SentrySpanContext get context => _context; |
| 180 | + |
| 181 | + @override |
| 182 | + void setData(String key, dynamic value) { |
| 183 | + setDataCalls.add(_SetDataCall(key, value)); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +class _SetDataCall { |
| 188 | + final String key; |
| 189 | + final dynamic value; |
| 190 | + |
| 191 | + _SetDataCall(this.key, this.value); |
| 192 | +} |
0 commit comments