|
| 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 | + "runtime/debug" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/apache/thrift/lib/go/test/gopath/src/clientmiddlewareexceptiontest" |
| 29 | + "github.com/apache/thrift/lib/go/thrift" |
| 30 | +) |
| 31 | + |
| 32 | +func TestServerConnectivityCheck(t *testing.T) { |
| 33 | + const ( |
| 34 | + // Server will sleep for longer than client is willing to wait |
| 35 | + // so client will close the connection. |
| 36 | + serverSleep = 50 * time.Millisecond |
| 37 | + clientSocketTimeout = time.Millisecond |
| 38 | + ) |
| 39 | + serverSocket, err := thrift.NewTServerSocket(":0") |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("failed to create server socket: %v", err) |
| 42 | + } |
| 43 | + processor := clientmiddlewareexceptiontest.NewClientMiddlewareExceptionTestProcessor(fakeClientMiddlewareExceptionTestHandler( |
| 44 | + func(ctx context.Context) (*clientmiddlewareexceptiontest.FooResponse, error) { |
| 45 | + time.Sleep(serverSleep) |
| 46 | + err := ctx.Err() |
| 47 | + if err == nil { |
| 48 | + t.Error("Expected server ctx to be cancelled, did not happen") |
| 49 | + return new(clientmiddlewareexceptiontest.FooResponse), nil |
| 50 | + } |
| 51 | + return nil, err |
| 52 | + }, |
| 53 | + )) |
| 54 | + server := thrift.NewTSimpleServer2(processor, serverSocket) |
| 55 | + if err := server.Listen(); err != nil { |
| 56 | + t.Fatalf("failed to listen server: %v", err) |
| 57 | + } |
| 58 | + server.SetLogger(func(msg string) { |
| 59 | + t.Errorf("Server logger called with %q", msg) |
| 60 | + t.Errorf("Server logger callstack:\n%s", debug.Stack()) |
| 61 | + }) |
| 62 | + addr := serverSocket.Addr().String() |
| 63 | + go server.Serve() |
| 64 | + t.Cleanup(func() { |
| 65 | + server.Stop() |
| 66 | + }) |
| 67 | + |
| 68 | + cfg := &thrift.TConfiguration{ |
| 69 | + SocketTimeout: clientSocketTimeout, |
| 70 | + } |
| 71 | + socket := thrift.NewTSocketConf(addr, cfg) |
| 72 | + if err := socket.Open(); err != nil { |
| 73 | + t.Fatalf("failed to create client connection: %v", err) |
| 74 | + } |
| 75 | + t.Cleanup(func() { |
| 76 | + socket.Close() |
| 77 | + }) |
| 78 | + inProtocol := thrift.NewTBinaryProtocolConf(socket, cfg) |
| 79 | + outProtocol := thrift.NewTBinaryProtocolConf(socket, cfg) |
| 80 | + client := thrift.NewTStandardClient(inProtocol, outProtocol) |
| 81 | + ctx, cancel := context.WithTimeout(context.Background(), clientSocketTimeout) |
| 82 | + defer cancel() |
| 83 | + _, err = clientmiddlewareexceptiontest.NewClientMiddlewareExceptionTestClient(client).Foo(ctx) |
| 84 | + socket.Close() |
| 85 | + if err == nil { |
| 86 | + t.Error("Expected client to time out, did not happen") |
| 87 | + } |
| 88 | +} |
0 commit comments