mirror of https://github.com/tidwall/tile38.git
add tests
This commit is contained in:
parent
6b0282d960
commit
de055384f9
|
@ -1,8 +1,56 @@
|
||||||
package server
|
package tests
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func subTestInfo(t *testing.T, mc *mockServer) {
|
func downloadURLWithStatusCode(t *testing.T, u string) (int, string) {
|
||||||
runStep(t, mc, "valid json", info_valid_json_test)
|
resp, err := http.Get(u)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return resp.StatusCode, string(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func subTestMetrics(t *testing.T, mc *mockServer) {
|
||||||
|
mc.Do("SET", "metrics_test_1", "1", "FIELD", "foo", 5.5, "POINT", 5, 5)
|
||||||
|
mc.Do("SET", "metrics_test_2", "2", "FIELD", "foo", 19.19, "POINT", 19, 19)
|
||||||
|
mc.Do("SET", "metrics_test_2", "3", "FIELD", "foo", 19.19, "POINT", 19, 19)
|
||||||
|
mc.Do("SET", "metrics_test_2", "truck1:driver", "STRING", "John Denton")
|
||||||
|
|
||||||
|
status, index := downloadURLWithStatusCode(t, "http://127.0.0.1:4321/")
|
||||||
|
if status != 200 {
|
||||||
|
t.Fatalf("Expected status code 200, got: %d", status)
|
||||||
|
}
|
||||||
|
if !strings.Contains(index, "<a href") {
|
||||||
|
t.Fatalf("missing link on index page")
|
||||||
|
}
|
||||||
|
|
||||||
|
status, metrics := downloadURLWithStatusCode(t, "http://127.0.0.1:4321/metrics")
|
||||||
|
if status != 200 {
|
||||||
|
t.Fatalf("Expected status code 200, got: %d", status)
|
||||||
|
}
|
||||||
|
for _, want := range []string{
|
||||||
|
`tile38_connected_clients 1`,
|
||||||
|
`tile38_cmd_duration_seconds_count{cmd="set"}`,
|
||||||
|
`go_build_info`,
|
||||||
|
`go_threads`,
|
||||||
|
`tile38_collection_objects{col="metrics_test_1"} 1`,
|
||||||
|
`tile38_collection_objects{col="metrics_test_2"} 3`,
|
||||||
|
`tile38_collection_points{col="metrics_test_2"} 2`,
|
||||||
|
`tile38_replication_info`,
|
||||||
|
`role="leader"`,
|
||||||
|
} {
|
||||||
|
if !strings.Contains(metrics, want) {
|
||||||
|
t.Fatalf("wanted metric: %s, got: %s", want, metrics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ func mockOpenServer() (*mockServer, error) {
|
||||||
s := &mockServer{port: port}
|
s := &mockServer{port: port}
|
||||||
tlog.SetOutput(logOutput)
|
tlog.SetOutput(logOutput)
|
||||||
go func() {
|
go func() {
|
||||||
if err := server.Serve("localhost", port, dir, true, ""); err != nil {
|
if err := server.Serve("localhost", port, dir, true, ":4321"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -48,6 +48,7 @@ func TestAll(t *testing.T) {
|
||||||
runSubTest(t, "info", mc, subTestInfo)
|
runSubTest(t, "info", mc, subTestInfo)
|
||||||
runSubTest(t, "client", mc, subTestClient)
|
runSubTest(t, "client", mc, subTestClient)
|
||||||
runSubTest(t, "timeouts", mc, subTestTimeout)
|
runSubTest(t, "timeouts", mc, subTestTimeout)
|
||||||
|
runSubTest(t, "metrics", mc, subTestMetrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runSubTest(t *testing.T, name string, mc *mockServer, test func(t *testing.T, mc *mockServer)) {
|
func runSubTest(t *testing.T, name string, mc *mockServer, test func(t *testing.T, mc *mockServer)) {
|
||||||
|
|
Loading…
Reference in New Issue