-
Notifications
You must be signed in to change notification settings - Fork 11
[Feature] Web Navigator Locks #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7cb433d
move sqlite_async to packages
stevensJourney fe85a70
monorepo updates
stevensJourney d434ad3
move drift package
stevensJourney 6fe8765
fix actions
stevensJourney 5d0fd64
more test fixes
stevensJourney 394c406
feat: use navigator locks
stevensJourney bdb966e
merge in main
stevensJourney 9e15265
comments
stevensJourney e05ce93
cleanup
stevensJourney 82b83c1
more cleanup
stevensJourney 588d92f
fix tests
stevensJourney e30ebf8
fix unhandled exception
stevensJourney 4ba51e5
cleanup
stevensJourney 3b78d5a
use timers
stevensJourney 6b11376
mutex fixes
stevensJourney 0ac04f2
melos tags
stevensJourney d01cb62
chore(release): publish packages
stevensJourney d16b01a
changelog
stevensJourney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,83 @@ | ||
@TestOn('!browser') | ||
import 'dart:isolate'; | ||
import 'dart:async'; | ||
import 'dart:math'; | ||
|
||
import 'package:sqlite_async/src/native/native_isolate_mutex.dart'; | ||
import 'package:sqlite_async/sqlite_async.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'utils/test_utils_impl.dart'; | ||
|
||
final testUtils = TestUtils(); | ||
|
||
void main() { | ||
group('Mutex Tests', () { | ||
test('Closing', () async { | ||
// Test that locks are properly released when calling SharedMutex.close() | ||
// in in Isolate. | ||
// A timeout in this test indicates a likely error. | ||
for (var i = 0; i < 50; i++) { | ||
final mutex = SimpleMutex(); | ||
final serialized = mutex.shared; | ||
|
||
final result = await Isolate.run(() async { | ||
return _lockInIsolate(serialized); | ||
group('Shared Mutex Tests', () { | ||
test('Queue exclusive operations', () async { | ||
final m = Mutex(); | ||
final collection = List.generate(10, (index) => index); | ||
final results = <int>[]; | ||
|
||
final futures = collection.map((element) async { | ||
return m.lock(() async { | ||
// Simulate some asynchronous work | ||
await Future.delayed(Duration(milliseconds: Random().nextInt(100))); | ||
results.add(element); | ||
return element; | ||
}); | ||
}).toList(); | ||
|
||
await mutex.lock(() async {}); | ||
// Await all the promises | ||
await Future.wait(futures); | ||
|
||
expect(result, equals(5)); | ||
} | ||
// Check if the results are in ascending order | ||
expect(results, equals(collection)); | ||
}); | ||
}); | ||
|
||
test('Re-use after closing', () async { | ||
// Test that shared locks can be opened and closed multiple times. | ||
final mutex = SimpleMutex(); | ||
final serialized = mutex.shared; | ||
test('Timeout should throw a TimeoutException', () async { | ||
final m = Mutex(); | ||
m.lock(() async { | ||
await Future.delayed(Duration(milliseconds: 300)); | ||
}); | ||
|
||
final result = await Isolate.run(() async { | ||
return _lockInIsolate(serialized); | ||
}); | ||
await expectLater( | ||
m.lock(() async { | ||
print('This should not get executed'); | ||
}, timeout: Duration(milliseconds: 200)), | ||
throwsA((e) => | ||
e is TimeoutException && | ||
e.message!.contains('Failed to acquire lock'))); | ||
}); | ||
|
||
final result2 = await Isolate.run(() async { | ||
return _lockInIsolate(serialized); | ||
}); | ||
test('In-time timeout should function normally', () async { | ||
final m = Mutex(); | ||
final results = []; | ||
m.lock(() async { | ||
await Future.delayed(Duration(milliseconds: 100)); | ||
results.add(1); | ||
}); | ||
|
||
await mutex.lock(() async {}); | ||
await m.lock(() async { | ||
results.add(2); | ||
}, timeout: Duration(milliseconds: 200)); | ||
|
||
expect(result, equals(5)); | ||
expect(result2, equals(5)); | ||
}); | ||
}, timeout: const Timeout(Duration(milliseconds: 5000))); | ||
} | ||
expect(results, equals([1, 2])); | ||
}); | ||
|
||
Future<Object> _lockInIsolate( | ||
SerializedMutex smutex, | ||
) async { | ||
final mutex = smutex.open(); | ||
// Start a "thread" that repeatedly takes a lock | ||
_infiniteLock(mutex).ignore(); | ||
await Future.delayed(const Duration(milliseconds: 10)); | ||
// Then close the mutex while the above loop is running. | ||
await mutex.close(); | ||
|
||
return 5; | ||
} | ||
test('Different Mutex instances should cause separate locking', () async { | ||
final m1 = Mutex(); | ||
final m2 = Mutex(); | ||
|
||
Future<void> _infiniteLock(SharedMutex mutex) async { | ||
while (true) { | ||
await mutex.lock(() async { | ||
await Future.delayed(const Duration(milliseconds: 1)); | ||
final results = []; | ||
final p1 = m1.lock(() async { | ||
await Future.delayed(Duration(milliseconds: 300)); | ||
results.add(1); | ||
}); | ||
} | ||
|
||
final p2 = m2.lock(() async { | ||
results.add(2); | ||
}); | ||
|
||
await p1; | ||
await p2; | ||
expect(results, equals([2, 1])); | ||
}); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.