|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | + |
| 3 | +import 'package:livekit_client/src/support/disposable.dart'; |
| 4 | + |
| 5 | +class TestDisposable extends Disposable { |
| 6 | + @override |
| 7 | + Future<bool> dispose() async { |
| 8 | + return await super.dispose(); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +void main() { |
| 13 | + group('Disposable', () { |
| 14 | + test('should execute all dispose functions even if one fails', () async { |
| 15 | + final disposable = TestDisposable(); |
| 16 | + bool func1Called = false; |
| 17 | + bool func2Called = false; |
| 18 | + bool func3Called = false; |
| 19 | + |
| 20 | + // Dispose functions are called in reverse order of addition |
| 21 | + |
| 22 | + // Added 1st -> Called 3rd |
| 23 | + disposable.onDispose(() async { |
| 24 | + func1Called = true; |
| 25 | + }); |
| 26 | + |
| 27 | + // Added 2nd -> Called 2nd |
| 28 | + disposable.onDispose(() async { |
| 29 | + func2Called = true; |
| 30 | + throw Exception('fail'); |
| 31 | + }); |
| 32 | + |
| 33 | + // Added 3rd -> Called 1st |
| 34 | + disposable.onDispose(() async { |
| 35 | + func3Called = true; |
| 36 | + }); |
| 37 | + |
| 38 | + await disposable.dispose(); |
| 39 | + |
| 40 | + expect(func3Called, isTrue, reason: 'Last added (func3) should be called first'); |
| 41 | + expect(func2Called, isTrue, reason: 'Middle added (func2) should be called'); |
| 42 | + expect(func1Called, isTrue, reason: 'First added (func1) should be called even if func2 failed'); |
| 43 | + }); |
| 44 | + }); |
| 45 | +} |
0 commit comments