blob: b3512958171bbc5cffb9205ad779121a1134bc39 [file] [log] [blame]
Jens Geyer5f9bdff2014-11-18 21:57:03 +01001/*
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
20package tests
21
22import (
23 "reflect"
24 "testing"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070025
26 "github.com/apache/thrift/lib/go/test/gopath/src/thrifttest"
Jens Geyer5f9bdff2014-11-18 21:57:03 +010027)
28
29type ThriftTestDriver struct {
taozle5c302e02017-07-23 15:21:44 +020030 client thrifttest.ThriftTest
Jens Geyer5f9bdff2014-11-18 21:57:03 +010031 t *testing.T
32}
33
taozle5c302e02017-07-23 15:21:44 +020034func NewThriftTestDriver(t *testing.T, client thrifttest.ThriftTest) *ThriftTestDriver {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010035 return &ThriftTestDriver{client, t}
36}
37
38func (p *ThriftTestDriver) Start() {
39 client := p.client
40 t := p.t
41
taozle5c302e02017-07-23 15:21:44 +020042 if client.TestVoid(defaultCtx) != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010043 t.Fatal("TestVoid failed")
44 }
45
taozle5c302e02017-07-23 15:21:44 +020046 if r, err := client.TestString(defaultCtx, "Test"); r != "Test" || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010047 t.Fatal("TestString with simple text failed")
48 }
49
taozle5c302e02017-07-23 15:21:44 +020050 if r, err := client.TestString(defaultCtx, ""); r != "" || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010051 t.Fatal("TestString with empty text failed")
52 }
53
54 stringTest := "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
55 "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
56 "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
57 "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
58 "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
59 "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
60 "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
61 "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
62 "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
63 "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
64 "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
65 "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
66 "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
67 "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
68 "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
69 "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪" +
70 "Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, " +
71 "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
72 "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
73 "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
74 "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
75 "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
76 "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
77 "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
78 "Bân-lâm-gú, 粵語"
79
taozle5c302e02017-07-23 15:21:44 +020080 if r, err := client.TestString(defaultCtx, stringTest); r != stringTest || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010081 t.Fatal("TestString with all languages failed")
82 }
83
84 specialCharacters := "quote: \" backslash:" +
85 " backspace: \b formfeed: \f newline: \n return: \r tab: " +
86 " now-all-of-them-together: '\\\b\n\r\t'" +
87 " now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><" +
88 " char-to-test-json-parsing: ]] \"]] \\\" }}}{ [[[ "
89
taozle5c302e02017-07-23 15:21:44 +020090 if r, err := client.TestString(defaultCtx, specialCharacters); r != specialCharacters || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010091 t.Fatal("TestString with specialCharacters failed")
92 }
93
taozle5c302e02017-07-23 15:21:44 +020094 if r, err := client.TestByte(defaultCtx, 1); r != 1 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010095 t.Fatal("TestByte(1) failed")
96 }
taozle5c302e02017-07-23 15:21:44 +020097 if r, err := client.TestByte(defaultCtx, 0); r != 0 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +010098 t.Fatal("TestByte(0) failed")
99 }
taozle5c302e02017-07-23 15:21:44 +0200100 if r, err := client.TestByte(defaultCtx, -1); r != -1 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100101 t.Fatal("TestByte(-1) failed")
102 }
taozle5c302e02017-07-23 15:21:44 +0200103 if r, err := client.TestByte(defaultCtx, -127); r != -127 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100104 t.Fatal("TestByte(-127) failed")
105 }
106
taozle5c302e02017-07-23 15:21:44 +0200107 if r, err := client.TestI32(defaultCtx, -1); r != -1 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100108 t.Fatal("TestI32(-1) failed")
109 }
taozle5c302e02017-07-23 15:21:44 +0200110 if r, err := client.TestI32(defaultCtx, 1); r != 1 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100111 t.Fatal("TestI32(1) failed")
112 }
113
taozle5c302e02017-07-23 15:21:44 +0200114 if r, err := client.TestI64(defaultCtx, -5); r != -5 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100115 t.Fatal("TestI64(-5) failed")
116 }
taozle5c302e02017-07-23 15:21:44 +0200117 if r, err := client.TestI64(defaultCtx, 5); r != 5 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100118 t.Fatal("TestI64(5) failed")
119 }
taozle5c302e02017-07-23 15:21:44 +0200120 if r, err := client.TestI64(defaultCtx, -34359738368); r != -34359738368 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100121 t.Fatal("TestI64(-34359738368) failed")
122 }
123
taozle5c302e02017-07-23 15:21:44 +0200124 if r, err := client.TestDouble(defaultCtx, -5.2098523); r != -5.2098523 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100125 t.Fatal("TestDouble(-5.2098523) failed")
126 }
taozle5c302e02017-07-23 15:21:44 +0200127 if r, err := client.TestDouble(defaultCtx, -7.012052175215044); r != -7.012052175215044 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100128 t.Fatal("TestDouble(-7.012052175215044) failed")
129 }
130
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100131 // TODO: add testBinary() call
132
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100133 out := thrifttest.NewXtruct()
134 out.StringThing = "Zero"
135 out.ByteThing = 1
136 out.I32Thing = -3
137 out.I64Thing = 1000000
taozle5c302e02017-07-23 15:21:44 +0200138 if r, err := client.TestStruct(defaultCtx, out); !reflect.DeepEqual(r, out) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100139 t.Fatal("TestStruct failed")
140 }
141
142 out2 := thrifttest.NewXtruct2()
143 out2.ByteThing = 1
144 out2.StructThing = out
145 out2.I32Thing = 5
taozle5c302e02017-07-23 15:21:44 +0200146 if r, err := client.TestNest(defaultCtx, out2); !reflect.DeepEqual(r, out2) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100147 t.Fatal("TestNest failed")
148 }
149
150 mapout := make(map[int32]int32)
151 for i := int32(0); i < 5; i++ {
152 mapout[i] = i - 10
153 }
taozle5c302e02017-07-23 15:21:44 +0200154 if r, err := client.TestMap(defaultCtx, mapout); !reflect.DeepEqual(r, mapout) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100155 t.Fatal("TestMap failed")
156 }
157
158 mapTestInput := map[string]string{
159 "a": "123", "a b": "with spaces ", "same": "same", "0": "numeric key",
160 "longValue": stringTest, stringTest: "long key",
161 }
taozle5c302e02017-07-23 15:21:44 +0200162 if r, err := client.TestStringMap(defaultCtx, mapTestInput); !reflect.DeepEqual(r, mapTestInput) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100163 t.Fatal("TestStringMap failed")
164 }
165
D. Can Celasun43fb34d2017-01-15 10:53:19 +0100166 setTestInput := []int32{1, 2, 3}
taozle5c302e02017-07-23 15:21:44 +0200167 if r, err := client.TestSet(defaultCtx, setTestInput); !reflect.DeepEqual(r, setTestInput) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100168 t.Fatal("TestSet failed")
169 }
170
171 listTest := []int32{1, 2, 3}
taozle5c302e02017-07-23 15:21:44 +0200172 if r, err := client.TestList(defaultCtx, listTest); !reflect.DeepEqual(r, listTest) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100173 t.Fatal("TestList failed")
174 }
175
taozle5c302e02017-07-23 15:21:44 +0200176 if r, err := client.TestEnum(defaultCtx, thrifttest.Numberz_ONE); r != thrifttest.Numberz_ONE || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100177 t.Fatal("TestEnum failed")
178 }
179
taozle5c302e02017-07-23 15:21:44 +0200180 if r, err := client.TestTypedef(defaultCtx, 69); r != 69 || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100181 t.Fatal("TestTypedef failed")
182 }
183
184 mapMapTest := map[int32]map[int32]int32{
185 4: {1: 1, 2: 2, 3: 3, 4: 4},
186 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
187 }
taozle5c302e02017-07-23 15:21:44 +0200188 if r, err := client.TestMapMap(defaultCtx, 1); !reflect.DeepEqual(r, mapMapTest) || err != nil {
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100189 t.Fatal("TestMapMap failed")
190 }
191
192 crazyX1 := thrifttest.NewXtruct()
193 crazyX1.StringThing = "Goodbye4"
194 crazyX1.ByteThing = 4
195 crazyX1.I32Thing = 4
196 crazyX1.I64Thing = 4
197
198 crazyX2 := thrifttest.NewXtruct()
199 crazyX2.StringThing = "Hello2"
200 crazyX2.ByteThing = 2
201 crazyX2.I32Thing = 2
202 crazyX2.I64Thing = 2
203
204 crazy := thrifttest.NewInsanity()
205 crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{5: 5, 8: 8}
206 crazy.Xtructs = []*thrifttest.Xtruct{crazyX1, crazyX2}
207
208 crazyEmpty := thrifttest.NewInsanity()
209 crazyEmpty.UserMap = map[thrifttest.Numberz]thrifttest.UserId{}
210 crazyEmpty.Xtructs = []*thrifttest.Xtruct{}
211
212 insanity := map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity{
213 1: {thrifttest.Numberz_TWO: crazy, thrifttest.Numberz_THREE: crazy},
214 2: {thrifttest.Numberz_SIX: crazyEmpty},
215 }
taozle5c302e02017-07-23 15:21:44 +0200216 if r, err := client.TestInsanity(defaultCtx, crazy); !reflect.DeepEqual(r, insanity) || err != nil {
Yuxuan 'fishy' Wang46530092019-11-05 13:31:13 -0800217 t.Fatal("TestInsanity failed:", err)
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100218 }
219
taozle5c302e02017-07-23 15:21:44 +0200220 if err := client.TestException(defaultCtx, "TException"); err == nil {
Yuxuan 'fishy' Wang46530092019-11-05 13:31:13 -0800221 t.Fatal("TestException TException failed:", err)
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100222 }
223
Yuxuan 'fishy' Wang46530092019-11-05 13:31:13 -0800224 err := client.TestException(defaultCtx, "Xception")
225 if e, ok := err.(*thrifttest.Xception); ok == false || e == nil {
226 t.Fatal("TestException Xception failed:", err)
227 } else if e.ErrorCode != 1001 || e.Message != "Xception" {
228 t.Fatal("TestException Xception failed:", e)
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100229 }
230
taozle5c302e02017-07-23 15:21:44 +0200231 if err := client.TestException(defaultCtx, "no Exception"); err != nil {
Yuxuan 'fishy' Wang46530092019-11-05 13:31:13 -0800232 t.Fatal("TestException no Exception failed:", err)
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100233 }
234
taozle5c302e02017-07-23 15:21:44 +0200235 if err := client.TestOneway(defaultCtx, 0); err != nil {
Yuxuan 'fishy' Wang46530092019-11-05 13:31:13 -0800236 t.Fatal("TestOneway failed:", err)
Jens Geyer5f9bdff2014-11-18 21:57:03 +0100237 }
238}