Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [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 | |
| 20 | package main |
| 21 | |
| 22 | import ( |
| 23 | "common" |
| 24 | "flag" |
| 25 | "gen/thrifttest" |
| 26 | t "log" |
| 27 | "reflect" |
| 28 | "thrift" |
| 29 | ) |
| 30 | |
| 31 | var host = flag.String("host", "localhost", "Host to connect") |
| 32 | var port = flag.Int64("port", 9090, "Port number to connect") |
| 33 | var domain_socket = flag.String("domain-socket", "", "Domain Socket (e.g. /tmp/thrifttest.thrift), instead of host and port") |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 34 | var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib") |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 35 | var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json") |
| 36 | var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") |
| 37 | var testloops = flag.Int("testloops", 1, "Number of Tests") |
| 38 | |
| 39 | func main() { |
| 40 | flag.Parse() |
| 41 | client, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl) |
| 42 | if err != nil { |
| 43 | t.Fatalf("Unable to start client: ", err) |
| 44 | } |
| 45 | for i := 0; i < *testloops; i++ { |
| 46 | callEverything(client) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | var rmapmap = map[int32]map[int32]int32{ |
| 51 | -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1}, |
| 52 | 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1}, |
| 53 | } |
| 54 | |
| 55 | var xxs = &thrifttest.Xtruct{ |
| 56 | StringThing: "Hello2", |
| 57 | ByteThing: 42, |
| 58 | I32Thing: 4242, |
| 59 | I64Thing: 424242, |
| 60 | } |
| 61 | |
| 62 | var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"} |
| 63 | |
| 64 | func callEverything(client *thrifttest.ThriftTestClient) { |
| 65 | var err error |
| 66 | if err = client.TestVoid(); err != nil { |
| 67 | t.Fatalf("Unexpected error in TestVoid() call: ", err) |
| 68 | } |
| 69 | |
| 70 | thing, err := client.TestString("thing") |
| 71 | if err != nil { |
| 72 | t.Fatalf("Unexpected error in TestString() call: ", err) |
| 73 | } |
| 74 | if thing != "thing" { |
| 75 | t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing) |
| 76 | } |
| 77 | |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame^] | 78 | bl, err := client.TestBool(true) |
| 79 | if err != nil { |
| 80 | t.Fatalf("Unexpected error in TestBool() call: ", err) |
| 81 | } |
| 82 | if !bl { |
| 83 | t.Fatalf("Unexpected TestBool() result expected true, got %f ", bl) |
| 84 | } |
| 85 | bl, err = client.TestBool(false) |
| 86 | if err != nil { |
| 87 | t.Fatalf("Unexpected error in TestBool() call: ", err) |
| 88 | } |
| 89 | if bl { |
| 90 | t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl) |
| 91 | } |
| 92 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 93 | b, err := client.TestByte(42) |
| 94 | if err != nil { |
| 95 | t.Fatalf("Unexpected error in TestByte() call: ", err) |
| 96 | } |
| 97 | if b != 42 { |
| 98 | t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b) |
| 99 | } |
| 100 | |
| 101 | i32, err := client.TestI32(4242) |
| 102 | if err != nil { |
| 103 | t.Fatalf("Unexpected error in TestI32() call: ", err) |
| 104 | } |
| 105 | if i32 != 4242 { |
| 106 | t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32) |
| 107 | } |
| 108 | |
| 109 | i64, err := client.TestI64(424242) |
| 110 | if err != nil { |
| 111 | t.Fatalf("Unexpected error in TestI64() call: ", err) |
| 112 | } |
| 113 | if i64 != 424242 { |
| 114 | t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64) |
| 115 | } |
| 116 | |
| 117 | d, err := client.TestDouble(42.42) |
| 118 | if err != nil { |
| 119 | t.Fatalf("Unexpected error in TestDouble() call: ", err) |
| 120 | } |
| 121 | if d != 42.42 { |
| 122 | t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d) |
| 123 | } |
| 124 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 125 | // TODO: add TestBinary() call |
| 126 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 127 | xs := thrifttest.NewXtruct() |
| 128 | xs.StringThing = "thing" |
| 129 | xs.ByteThing = 42 |
| 130 | xs.I32Thing = 4242 |
| 131 | xs.I64Thing = 424242 |
| 132 | xsret, err := client.TestStruct(xs) |
| 133 | if err != nil { |
| 134 | t.Fatalf("Unexpected error in TestStruct() call: ", err) |
| 135 | } |
| 136 | if *xs != *xsret { |
| 137 | t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret) |
| 138 | } |
| 139 | |
| 140 | x2 := thrifttest.NewXtruct2() |
| 141 | x2.StructThing = xs |
| 142 | x2ret, err := client.TestNest(x2) |
| 143 | if err != nil { |
| 144 | t.Fatalf("Unexpected error in TestNest() call: ", err) |
| 145 | } |
| 146 | if !reflect.DeepEqual(x2, x2ret) { |
| 147 | t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret) |
| 148 | } |
| 149 | |
| 150 | m := map[int32]int32{1: 2, 3: 4, 5: 42} |
| 151 | mret, err := client.TestMap(m) |
| 152 | if err != nil { |
| 153 | t.Fatalf("Unexpected error in TestMap() call: ", err) |
| 154 | } |
| 155 | if !reflect.DeepEqual(m, mret) { |
| 156 | t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret) |
| 157 | } |
| 158 | |
| 159 | sm := map[string]string{"a": "2", "b": "blah", "some": "thing"} |
| 160 | smret, err := client.TestStringMap(sm) |
| 161 | if err != nil { |
| 162 | t.Fatalf("Unexpected error in TestStringMap() call: ", err) |
| 163 | } |
| 164 | if !reflect.DeepEqual(sm, smret) { |
| 165 | t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret) |
| 166 | } |
| 167 | |
| 168 | s := map[int32]bool{1: true, 2: true, 42: true} |
| 169 | sret, err := client.TestSet(s) |
| 170 | if err != nil { |
| 171 | t.Fatalf("Unexpected error in TestSet() call: ", err) |
| 172 | } |
| 173 | if !reflect.DeepEqual(s, sret) { |
| 174 | t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) |
| 175 | } |
| 176 | |
| 177 | l := []int32{1, 2, 42} |
| 178 | lret, err := client.TestList(l) |
| 179 | if err != nil { |
| 180 | t.Fatalf("Unexpected error in TestList() call: ", err) |
| 181 | } |
| 182 | if !reflect.DeepEqual(l, lret) { |
| 183 | t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", l, lret) |
| 184 | } |
| 185 | |
| 186 | eret, err := client.TestEnum(thrifttest.Numberz_TWO) |
| 187 | if err != nil { |
| 188 | t.Fatalf("Unexpected error in TestEnum() call: ", err) |
| 189 | } |
| 190 | if eret != thrifttest.Numberz_TWO { |
| 191 | t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret) |
| 192 | } |
| 193 | |
| 194 | tret, err := client.TestTypedef(thrifttest.UserId(42)) |
| 195 | if err != nil { |
| 196 | t.Fatalf("Unexpected error in TestTypedef() call: ", err) |
| 197 | } |
| 198 | if tret != thrifttest.UserId(42) { |
| 199 | t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret) |
| 200 | } |
| 201 | |
| 202 | mapmap, err := client.TestMapMap(42) |
| 203 | if err != nil { |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame^] | 204 | t.Fatalf("Unexpected error in TestMapMap() call: ", err) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 205 | } |
| 206 | if !reflect.DeepEqual(mapmap, rmapmap) { |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame^] | 207 | t.Fatalf("Unexpected TestMapMap() result expected %#v, got %#v ", rmapmap, mapmap) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Nobuaki Sukegawa | 2fab3de | 2015-08-16 15:42:58 +0900 | [diff] [blame] | 210 | crazy := thrifttest.NewInsanity() |
| 211 | crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId { |
| 212 | thrifttest.Numberz_FIVE: 5, |
| 213 | thrifttest.Numberz_EIGHT: 8, |
| 214 | } |
| 215 | truck1 := thrifttest.NewXtruct() |
| 216 | truck1.StringThing = "Goodbye4" |
| 217 | truck1.ByteThing = 4; |
| 218 | truck1.I32Thing = 4; |
| 219 | truck1.I64Thing = 4; |
| 220 | truck2 := thrifttest.NewXtruct() |
| 221 | truck2.StringThing = "Hello2" |
| 222 | truck2.ByteThing = 2; |
| 223 | truck2.I32Thing = 2; |
| 224 | truck2.I64Thing = 2; |
| 225 | crazy.Xtructs = []*thrifttest.Xtruct { |
| 226 | truck1, |
| 227 | truck2, |
| 228 | } |
| 229 | insanity, err := client.TestInsanity(crazy) |
| 230 | if err != nil { |
| 231 | t.Fatalf("Unexpected error in TestInsanity() call: ", err) |
| 232 | } |
| 233 | if !reflect.DeepEqual(crazy, insanity[1][2]) { |
| 234 | t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ", |
| 235 | crazy, |
| 236 | insanity[1][2]) |
| 237 | } |
| 238 | if !reflect.DeepEqual(crazy, insanity[1][3]) { |
| 239 | t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ", |
| 240 | crazy, |
| 241 | insanity[1][3]) |
| 242 | } |
| 243 | if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 { |
| 244 | t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ", |
| 245 | insanity[2][6]) |
| 246 | } |
| 247 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 248 | xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24)) |
| 249 | if err != nil { |
| 250 | t.Fatalf("Unexpected error in TestMulti() call: ", err) |
| 251 | } |
| 252 | if !reflect.DeepEqual(xxs, xxsret) { |
| 253 | t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret) |
| 254 | } |
| 255 | |
| 256 | err = client.TestException("Xception") |
| 257 | if err == nil { |
| 258 | t.Fatalf("Expecting exception in TestException() call") |
| 259 | } |
| 260 | if !reflect.DeepEqual(err, xcept) { |
| 261 | t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err) |
| 262 | } |
| 263 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 264 | err = client.TestException("TException") |
Nobuaki Sukegawa | 2fab3de | 2015-08-16 15:42:58 +0900 | [diff] [blame] | 265 | _, ok := err.(thrift.TApplicationException) |
| 266 | if err == nil || !ok { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 267 | t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err) |
| 268 | } |
| 269 | |
| 270 | ign, err := client.TestMultiException("Xception", "ignoreme") |
| 271 | if ign != nil || err == nil { |
| 272 | t.Fatalf("Expecting exception in TestMultiException() call") |
| 273 | } |
| 274 | if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) { |
| 275 | t.Fatalf("Unexpected TestMultiException() %#v ", err) |
| 276 | } |
| 277 | |
| 278 | ign, err = client.TestMultiException("Xception2", "ignoreme") |
| 279 | if ign != nil || err == nil { |
| 280 | t.Fatalf("Expecting exception in TestMultiException() call") |
| 281 | } |
| 282 | expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}} |
| 283 | |
| 284 | if !reflect.DeepEqual(err, expecting) { |
| 285 | t.Fatalf("Unexpected TestMultiException() %#v ", err) |
| 286 | } |
| 287 | |
| 288 | err = client.TestOneway(2) |
| 289 | if err != nil { |
| 290 | t.Fatalf("Unexpected error in TestOneway() call: ", err) |
| 291 | } |
| 292 | |
| 293 | //Make sure the connection still alive |
| 294 | if err = client.TestVoid(); err != nil { |
| 295 | t.Fatalf("Unexpected error in TestVoid() call: ", err) |
| 296 | } |
| 297 | } |