forked from dart-lang/webdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_service_test.dart
138 lines (116 loc) · 3.87 KB
/
debug_service_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2019, 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.
@TestOn('vm')
@Timeout(Duration(minutes: 2))
library;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:test/test.dart';
import 'package:test_common/test_sdk_configuration.dart';
import 'package:vm_service/vm_service.dart';
import 'fixtures/context.dart';
import 'fixtures/project.dart';
import 'fixtures/utilities.dart';
void main() {
final provider = TestSdkConfigurationProvider();
tearDownAll(provider.dispose);
final context = TestContext(TestProject.test, provider);
setUpAll(() async {
// Disable DDS as we're testing DWDS behavior.
await context.setUp(
debugSettings: TestDebugSettings.noDevTools().copyWith(spawnDds: false),
);
});
tearDownAll(() async {
await context.tearDown();
});
test('Refuses connections without the auth token', () async {
expect(
WebSocket.connect('ws://localhost:${context.debugConnection.port}/ws'),
throwsA(isA<WebSocketException>()),
);
});
test('Accepts connections with the auth token', () async {
expect(
WebSocket.connect('${context.debugConnection.uri}/ws')
.then((ws) => ws.close()),
completes,
);
});
test(
'Refuses additional connections when in single client mode',
() async {
final ddsWs = await WebSocket.connect(
'${context.debugConnection.uri}/ws',
);
final completer = Completer<void>();
ddsWs.listen((event) {
final response = json.decode(event as String);
expect(response['id'], '0');
expect(response.containsKey('result'), isTrue);
final result = response['result'] as Map<String, dynamic>;
expect(result['type'], 'Success');
completer.complete();
});
const yieldControlToDDS = <String, dynamic>{
'jsonrpc': '2.0',
'id': '0',
'method': '_yieldControlToDDS',
'params': {
'uri': 'http://localhost:123',
},
};
ddsWs.add(json.encode(yieldControlToDDS));
await completer.future;
// While DDS is connected, expect additional connections to fail.
await expectLater(
WebSocket.connect('${context.debugConnection.uri}/ws'),
throwsA(isA<WebSocketException>()),
);
// However, once DDS is disconnected, additional clients can connect again.
await ddsWs.close();
expect(
WebSocket.connect('${context.debugConnection.uri}/ws')
.then((ws) => ws.close()),
completes,
);
},
);
test(
'Refuses to yield to dwds if existing clients found',
() async {
final ddsWs = await WebSocket.connect(
'${context.debugConnection.uri}/ws',
);
// Connect to vm service.
final ws = await WebSocket.connect('${context.debugConnection.uri}/ws');
final completer = Completer<Map<String, dynamic>>();
ddsWs.listen((event) {
completer.complete(json.decode(event as String));
});
const yieldControlToDDS = <String, dynamic>{
'jsonrpc': '2.0',
'id': '0',
'method': '_yieldControlToDDS',
'params': {
'uri': 'http://localhost:123',
},
};
// DDS should fail to start with existing vm clients.
ddsWs.add(json.encode(yieldControlToDDS));
final response = await completer.future;
expect(response['id'], '0');
expect(response.containsKey('error'), isTrue);
final result = response['error'] as Map<String, dynamic>;
expect(result['code'], RPCErrorKind.kFeatureDisabled.code);
expect(
result['message'],
'Existing VM service clients prevent DDS from taking control.',
);
await ddsWs.close();
await ws.close();
},
);
}