]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/test/go/src/common/context_test.go
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / test / go / src / common / context_test.go
CommitLineData
f67539c2
TL
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
20package common
21
22import (
23 "context"
24 "fmt"
25 "net"
26 "net/http"
27 "net/url"
28 "os"
29 "syscall"
30 "testing"
31 "thrift"
32 "time"
33)
34
35type slowHttpHandler struct{}
36
37func (slowHttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
38 time.Sleep(1 * time.Second)
39}
40
41func TestHttpContextTimeout(t *testing.T) {
42 certPath = "../../../keys"
43
44 unit := test_unit{"127.0.0.1", 9096, "", "http", "binary", false}
45
46 server := &http.Server{Addr: unit.host + fmt.Sprintf(":%d", unit.port), Handler: slowHttpHandler{}}
47 go server.ListenAndServe()
48
49 client, trans, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
50 if err != nil {
51 t.Errorf("Unable to start client: %v", err)
52 return
53 }
54 defer trans.Close()
55
56 unwrapErr := func(err error) error {
57 for {
58 switch err.(type) {
59 case thrift.TTransportException:
60 err = err.(thrift.TTransportException).Err()
61 case *url.Error:
62 err = err.(*url.Error).Err
63 case *net.OpError:
64 err = err.(*net.OpError).Err
65 case *os.SyscallError:
66 err = err.(*os.SyscallError).Err
67 default:
68 return err
69 }
70 }
71 }
72
73 serverStartupDeadline := time.Now().Add(5 * time.Second)
74 for {
75 ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)
76 err = client.TestVoid(ctx)
77 err = unwrapErr(err)
78 if err != syscall.ECONNREFUSED || time.Now().After(serverStartupDeadline) {
79 break
80 }
81 time.Sleep(time.Millisecond)
82 }
83
84 if err == nil {
85 t.Errorf("Request completed (should have timed out)")
86 return
87 }
88
89 // We've got to switch on `err.Error()` here since go1.7 doesn't properly return
90 // `context.DeadlineExceeded` error and `http.errRequestCanceled` is not exported.
91 // See https://github.com/golang/go/issues/17711
92 switch err.Error() {
93 case context.DeadlineExceeded.Error(), "net/http: request canceled":
94 // Expected error
95 default:
96 t.Errorf("Unexpected error: %s", err)
97 }
98}