-
Notifications
You must be signed in to change notification settings - Fork 87
/
server_test.go
149 lines (120 loc) · 4.18 KB
/
server_test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
)
func TestRunServer(t *testing.T) {
cnf, _ := ReadConfig("wrong_config.json")
collector := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
server := InitServer("", collector, false, true)
go server.Start(cnf)
server.echo.POST("/", server.writeHandler)
status, resp := request("POST", "/", "", server.echo)
assert.Equal(t, status, http.StatusOK)
assert.Equal(t, resp, "")
status, resp = request("POST", "/?query="+escSelect, "", server.echo)
assert.Equal(t, status, http.StatusOK)
assert.Equal(t, resp, "")
status, resp = request("POST", "/?query="+escTitle, qContent, server.echo)
assert.Equal(t, status, http.StatusOK)
assert.Equal(t, resp, "")
status, resp = request("POST", "/?query="+escTitle, "", server.echo)
assert.Equal(t, status, http.StatusInternalServerError)
assert.Equal(t, resp, "Empty insert\n")
status, resp = authRequest("POST", "default", "", "/?query="+escTitle, qContent, server.echo)
assert.Equal(t, status, http.StatusOK)
assert.Equal(t, resp, "")
status, resp = authRequest("POST", "default", "", "/", "", server.echo)
assert.Equal(t, status, http.StatusOK)
assert.Equal(t, resp, "")
server.echo.GET("/status", server.statusHandler)
status, _ = request("GET", "/status", "", server.echo)
assert.Equal(t, status, http.StatusOK)
server.echo.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
status, _ = request("GET", "/metrics", "", server.echo)
assert.Equal(t, status, http.StatusOK)
server.echo.GET("/debug/gc", server.gcHandler)
status, resp = request("GET", "/debug/gc", "", server.echo)
assert.Equal(t, status, http.StatusOK)
server.echo.GET("/debug/freemem", server.freeMemHandler)
status, resp = request("GET", "/debug/freemem", "", server.echo)
assert.Equal(t, status, http.StatusOK)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(ctx)
}
func TestServer_SafeQuit(t *testing.T) {
sender := &fakeSender{}
collect := NewCollector(sender, 1000, 1000, 0, true)
collect.AddTable("test")
collect.Push("sss", "sss")
assert.False(t, collect.Empty())
SafeQuit(collect, sender)
assert.True(t, collect.Empty())
assert.True(t, sender.Empty())
}
func TestServer_MultiServer(t *testing.T) {
received := make([]string, 0)
var mu sync.Mutex
s1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "")
req, _ := io.ReadAll(r.Body)
mu.Lock()
defer mu.Unlock()
received = append(received, string(req))
}))
defer s1.Close()
s2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "")
req, _ := io.ReadAll(r.Body)
mu.Lock()
defer mu.Unlock()
received = append(received, string(req))
}))
defer s2.Close()
sender := NewClickhouse(10, 10, "", false)
sender.AddServer(s1.URL, true)
sender.AddServer(s2.URL, true)
collect := NewCollector(sender, 1000, 1000, 0, true)
collect.AddTable("test")
collect.Push("eee", "eee")
collect.Push("fff", "fff")
collect.Push("ggg", "ggg")
assert.False(t, collect.Empty())
SafeQuit(collect, sender)
time.Sleep(100) // wait for http servers process requests
assert.Equal(t, 3, len(received))
assert.True(t, collect.Empty())
assert.True(t, sender.Empty())
os.Setenv("DUMP_CHECK_INTERVAL", "10")
cnf, err := ReadConfig("wrong_config.json")
os.Unsetenv("DUMP_CHECK_INTERVAL")
assert.Nil(t, err)
assert.Equal(t, 10, cnf.DumpCheckInterval)
go RunServer(cnf)
time.Sleep(1000)
}
func request(method, path string, body string, e *echo.Echo) (int, string) {
req := httptest.NewRequest(method, path, strings.NewReader(body))
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}
func authRequest(method, user string, password string, path string, body string, e *echo.Echo) (int, string) {
req := httptest.NewRequest(method, path, strings.NewReader(body))
req.SetBasicAuth(user, password)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}