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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/// Licensed to the Apache Software Foundation (ASF) under one
/// or more contributor license agreements. See the NOTICE file
/// distributed with this work for additional information
/// regarding copyright ownership. The ASF licenses this file
/// to you under the Apache License, Version 2.0 (the
/// 'License'); you may not use this file except in compliance
/// with the License. You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing,
/// software distributed under the License is distributed on an
/// 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
/// KIND, either express or implied. See the License for the
/// specific language governing permissions and limitations
/// under the License.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:args/args.dart';
import 'package:collection/collection.dart';
import 'package:http/http.dart' as http;
import 'package:thrift/thrift.dart';
import 'package:thrift/thrift_console.dart';
import 'package:thrift_test/thrift_test.dart';
const TEST_BASETYPES = 1; // 0000 0001
const TEST_STRUCTS = 2; // 0000 0010
const TEST_CONTAINERS = 4; // 0000 0100
const TEST_EXCEPTIONS = 8; // 0000 1000
const TEST_UNKNOWN = 64; // 0100 0000 (Failed to prepare environemt etc.)
const TEST_TIMEOUT = 128; // 1000 0000
const TEST_NOTUSED = 48; // 0011 0000 (reserved bits)
typedef Future FutureFunction();
class TTest {
final int errorCode;
final String name;
final FutureFunction func;
TTest(this.errorCode, this.name, this.func);
}
class TTestError extends Error {
final actual;
final expected;
TTestError(this.actual, this.expected);
String toString() => '$actual != $expected';
}
List<TTest> _tests;
ThriftTestClient client;
bool verbose;
/// Adapted from TestClient.php
main(List<String> args) async {
ArgResults results = _parseArgs(args);
if (results == null) {
exit(TEST_UNKNOWN);
}
verbose = results['verbose'] == true;
await _initTestClient(
host: results['host'],
port: int.parse(results['port']),
transportType: results['transport'],
protocolType: results['protocol']).catchError((e) {
stdout.writeln('Error:');
stdout.writeln('$e');
if (e is Error) {
stdout.writeln('${e.stackTrace}');
}
exit(TEST_UNKNOWN);
});
// run tests
_tests = _createTests();
int result = 0;
for (TTest test in _tests) {
if (verbose) stdout.write('${test.name}... ');
try {
await test.func();
if (verbose) stdout.writeln('success!');
} catch (e) {
if (verbose) stdout.writeln('$e');
result = result | test.errorCode;
}
}
exit(result);
}
ArgResults _parseArgs(List<String> args) {
var parser = new ArgParser();
parser.addOption('host', defaultsTo: 'localhost', help: 'The server host');
parser.addOption('port', defaultsTo: '9090', help: 'The port to connect to');
parser.addOption('transport',
defaultsTo: 'buffered',
allowed: ['buffered', 'framed', 'http'],
help: 'The transport name',
allowedHelp: {
'buffered': 'TBufferedTransport',
'framed': 'TFramedTransport'
});
parser.addOption('protocol',
defaultsTo: 'binary',
allowed: ['binary', 'compact', 'json'],
help: 'The protocol name',
allowedHelp: {
'binary': 'TBinaryProtocol',
'compact': 'TCompactProtocol',
'json': 'TJsonProtocol'
});
parser.addFlag('verbose', defaultsTo: true);
ArgResults results;
try {
results = parser.parse(args);
} catch (e) {
stdout.writeln('$e\n');
}
if (results == null) stdout.write(parser.usage);
return results;
}
TProtocolFactory getProtocolFactory(String protocolType) {
if (protocolType == 'binary') {
return new TBinaryProtocolFactory();
} else if (protocolType == 'compact') {
return new TCompactProtocolFactory();
} else if (protocolType == 'json') {
return new TJsonProtocolFactory();
}
throw new ArgumentError.value(protocolType);
}
Future _initTestClient(
{String host, int port, String transportType, String protocolType}) async {
TTransport transport;
var protocolFactory = getProtocolFactory(protocolType);
if (transportType == 'http') {
var httpClient = new http.IOClient();
var uri = Uri.parse('http://$host:$port');
var config = new THttpConfig(uri, {});
transport = new THttpClientTransport(httpClient, config);
} else {
var socket = await Socket.connect(host, port);
transport = new TClientSocketTransport(new TTcpSocket(socket));
if (transportType == 'framed') {
transport = new TFramedTransport(transport);
}
}
var protocol = protocolFactory.getProtocol(transport);
client = new ThriftTestClient(protocol);
await transport.open();
}
List<TTest> _createTests() {
List<TTest> tests = [];
var xtruct = new Xtruct()
..string_thing = 'Zero'
..byte_thing = 1
..i32_thing = -3
..i64_thing = -5;
tests.add(new TTest(TEST_BASETYPES, 'testVoid', () async {
await client.testVoid();
}));
tests.add(new TTest(TEST_BASETYPES, 'testString', () async {
var input = 'Test';
var result = await client.testString(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_BASETYPES, 'testBool', () async {
var input = true;
var result = await client.testBool(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_BASETYPES, 'testByte', () async {
var input = 64;
var result = await client.testByte(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_BASETYPES, 'testI32', () async {
var input = 2147483647;
var result = await client.testI32(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_BASETYPES, 'testI64', () async {
var input = 9223372036854775807;
var result = await client.testI64(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_BASETYPES, 'testDouble', () async {
var input = 3.1415926;
var result = await client.testDouble(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_BASETYPES, 'testBinary', () async {
var utf8Codec = const Utf8Codec();
var input = utf8Codec.encode('foo');
var result = await client.testBinary(input);
var equality = const ListEquality();
if (!equality.equals(result, input)) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testStruct', () async {
var result = await client.testStruct(xtruct);
if ('$result' != '$xtruct') throw new TTestError(result, xtruct);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testNest', () async {
var input = new Xtruct2()
..byte_thing = 1
..struct_thing = xtruct
..i32_thing = -3;
var result = await client.testNest(input);
if ('$result' != '$input') throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testMap', () async {
Map<int, int> input = {1: -10, 2: -9, 3: -8, 4: -7, 5: -6};
var result = await client.testMap(input);
var equality = const MapEquality();
if (!equality.equals(result, input)) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testSet', () async {
var input = new Set<int>.from([-2, -1, 0, 1, 2]);
var result = await client.testSet(input);
var equality = const SetEquality();
if (!equality.equals(result, input)) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testList', () async {
var input = [-2, -1, 0, 1, 2];
var result = await client.testList(input);
var equality = const ListEquality();
if (!equality.equals(result, input)) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testEnum', () async {
await _testEnum(Numberz.ONE);
await _testEnum(Numberz.TWO);
await _testEnum(Numberz.THREE);
await _testEnum(Numberz.FIVE);
await _testEnum(Numberz.EIGHT);
}));
tests.add(new TTest(TEST_BASETYPES, 'testTypedef', () async {
var input = 309858235082523;
var result = await client.testTypedef(input);
if (result != input) throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_CONTAINERS, 'testMapMap', () async {
Map<int, Map<int, int>> result = await client.testMapMap(1);
if (result.isEmpty || result[result.keys.first].isEmpty) {
throw new TTestError(result, 'Map<int, Map<int, int>>');
}
}));
tests.add(new TTest(TEST_CONTAINERS, 'testInsanity', () async {
var input = new Insanity();
input.userMap = {Numberz.FIVE: 5000};
input.xtructs = [xtruct];
Map<int, Map<int, Insanity>> result = await client.testInsanity(input);
if (result.isEmpty || result[result.keys.first].isEmpty) {
throw new TTestError(result, 'Map<int, Map<int, Insanity>>');
}
}));
tests.add(new TTest(TEST_CONTAINERS, 'testMulti', () async {
var input = new Xtruct()
..string_thing = 'Hello2'
..byte_thing = 123
..i32_thing = 456
..i64_thing = 789;
var result = await client.testMulti(input.byte_thing, input.i32_thing,
input.i64_thing, {1: 'one'}, Numberz.EIGHT, 5678);
if ('$result' != '$input') throw new TTestError(result, input);
}));
tests.add(new TTest(TEST_EXCEPTIONS, 'testException', () async {
try {
await client.testException('Xception');
} on Xception catch (_) {
return;
}
throw new TTestError(null, 'Xception');
}));
tests.add(new TTest(TEST_EXCEPTIONS, 'testMultiException', () async {
try {
await client.testMultiException('Xception2', 'foo');
} on Xception2 catch (_) {
return;
}
throw new TTestError(null, 'Xception2');
}));
return tests;
}
Future _testEnum(int input) async {
var result = await client.testEnum(input);
if (result != input) throw new TTestError(result, input);
}
|