]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/go/test/tests/client_error_test.go
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / go / test / tests / client_error_test.go
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 tests
21
22 import (
23 "context"
24 "errors"
25 "errortest"
26 "testing"
27 "thrift"
28
29 "github.com/golang/mock/gomock"
30 )
31
32 // TestCase: Comprehensive call and reply workflow in the client.
33 // Setup mock to fail at a certain position. Return true if position exists otherwise false.
34 func prepareClientCallReply(protocol *MockTProtocol, failAt int, failWith error) bool {
35 var err error = nil
36
37 if failAt == 0 {
38 err = failWith
39 }
40 last := protocol.EXPECT().WriteMessageBegin("testStruct", thrift.CALL, int32(1)).Return(err)
41 if failAt == 0 {
42 return true
43 }
44 if failAt == 1 {
45 err = failWith
46 }
47 last = protocol.EXPECT().WriteStructBegin("testStruct_args").Return(err).After(last)
48 if failAt == 1 {
49 return true
50 }
51 if failAt == 2 {
52 err = failWith
53 }
54 last = protocol.EXPECT().WriteFieldBegin("thing", thrift.TType(thrift.STRUCT), int16(1)).Return(err).After(last)
55 if failAt == 2 {
56 return true
57 }
58 if failAt == 3 {
59 err = failWith
60 }
61 last = protocol.EXPECT().WriteStructBegin("TestStruct").Return(err).After(last)
62 if failAt == 3 {
63 return true
64 }
65 if failAt == 4 {
66 err = failWith
67 }
68 last = protocol.EXPECT().WriteFieldBegin("m", thrift.TType(thrift.MAP), int16(1)).Return(err).After(last)
69 if failAt == 4 {
70 return true
71 }
72 if failAt == 5 {
73 err = failWith
74 }
75 last = protocol.EXPECT().WriteMapBegin(thrift.TType(thrift.STRING), thrift.TType(thrift.STRING), 0).Return(err).After(last)
76 if failAt == 5 {
77 return true
78 }
79 if failAt == 6 {
80 err = failWith
81 }
82 last = protocol.EXPECT().WriteMapEnd().Return(err).After(last)
83 if failAt == 6 {
84 return true
85 }
86 if failAt == 7 {
87 err = failWith
88 }
89 last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last)
90 if failAt == 7 {
91 return true
92 }
93 if failAt == 8 {
94 err = failWith
95 }
96 last = protocol.EXPECT().WriteFieldBegin("l", thrift.TType(thrift.LIST), int16(2)).Return(err).After(last)
97 if failAt == 8 {
98 return true
99 }
100 if failAt == 9 {
101 err = failWith
102 }
103 last = protocol.EXPECT().WriteListBegin(thrift.TType(thrift.STRING), 0).Return(err).After(last)
104 if failAt == 9 {
105 return true
106 }
107 if failAt == 10 {
108 err = failWith
109 }
110 last = protocol.EXPECT().WriteListEnd().Return(err).After(last)
111 if failAt == 10 {
112 return true
113 }
114 if failAt == 11 {
115 err = failWith
116 }
117 last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last)
118 if failAt == 11 {
119 return true
120 }
121 if failAt == 12 {
122 err = failWith
123 }
124
125 last = protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.SET), int16(3)).Return(err).After(last)
126 if failAt == 12 {
127 return true
128 }
129 if failAt == 13 {
130 err = failWith
131 }
132 last = protocol.EXPECT().WriteSetBegin(thrift.TType(thrift.STRING), 0).Return(err).After(last)
133 if failAt == 13 {
134 return true
135 }
136 if failAt == 14 {
137 err = failWith
138 }
139 last = protocol.EXPECT().WriteSetEnd().Return(err).After(last)
140 if failAt == 14 {
141 return true
142 }
143 if failAt == 15 {
144 err = failWith
145 }
146 last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last)
147 if failAt == 15 {
148 return true
149 }
150 if failAt == 16 {
151 err = failWith
152 }
153 last = protocol.EXPECT().WriteFieldBegin("i", thrift.TType(thrift.I32), int16(4)).Return(err).After(last)
154 if failAt == 16 {
155 return true
156 }
157 if failAt == 17 {
158 err = failWith
159 }
160 last = protocol.EXPECT().WriteI32(int32(3)).Return(err).After(last)
161 if failAt == 17 {
162 return true
163 }
164 if failAt == 18 {
165 err = failWith
166 }
167 last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last)
168 if failAt == 18 {
169 return true
170 }
171 if failAt == 19 {
172 err = failWith
173 }
174 last = protocol.EXPECT().WriteFieldStop().Return(err).After(last)
175 if failAt == 19 {
176 return true
177 }
178 if failAt == 20 {
179 err = failWith
180 }
181 last = protocol.EXPECT().WriteStructEnd().Return(err).After(last)
182 if failAt == 20 {
183 return true
184 }
185 if failAt == 21 {
186 err = failWith
187 }
188 last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last)
189 if failAt == 21 {
190 return true
191 }
192 if failAt == 22 {
193 err = failWith
194 }
195 last = protocol.EXPECT().WriteFieldStop().Return(err).After(last)
196 if failAt == 22 {
197 return true
198 }
199 if failAt == 23 {
200 err = failWith
201 }
202 last = protocol.EXPECT().WriteStructEnd().Return(err).After(last)
203 if failAt == 23 {
204 return true
205 }
206 if failAt == 24 {
207 err = failWith
208 }
209 last = protocol.EXPECT().WriteMessageEnd().Return(err).After(last)
210 if failAt == 24 {
211 return true
212 }
213 if failAt == 25 {
214 err = failWith
215 }
216 last = protocol.EXPECT().Flush(context.Background()).Return(err).After(last)
217 if failAt == 25 {
218 return true
219 }
220 if failAt == 26 {
221 err = failWith
222 }
223 last = protocol.EXPECT().ReadMessageBegin().Return("testStruct", thrift.REPLY, int32(1), err).After(last)
224 if failAt == 26 {
225 return true
226 }
227 if failAt == 27 {
228 err = failWith
229 }
230 last = protocol.EXPECT().ReadStructBegin().Return("testStruct_args", err).After(last)
231 if failAt == 27 {
232 return true
233 }
234 if failAt == 28 {
235 err = failWith
236 }
237 last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STRUCT), int16(0), err).After(last)
238 if failAt == 28 {
239 return true
240 }
241 if failAt == 29 {
242 err = failWith
243 }
244 last = protocol.EXPECT().ReadStructBegin().Return("TestStruct", err).After(last)
245 if failAt == 29 {
246 return true
247 }
248 if failAt == 30 {
249 err = failWith
250 }
251 last = protocol.EXPECT().ReadFieldBegin().Return("m", thrift.TType(thrift.MAP), int16(1), err).After(last)
252 if failAt == 30 {
253 return true
254 }
255 if failAt == 31 {
256 err = failWith
257 }
258 last = protocol.EXPECT().ReadMapBegin().Return(thrift.TType(thrift.STRING), thrift.TType(thrift.STRING), 0, err).After(last)
259 if failAt == 31 {
260 return true
261 }
262 if failAt == 32 {
263 err = failWith
264 }
265 last = protocol.EXPECT().ReadMapEnd().Return(err).After(last)
266 if failAt == 32 {
267 return true
268 }
269 if failAt == 33 {
270 err = failWith
271 }
272 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
273 if failAt == 33 {
274 return true
275 }
276 if failAt == 34 {
277 err = failWith
278 }
279 last = protocol.EXPECT().ReadFieldBegin().Return("l", thrift.TType(thrift.LIST), int16(2), err).After(last)
280 if failAt == 34 {
281 return true
282 }
283 if failAt == 35 {
284 err = failWith
285 }
286 last = protocol.EXPECT().ReadListBegin().Return(thrift.TType(thrift.STRING), 0, err).After(last)
287 if failAt == 35 {
288 return true
289 }
290 if failAt == 36 {
291 err = failWith
292 }
293 last = protocol.EXPECT().ReadListEnd().Return(err).After(last)
294 if failAt == 36 {
295 return true
296 }
297 if failAt == 37 {
298 err = failWith
299 }
300 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
301 if failAt == 37 {
302 return true
303 }
304 if failAt == 38 {
305 err = failWith
306 }
307 last = protocol.EXPECT().ReadFieldBegin().Return("s", thrift.TType(thrift.SET), int16(3), err).After(last)
308 if failAt == 38 {
309 return true
310 }
311 if failAt == 39 {
312 err = failWith
313 }
314 last = protocol.EXPECT().ReadSetBegin().Return(thrift.TType(thrift.STRING), 0, err).After(last)
315 if failAt == 39 {
316 return true
317 }
318 if failAt == 40 {
319 err = failWith
320 }
321 last = protocol.EXPECT().ReadSetEnd().Return(err).After(last)
322 if failAt == 40 {
323 return true
324 }
325 if failAt == 41 {
326 err = failWith
327 }
328 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
329 if failAt == 41 {
330 return true
331 }
332 if failAt == 42 {
333 err = failWith
334 }
335 last = protocol.EXPECT().ReadFieldBegin().Return("i", thrift.TType(thrift.I32), int16(4), err).After(last)
336 if failAt == 42 {
337 return true
338 }
339 if failAt == 43 {
340 err = failWith
341 }
342 last = protocol.EXPECT().ReadI32().Return(int32(3), err).After(last)
343 if failAt == 43 {
344 return true
345 }
346 if failAt == 44 {
347 err = failWith
348 }
349 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
350 if failAt == 44 {
351 return true
352 }
353 if failAt == 45 {
354 err = failWith
355 }
356 last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(5), err).After(last)
357 if failAt == 45 {
358 return true
359 }
360 if failAt == 46 {
361 err = failWith
362 }
363 last = protocol.EXPECT().ReadStructEnd().Return(err).After(last)
364 if failAt == 46 {
365 return true
366 }
367 if failAt == 47 {
368 err = failWith
369 }
370 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
371 if failAt == 47 {
372 return true
373 }
374 if failAt == 48 {
375 err = failWith
376 }
377 last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), err).After(last)
378 if failAt == 48 {
379 return true
380 }
381 if failAt == 49 {
382 err = failWith
383 }
384 last = protocol.EXPECT().ReadStructEnd().Return(err).After(last)
385 if failAt == 49 {
386 return true
387 }
388 if failAt == 50 {
389 err = failWith
390 }
391 last = protocol.EXPECT().ReadMessageEnd().Return(err).After(last)
392 if failAt == 50 {
393 return true
394 }
395 return false
396 }
397
398 // TestCase: Comprehensive call and reply workflow in the client.
399 // Expecting TTransportError on fail.
400 func TestClientReportTTransportErrors(t *testing.T) {
401 mockCtrl := gomock.NewController(t)
402
403 thing := errortest.NewTestStruct()
404 thing.M = make(map[string]string)
405 thing.L = make([]string, 0)
406 thing.S = make([]string, 0)
407 thing.I = 3
408
409 err := thrift.NewTTransportException(thrift.TIMED_OUT, "test")
410 for i := 0; ; i++ {
411 protocol := NewMockTProtocol(mockCtrl)
412 if !prepareClientCallReply(protocol, i, err) {
413 return
414 }
415 client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol))
416 _, retErr := client.TestStruct(defaultCtx, thing)
417 mockCtrl.Finish()
418 mockCtrl = gomock.NewController(t)
419 err2, ok := retErr.(thrift.TTransportException)
420 if !ok {
421 t.Fatal("Expected a TTrasportException")
422 }
423
424 if err2.TypeId() != thrift.TIMED_OUT {
425 t.Fatal("Expected TIMED_OUT error")
426 }
427 }
428 }
429
430 // TestCase: Comprehensive call and reply workflow in the client.
431 // Expecting TTransportError on fail.
432 // Similar to TestClientReportTTransportErrors, but using legacy client constructor.
433 func TestClientReportTTransportErrorsLegacy(t *testing.T) {
434 mockCtrl := gomock.NewController(t)
435 transport := thrift.NewTMemoryBuffer()
436 thing := errortest.NewTestStruct()
437 thing.M = make(map[string]string)
438 thing.L = make([]string, 0)
439 thing.S = make([]string, 0)
440 thing.I = 3
441
442 err := thrift.NewTTransportException(thrift.TIMED_OUT, "test")
443 for i := 0; ; i++ {
444 protocol := NewMockTProtocol(mockCtrl)
445 if !prepareClientCallReply(protocol, i, err) {
446 return
447 }
448 client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
449 _, retErr := client.TestStruct(defaultCtx, thing)
450 mockCtrl.Finish()
451 mockCtrl = gomock.NewController(t)
452 err2, ok := retErr.(thrift.TTransportException)
453 if !ok {
454 t.Fatal("Expected a TTrasportException")
455 }
456
457 if err2.TypeId() != thrift.TIMED_OUT {
458 t.Fatal("Expected TIMED_OUT error")
459 }
460 }
461 }
462
463 // TestCase: Comprehensive call and reply workflow in the client.
464 // Expecting TTProtocolErrors on fail.
465 func TestClientReportTProtocolErrors(t *testing.T) {
466 mockCtrl := gomock.NewController(t)
467
468 thing := errortest.NewTestStruct()
469 thing.M = make(map[string]string)
470 thing.L = make([]string, 0)
471 thing.S = make([]string, 0)
472 thing.I = 3
473
474 err := thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, errors.New("test"))
475 for i := 0; ; i++ {
476 protocol := NewMockTProtocol(mockCtrl)
477 if !prepareClientCallReply(protocol, i, err) {
478 return
479 }
480 client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol))
481 _, retErr := client.TestStruct(defaultCtx, thing)
482 mockCtrl.Finish()
483 mockCtrl = gomock.NewController(t)
484 err2, ok := retErr.(thrift.TProtocolException)
485 if !ok {
486 t.Fatal("Expected a TProtocolException")
487 }
488 if err2.TypeId() != thrift.INVALID_DATA {
489 t.Fatal("Expected INVALID_DATA error")
490 }
491 }
492 }
493
494 // TestCase: Comprehensive call and reply workflow in the client.
495 // Expecting TTProtocolErrors on fail.
496 // Similar to TestClientReportTProtocolErrors, but using legacy client constructor.
497 func TestClientReportTProtocolErrorsLegacy(t *testing.T) {
498 mockCtrl := gomock.NewController(t)
499 transport := thrift.NewTMemoryBuffer()
500 thing := errortest.NewTestStruct()
501 thing.M = make(map[string]string)
502 thing.L = make([]string, 0)
503 thing.S = make([]string, 0)
504 thing.I = 3
505
506 err := thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, errors.New("test"))
507 for i := 0; ; i++ {
508 protocol := NewMockTProtocol(mockCtrl)
509 if !prepareClientCallReply(protocol, i, err) {
510 return
511 }
512 client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
513 _, retErr := client.TestStruct(defaultCtx, thing)
514 mockCtrl.Finish()
515 mockCtrl = gomock.NewController(t)
516 err2, ok := retErr.(thrift.TProtocolException)
517 if !ok {
518 t.Fatal("Expected a TProtocolException")
519 }
520 if err2.TypeId() != thrift.INVALID_DATA {
521 t.Fatal("Expected INVALID_DATA error")
522 }
523 }
524 }
525
526 // TestCase: call and reply with exception workflow in the client.
527 // Setup mock to fail at a certain position. Return true if position exists otherwise false.
528 func prepareClientCallException(protocol *MockTProtocol, failAt int, failWith error) bool {
529 var err error = nil
530
531 // No need to test failure in this block, because it is covered in other test cases
532 last := protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1))
533 last = protocol.EXPECT().WriteStructBegin("testString_args").After(last)
534 last = protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)).After(last)
535 last = protocol.EXPECT().WriteString("test").After(last)
536 last = protocol.EXPECT().WriteFieldEnd().After(last)
537 last = protocol.EXPECT().WriteFieldStop().After(last)
538 last = protocol.EXPECT().WriteStructEnd().After(last)
539 last = protocol.EXPECT().WriteMessageEnd().After(last)
540 last = protocol.EXPECT().Flush(context.Background()).After(last)
541
542 // Reading the exception, might fail.
543 if failAt == 0 {
544 err = failWith
545 }
546 last = protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.EXCEPTION, int32(1), err).After(last)
547 if failAt == 0 {
548 return true
549 }
550 if failAt == 1 {
551 err = failWith
552 }
553 last = protocol.EXPECT().ReadStructBegin().Return("TApplicationException", err).After(last)
554 if failAt == 1 {
555 return true
556 }
557 if failAt == 2 {
558 err = failWith
559 }
560 last = protocol.EXPECT().ReadFieldBegin().Return("message", thrift.TType(thrift.STRING), int16(1), err).After(last)
561 if failAt == 2 {
562 return true
563 }
564 if failAt == 3 {
565 err = failWith
566 }
567 last = protocol.EXPECT().ReadString().Return("test", err).After(last)
568 if failAt == 3 {
569 return true
570 }
571 if failAt == 4 {
572 err = failWith
573 }
574 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
575 if failAt == 4 {
576 return true
577 }
578 if failAt == 5 {
579 err = failWith
580 }
581 last = protocol.EXPECT().ReadFieldBegin().Return("type", thrift.TType(thrift.I32), int16(2), err).After(last)
582 if failAt == 5 {
583 return true
584 }
585 if failAt == 6 {
586 err = failWith
587 }
588 last = protocol.EXPECT().ReadI32().Return(int32(thrift.PROTOCOL_ERROR), err).After(last)
589 if failAt == 6 {
590 return true
591 }
592 if failAt == 7 {
593 err = failWith
594 }
595 last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last)
596 if failAt == 7 {
597 return true
598 }
599 if failAt == 8 {
600 err = failWith
601 }
602 last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(2), err).After(last)
603 if failAt == 8 {
604 return true
605 }
606 if failAt == 9 {
607 err = failWith
608 }
609 last = protocol.EXPECT().ReadStructEnd().Return(err).After(last)
610 if failAt == 9 {
611 return true
612 }
613 if failAt == 10 {
614 err = failWith
615 }
616 last = protocol.EXPECT().ReadMessageEnd().Return(err).After(last)
617 if failAt == 10 {
618 return true
619 }
620
621 return false
622 }
623
624 // TestCase: call and reply with exception workflow in the client.
625 func TestClientCallException(t *testing.T) {
626 mockCtrl := gomock.NewController(t)
627
628 err := thrift.NewTTransportException(thrift.TIMED_OUT, "test")
629 for i := 0; ; i++ {
630 protocol := NewMockTProtocol(mockCtrl)
631 willComplete := !prepareClientCallException(protocol, i, err)
632
633 client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol))
634 _, retErr := client.TestString(defaultCtx, "test")
635 mockCtrl.Finish()
636 mockCtrl = gomock.NewController(t)
637
638 if !willComplete {
639 err2, ok := retErr.(thrift.TTransportException)
640 if !ok {
641 t.Fatal("Expected a TTransportException")
642 }
643 if err2.TypeId() != thrift.TIMED_OUT {
644 t.Fatal("Expected TIMED_OUT error")
645 }
646 } else {
647 err2, ok := retErr.(thrift.TApplicationException)
648 if !ok {
649 t.Fatal("Expected a TApplicationException")
650 }
651 if err2.TypeId() != thrift.PROTOCOL_ERROR {
652 t.Fatal("Expected PROTOCOL_ERROR error")
653 }
654 break
655 }
656 }
657 }
658
659 // TestCase: call and reply with exception workflow in the client.
660 // Similar to TestClientCallException, but using legacy client constructor.
661 func TestClientCallExceptionLegacy(t *testing.T) {
662 mockCtrl := gomock.NewController(t)
663 transport := thrift.NewTMemoryBuffer()
664 err := thrift.NewTTransportException(thrift.TIMED_OUT, "test")
665 for i := 0; ; i++ {
666 protocol := NewMockTProtocol(mockCtrl)
667 willComplete := !prepareClientCallException(protocol, i, err)
668
669 client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
670 _, retErr := client.TestString(defaultCtx, "test")
671 mockCtrl.Finish()
672 mockCtrl = gomock.NewController(t)
673
674 if !willComplete {
675 err2, ok := retErr.(thrift.TTransportException)
676 if !ok {
677 t.Fatal("Expected a TTransportException")
678 }
679 if err2.TypeId() != thrift.TIMED_OUT {
680 t.Fatal("Expected TIMED_OUT error")
681 }
682 } else {
683 err2, ok := retErr.(thrift.TApplicationException)
684 if !ok {
685 t.Fatal("Expected a TApplicationException")
686 }
687 if err2.TypeId() != thrift.PROTOCOL_ERROR {
688 t.Fatal("Expected PROTOCOL_ERROR error")
689 }
690 break
691 }
692 }
693 }
694
695 // TestCase: Mismatching sequence id has been received in the client.
696 func TestClientSeqIdMismatch(t *testing.T) {
697 mockCtrl := gomock.NewController(t)
698 protocol := NewMockTProtocol(mockCtrl)
699 gomock.InOrder(
700 protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
701 protocol.EXPECT().WriteStructBegin("testString_args"),
702 protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
703 protocol.EXPECT().WriteString("test"),
704 protocol.EXPECT().WriteFieldEnd(),
705 protocol.EXPECT().WriteFieldStop(),
706 protocol.EXPECT().WriteStructEnd(),
707 protocol.EXPECT().WriteMessageEnd(),
708 protocol.EXPECT().Flush(context.Background()),
709 protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil),
710 )
711
712 client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol))
713 _, err := client.TestString(defaultCtx, "test")
714 mockCtrl.Finish()
715 appErr, ok := err.(thrift.TApplicationException)
716 if !ok {
717 t.Fatal("Expected TApplicationException")
718 }
719 if appErr.TypeId() != thrift.BAD_SEQUENCE_ID {
720 t.Fatal("Expected BAD_SEQUENCE_ID error")
721 }
722 }
723
724 // TestCase: Mismatching sequence id has been received in the client.
725 // Similar to TestClientSeqIdMismatch, but using legacy client constructor.
726 func TestClientSeqIdMismatchLegeacy(t *testing.T) {
727 mockCtrl := gomock.NewController(t)
728 transport := thrift.NewTMemoryBuffer()
729 protocol := NewMockTProtocol(mockCtrl)
730 gomock.InOrder(
731 protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
732 protocol.EXPECT().WriteStructBegin("testString_args"),
733 protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
734 protocol.EXPECT().WriteString("test"),
735 protocol.EXPECT().WriteFieldEnd(),
736 protocol.EXPECT().WriteFieldStop(),
737 protocol.EXPECT().WriteStructEnd(),
738 protocol.EXPECT().WriteMessageEnd(),
739 protocol.EXPECT().Flush(context.Background()),
740 protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil),
741 )
742
743 client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
744 _, err := client.TestString(defaultCtx, "test")
745 mockCtrl.Finish()
746 appErr, ok := err.(thrift.TApplicationException)
747 if !ok {
748 t.Fatal("Expected TApplicationException")
749 }
750 if appErr.TypeId() != thrift.BAD_SEQUENCE_ID {
751 t.Fatal("Expected BAD_SEQUENCE_ID error")
752 }
753 }
754
755 // TestCase: Wrong method name has been received in the client.
756 func TestClientWrongMethodName(t *testing.T) {
757 mockCtrl := gomock.NewController(t)
758 protocol := NewMockTProtocol(mockCtrl)
759 gomock.InOrder(
760 protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
761 protocol.EXPECT().WriteStructBegin("testString_args"),
762 protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
763 protocol.EXPECT().WriteString("test"),
764 protocol.EXPECT().WriteFieldEnd(),
765 protocol.EXPECT().WriteFieldStop(),
766 protocol.EXPECT().WriteStructEnd(),
767 protocol.EXPECT().WriteMessageEnd(),
768 protocol.EXPECT().Flush(context.Background()),
769 protocol.EXPECT().ReadMessageBegin().Return("unknown", thrift.REPLY, int32(1), nil),
770 )
771
772 client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol))
773 _, err := client.TestString(defaultCtx, "test")
774 mockCtrl.Finish()
775 appErr, ok := err.(thrift.TApplicationException)
776 if !ok {
777 t.Fatal("Expected TApplicationException")
778 }
779 if appErr.TypeId() != thrift.WRONG_METHOD_NAME {
780 t.Fatal("Expected WRONG_METHOD_NAME error")
781 }
782 }
783
784 // TestCase: Wrong method name has been received in the client.
785 // Similar to TestClientWrongMethodName, but using legacy client constructor.
786 func TestClientWrongMethodNameLegacy(t *testing.T) {
787 mockCtrl := gomock.NewController(t)
788 transport := thrift.NewTMemoryBuffer()
789 protocol := NewMockTProtocol(mockCtrl)
790 gomock.InOrder(
791 protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
792 protocol.EXPECT().WriteStructBegin("testString_args"),
793 protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
794 protocol.EXPECT().WriteString("test"),
795 protocol.EXPECT().WriteFieldEnd(),
796 protocol.EXPECT().WriteFieldStop(),
797 protocol.EXPECT().WriteStructEnd(),
798 protocol.EXPECT().WriteMessageEnd(),
799 protocol.EXPECT().Flush(context.Background()),
800 protocol.EXPECT().ReadMessageBegin().Return("unknown", thrift.REPLY, int32(1), nil),
801 )
802
803 client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
804 _, err := client.TestString(defaultCtx, "test")
805 mockCtrl.Finish()
806 appErr, ok := err.(thrift.TApplicationException)
807 if !ok {
808 t.Fatal("Expected TApplicationException")
809 }
810 if appErr.TypeId() != thrift.WRONG_METHOD_NAME {
811 t.Fatal("Expected WRONG_METHOD_NAME error")
812 }
813 }
814
815 // TestCase: Wrong message type has been received in the client.
816 func TestClientWrongMessageType(t *testing.T) {
817 mockCtrl := gomock.NewController(t)
818 protocol := NewMockTProtocol(mockCtrl)
819 gomock.InOrder(
820 protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
821 protocol.EXPECT().WriteStructBegin("testString_args"),
822 protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
823 protocol.EXPECT().WriteString("test"),
824 protocol.EXPECT().WriteFieldEnd(),
825 protocol.EXPECT().WriteFieldStop(),
826 protocol.EXPECT().WriteStructEnd(),
827 protocol.EXPECT().WriteMessageEnd(),
828 protocol.EXPECT().Flush(context.Background()),
829 protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.INVALID_TMESSAGE_TYPE, int32(1), nil),
830 )
831
832 client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol))
833 _, err := client.TestString(defaultCtx, "test")
834 mockCtrl.Finish()
835 appErr, ok := err.(thrift.TApplicationException)
836 if !ok {
837 t.Fatal("Expected TApplicationException")
838 }
839 if appErr.TypeId() != thrift.INVALID_MESSAGE_TYPE_EXCEPTION {
840 t.Fatal("Expected INVALID_MESSAGE_TYPE_EXCEPTION error")
841 }
842 }
843
844 // TestCase: Wrong message type has been received in the client.
845 // Similar to TestClientWrongMessageType, but using legacy client constructor.
846 func TestClientWrongMessageTypeLegacy(t *testing.T) {
847 mockCtrl := gomock.NewController(t)
848 transport := thrift.NewTMemoryBuffer()
849 protocol := NewMockTProtocol(mockCtrl)
850 gomock.InOrder(
851 protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)),
852 protocol.EXPECT().WriteStructBegin("testString_args"),
853 protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)),
854 protocol.EXPECT().WriteString("test"),
855 protocol.EXPECT().WriteFieldEnd(),
856 protocol.EXPECT().WriteFieldStop(),
857 protocol.EXPECT().WriteStructEnd(),
858 protocol.EXPECT().WriteMessageEnd(),
859 protocol.EXPECT().Flush(context.Background()),
860 protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.INVALID_TMESSAGE_TYPE, int32(1), nil),
861 )
862
863 client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
864 _, err := client.TestString(defaultCtx, "test")
865 mockCtrl.Finish()
866 appErr, ok := err.(thrift.TApplicationException)
867 if !ok {
868 t.Fatal("Expected TApplicationException")
869 }
870 if appErr.TypeId() != thrift.INVALID_MESSAGE_TYPE_EXCEPTION {
871 t.Fatal("Expected INVALID_MESSAGE_TYPE_EXCEPTION error")
872 }
873 }