Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | var thrift = require('thrift'); |
| 20 | |
| 21 | var ThriftTest = require('./gen-nodejs/ThriftTest'), |
| 22 | ttypes = require('./gen-nodejs/ThriftTest_types'); |
| 23 | |
| 24 | var connection = thrift.createConnection('localhost', 9090), |
| 25 | client = thrift.createClient(ThriftTest, connection); |
| 26 | |
| 27 | var tfailed = 0; |
| 28 | var tpassed = 0; |
| 29 | |
| 30 | function failed(err) { |
| 31 | console.trace(err); |
| 32 | return tfailed++; |
| 33 | } |
| 34 | function passed() { |
| 35 | return tpassed++; |
| 36 | } |
| 37 | |
| 38 | connection.on('error', function(err) { |
| 39 | failed(err); |
| 40 | }); |
| 41 | |
| 42 | console.time("Tests completed in"); |
| 43 | |
| 44 | client.testVoid(function(err, response) { |
| 45 | if (err) { return failed(err); } |
| 46 | console.log("testVoid() = ", response); |
| 47 | passed(); |
| 48 | }); |
| 49 | |
| 50 | client.testString("Test", function(err, response) { |
| 51 | if (err) { return failed(err); } |
| 52 | console.log("testString('Test') = ", response); |
| 53 | passed(); |
| 54 | }); |
| 55 | |
| 56 | client.testByte(1, function(err, response) { |
| 57 | if (err) { return failed(err); } |
| 58 | console.log("testByte(1) = ", response); |
| 59 | passed(); |
| 60 | }); |
| 61 | |
| 62 | client.testI32(-1, function(err, response) { |
| 63 | if (err) { return failed(err); } |
| 64 | console.log("testI32(-1) = ", response); |
| 65 | passed(); |
| 66 | }); |
| 67 | |
| 68 | client.testI64(5, function(err, response) { |
| 69 | if (err) { return failed(err); } |
| 70 | console.log("testI64(5) = ", response); |
| 71 | passed(); |
| 72 | }); |
| 73 | |
| 74 | /* |
| 75 | * FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory |
| 76 | client.testI64(-5, function(err, response) { |
| 77 | if (err) { return failed(err); } |
| 78 | console.log("testI64(-5) = ", response); |
| 79 | passed(); |
| 80 | }); |
| 81 | */ |
| 82 | |
| 83 | client.testI64(-34359738368, function(err, response) { |
| 84 | if (err) { return failed(err); } |
| 85 | console.log("testI64(-34359738368) = ", response); |
| 86 | passed(); |
| 87 | }); |
| 88 | |
| 89 | client.testDouble(-5.2098523, function(err, response) { |
| 90 | if (err) { return failed(err); } |
| 91 | console.log("testDouble(-5.2098523) = ", response); |
| 92 | passed(); |
| 93 | }); |
| 94 | |
| 95 | var out = new ttypes.Xtruct({ |
| 96 | string_thing: 'Zero', |
| 97 | byte_thing: 1, |
| 98 | i32_thing: -3, |
| 99 | i64_thing: 1000000 |
| 100 | }); |
| 101 | client.testStruct(out, function(err, response) { |
| 102 | if (err) { return failed(err); } |
| 103 | console.log("testStruct(", out, ") = \n", response); |
| 104 | passed(); |
| 105 | }); |
| 106 | |
| 107 | var out2 = new ttypes.Xtruct2(); |
| 108 | out2.byte_thing = 1; |
| 109 | out2.struct_thing = out; |
| 110 | out2.i32_thing = 5; |
| 111 | client.testNest(out2, function(err, response) { |
| 112 | if (err) { return failed(err); } |
| 113 | console.log("testNest(", out2, ") = \n", response); |
| 114 | passed(); |
| 115 | }); |
| 116 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 117 | var mapout = {}; |
| 118 | for (var i = 0; i < 5; ++i) { |
| 119 | mapout[i] = i-10; |
| 120 | } |
| 121 | client.testMap(mapout, function(err, response) { |
| 122 | if (err) { return failed(err); } |
| 123 | console.log("testMap(", mapout, ") = \n", response); |
| 124 | passed(); |
| 125 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * TODO: testSet, testList, testEnum, testTypedef, testMapMap, testInsanity |
| 129 | */ |
| 130 | |
| 131 | |
| 132 | client.testException('ApplicationException', function(err, response) { |
| 133 | console.log("testException('ApplicationException') = ", err); |
| 134 | if (response) { return failed(response); } |
| 135 | passed(); |
| 136 | }); |
| 137 | |
| 138 | client.testException('Xception', function(err, response) { |
| 139 | console.log("testException('Xception') = ", err); |
| 140 | if (response) { return failed(response); } |
| 141 | passed(); |
| 142 | }); |
| 143 | |
| 144 | client.testException('success', function(err, response) { |
| 145 | if (err) { return failed(err); } |
| 146 | console.log("testException('success') = ", response); |
| 147 | passed(); |
| 148 | }); |
| 149 | |
| 150 | setTimeout(function(){ |
| 151 | console.timeEnd("Tests completed in"); |
| 152 | console.log(tpassed + " passed, " + tfailed + " failed"); |
| 153 | connection.end(); |
| 154 | }, 200); |