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") |
| 34 | var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http") |
| 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 | |
| 78 | b, err := client.TestByte(42) |
| 79 | if err != nil { |
| 80 | t.Fatalf("Unexpected error in TestByte() call: ", err) |
| 81 | } |
| 82 | if b != 42 { |
| 83 | t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b) |
| 84 | } |
| 85 | |
| 86 | i32, err := client.TestI32(4242) |
| 87 | if err != nil { |
| 88 | t.Fatalf("Unexpected error in TestI32() call: ", err) |
| 89 | } |
| 90 | if i32 != 4242 { |
| 91 | t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32) |
| 92 | } |
| 93 | |
| 94 | i64, err := client.TestI64(424242) |
| 95 | if err != nil { |
| 96 | t.Fatalf("Unexpected error in TestI64() call: ", err) |
| 97 | } |
| 98 | if i64 != 424242 { |
| 99 | t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64) |
| 100 | } |
| 101 | |
| 102 | d, err := client.TestDouble(42.42) |
| 103 | if err != nil { |
| 104 | t.Fatalf("Unexpected error in TestDouble() call: ", err) |
| 105 | } |
| 106 | if d != 42.42 { |
| 107 | t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d) |
| 108 | } |
| 109 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 110 | // TODO: add TestBinary() call |
| 111 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 112 | xs := thrifttest.NewXtruct() |
| 113 | xs.StringThing = "thing" |
| 114 | xs.ByteThing = 42 |
| 115 | xs.I32Thing = 4242 |
| 116 | xs.I64Thing = 424242 |
| 117 | xsret, err := client.TestStruct(xs) |
| 118 | if err != nil { |
| 119 | t.Fatalf("Unexpected error in TestStruct() call: ", err) |
| 120 | } |
| 121 | if *xs != *xsret { |
| 122 | t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret) |
| 123 | } |
| 124 | |
| 125 | x2 := thrifttest.NewXtruct2() |
| 126 | x2.StructThing = xs |
| 127 | x2ret, err := client.TestNest(x2) |
| 128 | if err != nil { |
| 129 | t.Fatalf("Unexpected error in TestNest() call: ", err) |
| 130 | } |
| 131 | if !reflect.DeepEqual(x2, x2ret) { |
| 132 | t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret) |
| 133 | } |
| 134 | |
| 135 | m := map[int32]int32{1: 2, 3: 4, 5: 42} |
| 136 | mret, err := client.TestMap(m) |
| 137 | if err != nil { |
| 138 | t.Fatalf("Unexpected error in TestMap() call: ", err) |
| 139 | } |
| 140 | if !reflect.DeepEqual(m, mret) { |
| 141 | t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret) |
| 142 | } |
| 143 | |
| 144 | sm := map[string]string{"a": "2", "b": "blah", "some": "thing"} |
| 145 | smret, err := client.TestStringMap(sm) |
| 146 | if err != nil { |
| 147 | t.Fatalf("Unexpected error in TestStringMap() call: ", err) |
| 148 | } |
| 149 | if !reflect.DeepEqual(sm, smret) { |
| 150 | t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret) |
| 151 | } |
| 152 | |
| 153 | s := map[int32]bool{1: true, 2: true, 42: true} |
| 154 | sret, err := client.TestSet(s) |
| 155 | if err != nil { |
| 156 | t.Fatalf("Unexpected error in TestSet() call: ", err) |
| 157 | } |
| 158 | if !reflect.DeepEqual(s, sret) { |
| 159 | t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) |
| 160 | } |
| 161 | |
| 162 | l := []int32{1, 2, 42} |
| 163 | lret, err := client.TestList(l) |
| 164 | if err != nil { |
| 165 | t.Fatalf("Unexpected error in TestList() call: ", err) |
| 166 | } |
| 167 | if !reflect.DeepEqual(l, lret) { |
| 168 | t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", l, lret) |
| 169 | } |
| 170 | |
| 171 | eret, err := client.TestEnum(thrifttest.Numberz_TWO) |
| 172 | if err != nil { |
| 173 | t.Fatalf("Unexpected error in TestEnum() call: ", err) |
| 174 | } |
| 175 | if eret != thrifttest.Numberz_TWO { |
| 176 | t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret) |
| 177 | } |
| 178 | |
| 179 | tret, err := client.TestTypedef(thrifttest.UserId(42)) |
| 180 | if err != nil { |
| 181 | t.Fatalf("Unexpected error in TestTypedef() call: ", err) |
| 182 | } |
| 183 | if tret != thrifttest.UserId(42) { |
| 184 | t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret) |
| 185 | } |
| 186 | |
| 187 | mapmap, err := client.TestMapMap(42) |
| 188 | if err != nil { |
| 189 | t.Fatalf("Unexpected error in TestMapmap() call: ", err) |
| 190 | } |
| 191 | if !reflect.DeepEqual(mapmap, rmapmap) { |
| 192 | t.Fatalf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap) |
| 193 | } |
| 194 | |
| 195 | xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24)) |
| 196 | if err != nil { |
| 197 | t.Fatalf("Unexpected error in TestMulti() call: ", err) |
| 198 | } |
| 199 | if !reflect.DeepEqual(xxs, xxsret) { |
| 200 | t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret) |
| 201 | } |
| 202 | |
| 203 | err = client.TestException("Xception") |
| 204 | if err == nil { |
| 205 | t.Fatalf("Expecting exception in TestException() call") |
| 206 | } |
| 207 | if !reflect.DeepEqual(err, xcept) { |
| 208 | t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err) |
| 209 | } |
| 210 | |
| 211 | // TODO: connection is being closed on this |
| 212 | err = client.TestException("TException") |
| 213 | tex, ok := err.(thrift.TApplicationException) |
| 214 | if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR { |
| 215 | t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err) |
| 216 | } |
| 217 | |
| 218 | ign, err := client.TestMultiException("Xception", "ignoreme") |
| 219 | if ign != nil || err == nil { |
| 220 | t.Fatalf("Expecting exception in TestMultiException() call") |
| 221 | } |
| 222 | if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) { |
| 223 | t.Fatalf("Unexpected TestMultiException() %#v ", err) |
| 224 | } |
| 225 | |
| 226 | ign, err = client.TestMultiException("Xception2", "ignoreme") |
| 227 | if ign != nil || err == nil { |
| 228 | t.Fatalf("Expecting exception in TestMultiException() call") |
| 229 | } |
| 230 | expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}} |
| 231 | |
| 232 | if !reflect.DeepEqual(err, expecting) { |
| 233 | t.Fatalf("Unexpected TestMultiException() %#v ", err) |
| 234 | } |
| 235 | |
| 236 | err = client.TestOneway(2) |
| 237 | if err != nil { |
| 238 | t.Fatalf("Unexpected error in TestOneway() call: ", err) |
| 239 | } |
| 240 | |
| 241 | //Make sure the connection still alive |
| 242 | if err = client.TestVoid(); err != nil { |
| 243 | t.Fatalf("Unexpected error in TestVoid() call: ", err) |
| 244 | } |
| 245 | } |