Mark Erickson | 932c470 | 2015-08-29 10:46:51 -0500 | [diff] [blame^] | 1 | /// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | /// or more contributor license agreements. See the NOTICE file |
| 3 | /// distributed with this work for additional information |
| 4 | /// regarding copyright ownership. The ASF licenses this file |
| 5 | /// to you under the Apache License, Version 2.0 (the |
| 6 | /// 'License'); you may not use this file except in compliance |
| 7 | /// with the License. You may obtain a copy of the License at |
| 8 | /// |
| 9 | /// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | /// |
| 11 | /// Unless required by applicable law or agreed to in writing, |
| 12 | /// software distributed under the License is distributed on an |
| 13 | /// 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | /// KIND, either express or implied. See the License for the |
| 15 | /// specific language governing permissions and limitations |
| 16 | /// under the License. |
| 17 | |
| 18 | import 'dart:async'; |
| 19 | import 'dart:convert'; |
| 20 | import 'dart:io'; |
| 21 | |
| 22 | import 'package:args/args.dart'; |
| 23 | import 'package:collection/equality.dart'; |
| 24 | import 'package:thrift/thrift.dart'; |
| 25 | import 'package:thrift/thrift_console.dart'; |
| 26 | import 'package:thrift_test/thrift_test.dart'; |
| 27 | |
| 28 | ThriftTestClient client; |
| 29 | bool verbose; |
| 30 | |
| 31 | /// Adapted from TestClient.php |
| 32 | main(List<String> args) async { |
| 33 | var parser = new ArgParser(); |
| 34 | parser.addOption('host', defaultsTo: 'localhost', help: 'The server host'); |
| 35 | parser.addOption('port', defaultsTo: '9090', help: 'The port to connect to'); |
| 36 | parser.addOption('transport', |
| 37 | defaultsTo: 'buffered', |
| 38 | allowed: ['buffered', 'framed'], |
| 39 | help: 'The transport name', |
| 40 | allowedHelp: { |
| 41 | 'buffered': 'TBufferedTransport', |
| 42 | 'framed': 'TFramedTransport' |
| 43 | }); |
| 44 | parser.addOption('protocol', |
| 45 | defaultsTo: 'binary', |
| 46 | allowed: ['binary', 'json'], |
| 47 | help: 'The protocol name', |
| 48 | allowedHelp: {'binary': 'TBinaryProtocol', 'json': 'TJsonProtocol'}); |
| 49 | parser.addFlag('verbose', defaultsTo: false); |
| 50 | |
| 51 | ArgResults results; |
| 52 | try { |
| 53 | results = parser.parse(args); |
| 54 | } catch (e) { |
| 55 | stdout.writeln('$e\n'); |
| 56 | results = null; |
| 57 | } |
| 58 | |
| 59 | if (results == null) { |
| 60 | print(parser.usage); |
| 61 | exit(0); |
| 62 | } |
| 63 | |
| 64 | verbose = results['verbose'] == true; |
| 65 | |
| 66 | await init( |
| 67 | host: results['host'], |
| 68 | port: int.parse(results['port']), |
| 69 | transportType: results['transport'], |
| 70 | protocolType: results['protocol']).then((_) { |
| 71 | exit(0); |
| 72 | }).catchError((e) { |
| 73 | stdout.writeln('Error:'); |
| 74 | stdout.writeln('$e'); |
| 75 | if (e is Error) { |
| 76 | stdout.writeln('${e.stackTrace}'); |
| 77 | } |
| 78 | exit(1); |
| 79 | }); |
| 80 | exit(0); |
| 81 | } |
| 82 | |
| 83 | TProtocolFactory getProtocolFactory(String protocolType) { |
| 84 | if (protocolType == 'binary') { |
| 85 | return new TBinaryProtocolFactory(); |
| 86 | } else if (protocolType == 'json') { |
| 87 | return new TJsonProtocolFactory(); |
| 88 | } |
| 89 | |
| 90 | throw new ArgumentError.value(protocolType); |
| 91 | } |
| 92 | |
| 93 | Future init( |
| 94 | {String host, int port, String transportType, String protocolType}) async { |
| 95 | TTransport transport; |
| 96 | var protocolFactory = getProtocolFactory(protocolType); |
| 97 | |
| 98 | if (transportType == 'http') { |
| 99 | var httpClient = new HttpClient(); |
| 100 | var config = new THttpConfig(Uri.parse('http://localhost'), {}); |
| 101 | transport = new THttpClientTransport(httpClient, config); |
| 102 | } else { |
| 103 | var socket = await Socket.connect(host, port); |
| 104 | transport = new TClientSocketTransport(new TTcpSocket(socket)); |
| 105 | if (transportType == 'framed') { |
| 106 | transport = new TFramedTransport(transport); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | var protocol = protocolFactory.getProtocol(transport); |
| 111 | client = new ThriftTestClient(protocol); |
| 112 | |
| 113 | await transport.open(); |
| 114 | |
| 115 | await runTests(); |
| 116 | } |
| 117 | |
| 118 | Future _test(String name, Function testFunc) async { |
| 119 | if (verbose) stdout.write('$name... '); |
| 120 | await testFunc(); |
| 121 | if (verbose) stdout.writeln('success!'); |
| 122 | } |
| 123 | |
| 124 | Future runTests() async { |
| 125 | var xtruct = new Xtruct() |
| 126 | ..string_thing = 'Zero' |
| 127 | ..byte_thing = 1 |
| 128 | ..i32_thing = -3 |
| 129 | ..i64_thing = -5; |
| 130 | |
| 131 | await _test('testVoid', () async { |
| 132 | await client.testVoid(); |
| 133 | }); |
| 134 | |
| 135 | await _test('testString', () async { |
| 136 | var input = 'Test'; |
| 137 | var result = await client.testString(input); |
| 138 | if (result != input) throw new StateError('$result != $input'); |
| 139 | }); |
| 140 | |
| 141 | await _test('testBool', () async { |
| 142 | var input = true; |
| 143 | var result = await client.testBool(input); |
| 144 | if (result != input) throw new StateError('$result != $input'); |
| 145 | }); |
| 146 | |
| 147 | await _test('testByte', () async { |
| 148 | var input = 64; |
| 149 | var result = await client.testByte(input); |
| 150 | if (result != input) throw new StateError('$result != $input'); |
| 151 | }); |
| 152 | |
| 153 | await _test('testI32', () async { |
| 154 | var input = 2147483647; |
| 155 | var result = await client.testI32(input); |
| 156 | if (result != input) throw new StateError('$result != $input'); |
| 157 | }); |
| 158 | |
| 159 | await _test('testI64', () async { |
| 160 | var input = 9223372036854775807; |
| 161 | var result = await client.testI64(input); |
| 162 | if (result != input) throw new StateError('$result != $input'); |
| 163 | }); |
| 164 | |
| 165 | await _test('testDouble', () async { |
| 166 | var input = 3.1415926; |
| 167 | var result = await client.testDouble(input); |
| 168 | if (result != input) throw new StateError('$result != $input'); |
| 169 | }); |
| 170 | |
| 171 | await _test('testBinary', () async { |
| 172 | var utf8Codec = const Utf8Codec(); |
| 173 | var input = utf8Codec.encode('foo'); |
| 174 | var result = await client.testBinary(input); |
| 175 | var equality = const ListEquality(); |
| 176 | if (!equality.equals( |
| 177 | result, input)) throw new StateError('$result != $input'); |
| 178 | }); |
| 179 | |
| 180 | await _test('testStruct', () async { |
| 181 | var result = await client.testStruct(xtruct); |
| 182 | if ('$result' != '$xtruct') throw new StateError('$result != $xtruct'); |
| 183 | }); |
| 184 | |
| 185 | await _test('testNest', () async { |
| 186 | var input = new Xtruct2() |
| 187 | ..byte_thing = 1 |
| 188 | ..struct_thing = xtruct |
| 189 | ..i32_thing = -3; |
| 190 | |
| 191 | var result = await client.testNest(input); |
| 192 | if ('$result' != '$input') throw new StateError('$result != $input'); |
| 193 | }); |
| 194 | |
| 195 | await _test('testMap', () async { |
| 196 | Map<int, int> input = {1: -10, 2: -9, 3: -8, 4: -7, 5: -6}; |
| 197 | |
| 198 | var result = await client.testMap(input); |
| 199 | var equality = const MapEquality(); |
| 200 | if (!equality.equals( |
| 201 | result, input)) throw new StateError('$result != $input'); |
| 202 | }); |
| 203 | |
| 204 | await _test('testSet', () async { |
| 205 | var input = new Set.from([-2, -1, 0, 1, 2]); |
| 206 | var result = await client.testSet(input); |
| 207 | var equality = const SetEquality(); |
| 208 | if (!equality.equals( |
| 209 | result, input)) throw new StateError('$result != $input'); |
| 210 | }); |
| 211 | |
| 212 | await _test('testList', () async { |
| 213 | var input = [-2, -1, 0, 1, 2]; |
| 214 | var result = await client.testList(input); |
| 215 | var equality = const ListEquality(); |
| 216 | if (!equality.equals( |
| 217 | result, input)) throw new StateError('$result != $input'); |
| 218 | }); |
| 219 | |
| 220 | await _test('testEnum', () async { |
| 221 | await _testEnum(Numberz.ONE); |
| 222 | await _testEnum(Numberz.TWO); |
| 223 | await _testEnum(Numberz.THREE); |
| 224 | await _testEnum(Numberz.FIVE); |
| 225 | await _testEnum(Numberz.EIGHT); |
| 226 | }); |
| 227 | |
| 228 | await _test('testTypedef', () async { |
| 229 | var input = 309858235082523; |
| 230 | var result = await client.testTypedef(input); |
| 231 | if (result != input) throw new StateError('$result != $input'); |
| 232 | }); |
| 233 | |
| 234 | await _test('testMapMap', () async { |
| 235 | Map<int, Map<int, int>> result = await client.testMapMap(1); |
| 236 | if (result.isEmpty || |
| 237 | result[result.keys.first].isEmpty) throw new StateError( |
| 238 | 'expected Map<int, Map<int, int>> got $result'); |
| 239 | }); |
| 240 | |
| 241 | await _test('testInsanity', () async { |
| 242 | var input = new Insanity(); |
| 243 | input.userMap = {Numberz.FIVE: 5000}; |
| 244 | input.xtructs = [xtruct]; |
| 245 | |
| 246 | Map<int, Map<int, Insanity>> result = await client.testInsanity(input); |
| 247 | if (result.isEmpty || |
| 248 | result[result.keys.first].isEmpty) throw new StateError( |
| 249 | 'expected Map<int, Map<int, Insanity>> got $result'); |
| 250 | }); |
| 251 | |
| 252 | await _test('testMulti', () async { |
| 253 | var input = new Xtruct() |
| 254 | ..string_thing = 'Hello2' |
| 255 | ..byte_thing = 123 |
| 256 | ..i32_thing = 456 |
| 257 | ..i64_thing = 789; |
| 258 | |
| 259 | var result = await client.testMulti(input.byte_thing, input.i32_thing, |
| 260 | input.i64_thing, {1: 'one'}, Numberz.EIGHT, 5678); |
| 261 | if ('$result' != '$input') throw new StateError('$result != $input'); |
| 262 | }); |
| 263 | |
| 264 | await _test('testException', () async { |
| 265 | try { |
| 266 | await client.testException('Xception'); |
| 267 | } on Xception catch (x) { |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | throw new StateError('expected an Xception'); |
| 272 | }); |
| 273 | |
| 274 | await _test('testMultiException', () async { |
| 275 | try { |
| 276 | await client.testMultiException('Xception2', 'foo'); |
| 277 | } on Xception2 catch (x) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | throw new StateError('expected an Xception2'); |
| 282 | }); |
| 283 | } |
| 284 | |
| 285 | Future _testEnum(int input) async { |
| 286 | var result = await client.testEnum(input); |
| 287 | if (result != input) throw new StateError('$result != $input'); |
| 288 | } |