Skip to content

Commit f8d4326

Browse files
committed
test: request before server initialized
1 parent f270e45 commit f8d4326

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

packages/custom_lint/lib/src/v2/custom_lint_analyzer_plugin.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer_plugin/src/protocol/protocol_internal.dart'
1111
show ResponseResult;
1212
import 'package:async/async.dart';
1313
import 'package:custom_lint_core/custom_lint_core.dart';
14+
import 'package:meta/meta.dart';
1415
import 'package:package_config/package_config.dart';
1516
import 'package:path/path.dart' as p;
1617
import 'package:pub_semver/pub_semver.dart';
@@ -114,6 +115,11 @@ class CustomLintServer {
114115
/// Whether plugins should include lints used for debugging.
115116
final bool includeBuiltInLints;
116117

118+
/// Allow mocking a SocketCustomLintServerToClientChannel for test
119+
@visibleForTesting
120+
Future<SocketCustomLintServerToClientChannel?> get clientChannel =>
121+
_clientChannel.safeFirst;
122+
117123
late final StreamSubscription<void> _requestSubscription;
118124
StreamSubscription<void>? _clientChannelEventsSubscription;
119125
late PluginVersionCheckParams _pluginVersionCheckParams;
@@ -187,7 +193,7 @@ class CustomLintServer {
187193
orElse: () async {
188194
return _runner.run(() async {
189195
final clientChannel = await _clientChannel.safeFirst;
190-
if (clientChannel == null || !clientChannel.initialed) {
196+
if (clientChannel == null || !clientChannel.initialized) {
191197
_delayedRequest.add(request);
192198
return null;
193199
}

packages/custom_lint/lib/src/v2/server_to_client_channel.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io';
44

55
import 'package:analyzer_plugin/protocol/protocol.dart';
66
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
7+
import 'package:meta/meta.dart';
78
import 'package:path/path.dart';
89
import 'package:uuid/uuid.dart';
910

@@ -81,12 +82,16 @@ class SocketCustomLintServerToClientChannel {
8182
final CustomLintWorkspace _workspace;
8283

8384
AnalysisSetContextRootsParams _contextRoots;
84-
bool _initialed = false;
85+
bool _initialized = false;
86+
87+
/// List request before initialized
88+
@visibleForTesting
89+
List<Request> initialRequest = [];
8590

8691
/// Initial state
8792
///
8893
/// Returns `true` if requested `analysis.setContextRoots`
89-
bool get initialed => _initialed;
94+
bool get initialized => _initialized;
9095

9196
late final Stream<CustomLintMessage> _messages = _channel.messages
9297
.map((e) => e! as Map<String, Object?>)
@@ -133,7 +138,7 @@ class SocketCustomLintServerToClientChannel {
133138
sendAnalyzerPluginRequest(_version.toRequest(const Uuid().v4())),
134139
sendAnalyzerPluginRequest(_contextRoots.toRequest(const Uuid().v4())),
135140
]);
136-
_initialed = true;
141+
_initialized = true;
137142
}
138143

139144
/// Updates the context roots on the client
@@ -238,6 +243,7 @@ void main(List<String> args) async {
238243
/// Sends a request based on the analyzer_plugin protocol, expecting
239244
/// an analyzer_plugin response.
240245
Future<Response> sendAnalyzerPluginRequest(Request request) async {
246+
if (!_initialized) initialRequest.add(request);
241247
final response = await sendCustomLintRequest(
242248
CustomLintRequest.analyzerPluginRequest(request, id: request.id),
243249
);

packages/custom_lint/test/run_plugin.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ Future<List<AnalysisErrorsParams>> runServerInCliModeForApp(
2020
}
2121

2222
class ManualRunner {
23-
ManualRunner(this.runner, this.channel);
23+
ManualRunner(this.runner, this.channel, this.server);
2424

2525
final CustomLintRunner runner;
2626
final ServerIsolateChannel channel;
27+
final CustomLintServer server;
2728

2829
Future<void> get initialize => runner.initialize;
2930

@@ -91,7 +92,7 @@ Future<ManualRunner> startRunnerForApp(
9192

9293
unawaited(runner.initialize);
9394

94-
return ManualRunner(runner, channel);
95+
return ManualRunner(runner, channel, customLintServer);
9596
});
9697
}
9798

packages/custom_lint/test/server_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,44 @@ void fn() {}
7171
);
7272
});
7373

74+
test('Handles request before server initialized', () async {
75+
final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource);
76+
77+
final app = createLintUsage(
78+
source: {
79+
'lib/main.dart': '''
80+
void fn() {}
81+
82+
void fn2() {}
83+
''',
84+
'lib/another.dart': 'void fn() {}\n',
85+
},
86+
plugins: {'test_lint': plugin.uri},
87+
name: 'test_app',
88+
);
89+
90+
final runner = await startRunnerForApp(app, includeBuiltInLints: false);
91+
final another = File(join(app.path, 'lib', 'another.dart'));
92+
another.writeAsStringSync('test');
93+
await runner.channel.sendRequest(
94+
AnalysisUpdateContentParams({
95+
another.path: AddContentOverlay(another.readAsStringSync()),
96+
}),
97+
);
98+
await runner.initialize;
99+
100+
final initialRequest = (await runner.server.clientChannel)?.initialRequest;
101+
// Request send before `SocketCustomLintServerToClientChannel.init` must delay,
102+
// for ensure initialRequest is [PluginVersionCheckParams, AnalysisSetContextRootsParams]
103+
expect(initialRequest, hasLength(2));
104+
expect(
105+
initialRequest?.last.method,
106+
AnalysisSetContextRootsParams([]).toRequest('test').method,
107+
);
108+
109+
await runner.close();
110+
});
111+
74112
test('Handles files getting deleted', () async {
75113
// Regression test for https://github.com/invertase/dart_custom_lint/issues/105
76114
final plugin = createPlugin(name: 'test_lint', main: helloWordPluginSource);

0 commit comments

Comments
 (0)