Skip to content
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

Use WebSocket fakes #3714

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build_runner/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies:
stream_transform: ^2.0.0
timing: ^1.0.0
watcher: ^1.0.0
web_socket_channel: ">=2.0.0 <4.0.0"
web_socket_channel: ^3.0.0
yaml: ^3.0.0

dev_dependencies:
Expand All @@ -57,6 +57,7 @@ dev_dependencies:
test: ^1.25.5
test_descriptor: ^2.0.0
test_process: ^2.0.0
web_socket: ^0.1.5

topics:
- build-runner
87 changes: 13 additions & 74 deletions build_runner/test/server/serve_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:_test_common/common.dart';
import 'package:async/async.dart';
import 'package:build/build.dart';
import 'package:build_runner/src/entrypoint/options.dart';
import 'package:build_runner/src/generate/watch_impl.dart';
Expand All @@ -20,81 +19,12 @@ import 'package:build_runner_core/src/generate/performance_tracker.dart';
import 'package:build_runner_core/src/package_graph/target_graph.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/fake.dart';
import 'package:test/test.dart';
import 'package:web_socket/testing.dart';
import 'package:web_socket_channel/adapter_web_socket_channel.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class FakeSink extends DelegatingStreamSink implements WebSocketSink {
final FakeWebSocketChannel _channel;

FakeSink(this._channel) : super(_channel._controller.sink);

@override
Future close([int? closeCode, String? closeReason]) async {
await super.close();
_channel._isClosed = true;
_channel._closeCode = closeCode;
_channel._closeReason = closeReason;
await _channel._closed(closeCode, closeReason);
}
}

class FakeWebSocketChannel extends StreamChannelMixin
implements WebSocketChannel {
final StreamChannel _controller;
final Future Function(int? closeCode, String? closeReason) _closed;

bool _isClosed = false;
int? _closeCode;
String? _closeReason;

FakeWebSocketChannel(this._controller, this._closed);

@override
int? get closeCode => _closeCode;

@override
String? get closeReason => _closeReason;

@override
String? get protocol => throw UnimplementedError();

@override
Future<void> get ready => Future.value();

@override
WebSocketSink get sink => FakeSink(this);

@override
Stream get stream => _controller.stream;

Future _remoteClosed(int closeCode, String? closeReason) async {
if (!_isClosed) {
await sink.close(closeCode, closeReason);
}
}
}

(WebSocketChannel, WebSocketChannel) createFakes() {
final peer1Write = StreamController<dynamic>();
final peer2Write = StreamController<dynamic>();

late FakeWebSocketChannel foreign;
late FakeWebSocketChannel local;

foreign = FakeWebSocketChannel(
StreamChannel(peer2Write.stream, peer1Write.sink),
(closeCode, closeReason) =>
local._remoteClosed(closeCode ?? 1005, closeReason));
local = FakeWebSocketChannel(
StreamChannel(peer1Write.stream, peer2Write.sink),
(closeCode, closeReason) =>
foreign._remoteClosed(closeCode ?? 1005, closeReason));

return (foreign, local);
}

void main() {
late ServeHandler serveHandler;
late InMemoryRunnerAssetReader reader;
Expand Down Expand Up @@ -376,8 +306,12 @@ void main() {

handler = BuildUpdatesWebSocketHandler(watchImpl, mockHandlerFactory);

(serverChannel1, clientChannel1) = createFakes();
(serverChannel2, clientChannel2) = createFakes();
final (serverSocket1, clientSocket1) = fakes();
final (serverSocket2, clientSocket2) = fakes();
serverChannel1 = AdapterWebSocketChannel(serverSocket1);
clientChannel1 = AdapterWebSocketChannel(clientSocket1);
serverChannel2 = AdapterWebSocketChannel(serverSocket2);
clientChannel2 = AdapterWebSocketChannel(clientSocket2);
});

tearDown(() {
Expand All @@ -393,6 +327,7 @@ void main() {
await createMockConnection(serverChannel1, 'web');
await createMockConnection(serverChannel2, 'web');
await handler.emitUpdateMessage(BuildResult(BuildStatus.success, []));
await pumpEventQueue();
await clientChannel1.sink.close();
await clientChannel2.sink.close();
});
Expand All @@ -403,8 +338,10 @@ void main() {
await createMockConnection(serverChannel1, 'web');
await createMockConnection(serverChannel2, 'web');
await handler.emitUpdateMessage(BuildResult(BuildStatus.success, []));
await pumpEventQueue();
await clientChannel2.sink.close();
await handler.emitUpdateMessage(BuildResult(BuildStatus.success, []));
await pumpEventQueue();
await clientChannel1.sink.close();
});

Expand Down Expand Up @@ -448,6 +385,7 @@ void main() {
AssetId('a', 'web/index.html'),
AssetId('a', 'lib/some.dart.js'),
]));
await pumpEventQueue();
await clientChannel1.sink.close();
});

Expand Down Expand Up @@ -487,6 +425,7 @@ void main() {
AssetId('a', 'web2/index.html'),
AssetId('a', 'lib/some.dart.js'),
]));
await pumpEventQueue();
await clientChannel1.sink.close();
await clientChannel2.sink.close();
});
Expand Down
Loading