forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testserver.go
72 lines (59 loc) · 1.56 KB
/
testserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package dbsql
import (
"net/http"
"net/http/httptest"
"github.com/apache/thrift/lib/go/thrift"
"github.com/databricks/databricks-sql-go/internal/cli_service"
)
type thriftHandler struct {
processor thrift.TProcessor
inPfactory, outPfactory thrift.TProtocolFactory
count503_2_retries int
count503_5_retries int
count429_2_retries int
}
func (h *thriftHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/503-2-retries":
if h.count503_2_retries <= 1 {
w.WriteHeader(http.StatusServiceUnavailable)
h.count503_2_retries++
return
} else {
h.count503_2_retries = 0
}
case "/429-2-retries":
if h.count429_2_retries <= 1 {
w.Header().Add("Retry-After", "1")
w.WriteHeader(http.StatusTooManyRequests)
h.count429_2_retries++
return
} else {
h.count429_2_retries = 0
}
case "/503-5-retries":
if h.count503_5_retries <= 5 {
w.WriteHeader(http.StatusServiceUnavailable)
h.count503_5_retries++
return
} else {
h.count503_5_retries = 0
}
}
thriftHandler := thrift.NewThriftHandlerFunc(h.processor, h.inPfactory, h.outPfactory)
thriftHandler(w, r)
}
func initThriftTestServer(handler cli_service.TCLIService) *httptest.Server {
tcfg := &thrift.TConfiguration{
TLSConfig: nil,
}
protocolFactory := thrift.NewTBinaryProtocolFactoryConf(tcfg)
processor := cli_service.NewTCLIServiceProcessor(handler)
th := thriftHandler{
processor: processor,
inPfactory: protocolFactory,
outPfactory: protocolFactory,
}
ts := httptest.NewServer(&th)
return ts
}