-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conformances test that verify that the Client works in Isolates (#…
…889)
- Loading branch information
1 parent
ee03604
commit 74f9d3d
Showing
8 changed files
with
89 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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
10 changes: 10 additions & 0 deletions
10
pkgs/http_client_conformance_tests/lib/src/dummy_isolate.dart
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'dart:async'; | ||
|
||
// ignore: avoid_classes_with_only_static_members | ||
/// An Isolate implementation for the web that throws when used. | ||
abstract class Isolate { | ||
static Future<R> run<R>(FutureOr<R> Function() computation, | ||
{String? debugName}) => | ||
throw ArgumentError.value('true', 'canWorkInIsolates', | ||
'isolate tests are not supported on the web'); | ||
} |
51 changes: 51 additions & 0 deletions
51
pkgs/http_client_conformance_tests/lib/src/isolate_test.dart
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:isolate' if (dart.library.html) 'dummy_isolate.dart'; | ||
|
||
import 'package:async/async.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:stream_channel/stream_channel.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'request_body_server_vm.dart' | ||
if (dart.library.html) 'request_body_server_web.dart'; | ||
|
||
Future<void> _testPost(Client Function() clientFactory, String host) async { | ||
await Isolate.run( | ||
() => clientFactory().post(Uri.http(host, ''), body: 'Hello World!')); | ||
} | ||
|
||
/// Tests that the [Client] is useable from Isolates other than the main | ||
/// isolate. | ||
/// | ||
/// If [canWorkInIsolates] is `false` then the tests will be skipped. | ||
void testIsolate(Client Function() clientFactory, | ||
{bool canWorkInIsolates = true}) { | ||
group('test isolate', () { | ||
late final String host; | ||
late final StreamChannel<Object?> httpServerChannel; | ||
late final StreamQueue<Object?> httpServerQueue; | ||
|
||
setUpAll(() async { | ||
httpServerChannel = await startServer(); | ||
httpServerQueue = StreamQueue(httpServerChannel.stream); | ||
host = 'localhost:${await httpServerQueue.next}'; | ||
}); | ||
tearDownAll(() => httpServerChannel.sink.add(null)); | ||
|
||
test('client.post() with string body', () async { | ||
await _testPost(clientFactory, host); | ||
|
||
final serverReceivedContentType = await httpServerQueue.next; | ||
final serverReceivedBody = await httpServerQueue.next; | ||
|
||
expect(serverReceivedContentType, ['text/plain; charset=utf-8']); | ||
expect(serverReceivedBody, 'Hello World!'); | ||
}); | ||
}, | ||
skip: canWorkInIsolates | ||
? false | ||
: 'does not work outside of the main isolate'); | ||
} |