Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-08-01 12:51:38 +03:00
parent 03fe6da356
commit 9496807a27
7 changed files with 93 additions and 44 deletions

View File

@ -89,7 +89,9 @@ func initial(t *testing.T) (server.Server, client.Client) {
) )
h := &testServer{} h := &testServer{}
gpb.RegisterTestServer(s, h) if err := gpb.RegisterTestServer(s, h); err != nil {
t.Fatal(err)
}
if err := s.Init(); err != nil { if err := s.Init(); err != nil {
t.Fatalf("failed to init: %v", err) t.Fatalf("failed to init: %v", err)
@ -118,7 +120,9 @@ func check(t *testing.T, addr string, path string, expected string, timeout bool
if err != nil { if err != nil {
t.Fatalf("Failed to created http.Request: %v", err) t.Fatalf("Failed to created http.Request: %v", err)
} }
defer rsp.Body.Close() defer func() {
_ = rsp.Body.Close()
}()
buf, err := ioutil.ReadAll(rsp.Body) buf, err := ioutil.ReadAll(rsp.Body)
if err != nil { if err != nil {
@ -140,7 +144,9 @@ func check(t *testing.T, addr string, path string, expected string, timeout bool
func TestApiTimeout(t *testing.T) { func TestApiTimeout(t *testing.T) {
s, c := initial(t) s, c := initial(t)
defer s.Stop() defer func() {
_ = s.Stop()
}()
router := rregister.NewRouter( router := rregister.NewRouter(
router.WithHandler(rpc.Handler), router.WithHandler(rpc.Handler),
@ -167,14 +173,19 @@ func TestApiTimeout(t *testing.T) {
log.Println(hsrv.ListenAndServe()) log.Println(hsrv.ListenAndServe())
}() }()
defer hsrv.Close() defer func() {
_ = hsrv.Close()
}()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"Id":"go.micro.client","Code":408,"Detail":"context deadline exceeded","Status":"Request Timeout"}`, true) check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"Id":"go.micro.client","Code":408,"Detail":"context deadline exceeded","Status":"Request Timeout"}`, true)
} }
func TestRouterRegisterPcre(t *testing.T) { func TestRouterRegisterPcre(t *testing.T) {
s, c := initial(t) s, c := initial(t)
defer s.Stop() defer func() {
_ = s.Stop()
}()
router := rregister.NewRouter( router := rregister.NewRouter(
router.WithHandler(rpc.Handler), router.WithHandler(rpc.Handler),
@ -208,7 +219,9 @@ func TestRouterRegisterPcre(t *testing.T) {
func TestRouterStaticPcre(t *testing.T) { func TestRouterStaticPcre(t *testing.T) {
s, c := initial(t) s, c := initial(t)
defer s.Stop() defer func() {
_ = s.Stop()
}()
router := rstatic.NewRouter( router := rstatic.NewRouter(
router.WithHandler(rpc.Handler), router.WithHandler(rpc.Handler),
@ -244,7 +257,9 @@ func TestRouterStaticPcre(t *testing.T) {
go func() { go func() {
log.Println(hsrv.ListenAndServe()) log.Println(hsrv.ListenAndServe())
}() }()
defer hsrv.Close() defer func() {
hsrv.Close()
}()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`, false) check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`, false)
@ -252,7 +267,9 @@ func TestRouterStaticPcre(t *testing.T) {
func TestRouterStaticGpath(t *testing.T) { func TestRouterStaticGpath(t *testing.T) {
s, c := initial(t) s, c := initial(t)
defer s.Stop() defer func() {
_ = s.Stop()
}()
router := rstatic.NewRouter( router := rstatic.NewRouter(
router.WithHandler(rpc.Handler), router.WithHandler(rpc.Handler),
@ -285,7 +302,9 @@ func TestRouterStaticGpath(t *testing.T) {
go func() { go func() {
log.Println(hsrv.ListenAndServe()) log.Println(hsrv.ListenAndServe())
}() }()
defer hsrv.Close() defer func() {
_ = hsrv.Close()
}()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`, false) check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`, false)
@ -296,7 +315,9 @@ func TestRouterStaticPcreInvalid(t *testing.T) {
var err error var err error
s, c := initial(t) s, c := initial(t)
defer s.Stop() defer func() {
_ = s.Stop()
}()
router := rstatic.NewRouter( router := rstatic.NewRouter(
router.WithHandler(rpc.Handler), router.WithHandler(rpc.Handler),

View File

@ -38,12 +38,14 @@ var (
) )
func TestConsumerGroup(t *testing.T) { func TestConsumerGroup(t *testing.T) {
topic := fmt.Sprintf("test_topic") topic := "test_topic"
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
t.Skip() t.Skip()
} }
logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)) if err := logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)); err != nil {
t.Fatal(err)
}
ctx := context.Background() ctx := context.Background()
var addrs []string var addrs []string
@ -189,12 +191,14 @@ func TestConsumerGroup(t *testing.T) {
} }
func TestSub(t *testing.T) { func TestSub(t *testing.T) {
topic := fmt.Sprintf("test_topic") topic := "test_topic"
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
t.Skip() t.Skip()
} }
logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)) if err := logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)); err != nil {
t.Fatal(err)
}
ctx := context.Background() ctx := context.Background()
var addrs []string var addrs []string
@ -256,6 +260,7 @@ func TestSub(t *testing.T) {
}() }()
fmt.Printf("prefill topic\n") fmt.Printf("prefill topic\n")
go func() { go func() {
for i := 0; i < 900000; i++ { for i := 0; i < 900000; i++ {
if err := brk.Publish(ctx, topic, bm); err != nil { if err := brk.Publish(ctx, topic, bm); err != nil {
@ -326,7 +331,9 @@ func BenchmarkPub(b *testing.B) {
b.Skip() b.Skip()
} }
logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)) if err := logger.DefaultLogger.Init(logger.WithLevel(logger.TraceLevel)); err != nil {
b.Fatal(err)
}
ctx := context.Background() ctx := context.Background()
var addrs []string var addrs []string
@ -415,7 +422,7 @@ func BenchmarkPub(b *testing.B) {
func BenchmarkPubSub(b *testing.B) { func BenchmarkPubSub(b *testing.B) {
b.Skip() b.Skip()
ctx := context.Background() ctx := context.Background()
topic := fmt.Sprintf("test_topic") topic := "test_topic"
var addrs []string var addrs []string
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 { if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
addrs = []string{"127.0.0.1:9092"} addrs = []string{"127.0.0.1:9092"}
@ -500,7 +507,9 @@ func TestPubSub(t *testing.T) {
t.Skip() t.Skip()
} }
logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)) if err := logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)); err != nil {
t.Fatal(err)
}
ctx := context.Background() ctx := context.Background()
var addrs []string var addrs []string

View File

@ -118,14 +118,21 @@ func TestFlow(t *testing.T) {
} }
id, err := w.Execute(ctx, &flow.Message{Body: req}, flow.ExecuteTimeout(2*time.Second)) id, err := w.Execute(ctx, &flow.Message{Body: req}, flow.ExecuteTimeout(2*time.Second))
keys, err := s.List(ctx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var keys []string
keys, err = s.List(ctx)
if err != nil {
t.Fatal(err)
}
_ = keys
keys, err = store.NewNamespaceStore(s, filepath.Join("workflows", id)).List(ctx) keys, err = store.NewNamespaceStore(s, filepath.Join("workflows", id)).List(ctx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_ = keys
keys, err = store.NewNamespaceStore(s, filepath.Join("steps", id)).List(ctx) keys, err = store.NewNamespaceStore(s, filepath.Join("steps", id)).List(ctx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -101,7 +101,9 @@ func TestMultipart(t *testing.T) {
} }
h := &Handler{t: t} h := &Handler{t: t}
pb.RegisterTestServer(srv, h) if err := pb.RegisterTestServer(srv, h); err != nil {
t.Fatal(err)
}
// start server // start server
if err := srv.Start(); err != nil { if err := srv.Start(); err != nil {
@ -140,9 +142,6 @@ func NewServerHandlerWrapper(t *testing.T) server.HandlerWrapper {
t.Fatal("metadata empty") t.Fatal("metadata empty")
} }
if v, ok := md.Get("Authorization"); ok && v == "test" { if v, ok := md.Get("Authorization"); ok && v == "test" {
type err struct {
detail string
}
nmd := metadata.New(1) nmd := metadata.New(1)
nmd.Set("my-key", "my-val") nmd.Set("my-key", "my-val")
nmd.Set("Content-Type", "text/xml") nmd.Set("Content-Type", "text/xml")
@ -234,7 +233,9 @@ func TestNativeFormUrlencoded(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
h := &Handler{t: t} h := &Handler{t: t}
pb.RegisterTestServer(srv, h) if err := pb.RegisterTestServer(srv, h); err != nil {
t.Fatal(err)
}
// start server // start server
if err := srv.Start(); err != nil { if err := srv.Start(); err != nil {
@ -266,6 +267,9 @@ func TestNativeFormUrlencoded(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/v1/test/call/my_name", service[0].Nodes[0].Address), strings.NewReader(data.Encode())) // URL-encoded payload req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/v1/test/call/my_name", service[0].Nodes[0].Address), strings.NewReader(data.Encode())) // URL-encoded payload
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
//req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) //req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
if err != nil {
t.Fatal(err)
}
rsp, err := http.DefaultClient.Do(req) rsp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -432,8 +436,11 @@ func TestNativeClientServer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer hrsp.Body.Close() defer func() {
buf, err := io.ReadAll(hrsp.Body) _ = hrsp.Body.Close()
}()
_, err = io.ReadAll(hrsp.Body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -460,7 +467,7 @@ func TestNativeClientServer(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
buf, err = io.ReadAll(hrsp.Body) buf, err := io.ReadAll(hrsp.Body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -491,7 +498,9 @@ func TestNativeServer(t *testing.T) {
) )
h := &Handler{t: t} h := &Handler{t: t}
pb.RegisterTestServer(srv, h) if err := pb.RegisterTestServer(srv, h); err != nil {
t.Fatal(err)
}
// start server // start server
if err := srv.Start(); err != nil { if err := srv.Start(); err != nil {
@ -633,7 +642,7 @@ func TestHTTPHandler(t *testing.T) {
// create server mux // create server mux
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello world`)) _, _ = w.Write([]byte(`hello world`))
}) })
// create handler // create handler
@ -692,7 +701,7 @@ func TestHTTPServer(t *testing.T) {
// create server mux // create server mux
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello world`)) _, _ = w.Write([]byte(`hello world`))
}) })
// create server // create server

View File

@ -65,14 +65,6 @@ func TestTCPServer(t *testing.T) {
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes) t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
} }
go func() {
<-h.done
// stop server
if err := srv.Stop(); err != nil {
t.Fatal(err)
}
}()
c, err := net.DialTimeout("tcp", srv.Options().Address, 5*time.Second) c, err := net.DialTimeout("tcp", srv.Options().Address, 5*time.Second)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -82,6 +74,12 @@ func TestTCPServer(t *testing.T) {
if _, err = c.Write([]byte("test")); err != nil { if _, err = c.Write([]byte("test")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
<-h.done
// stop server
if err := srv.Stop(); err != nil {
t.Fatal(err)
}
} }
func (h *testHandler) Serve(c net.Conn) { func (h *testHandler) Serve(c net.Conn) {

View File

@ -1,3 +1,5 @@
// +build ignore
package prometheus_test package prometheus_test
import ( import (
@ -14,7 +16,7 @@ import (
srv "github.com/unistack-org/micro-server-grpc/v3" srv "github.com/unistack-org/micro-server-grpc/v3"
"github.com/unistack-org/micro/v3/broker" "github.com/unistack-org/micro/v3/broker"
"github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/client"
errors "github.com/unistack-org/micro/v3/errors" "github.com/unistack-org/micro/v3/errors"
"github.com/unistack-org/micro/v3/register" "github.com/unistack-org/micro/v3/register"
"github.com/unistack-org/micro/v3/router" "github.com/unistack-org/micro/v3/router"
"github.com/unistack-org/micro/v3/server" "github.com/unistack-org/micro/v3/server"
@ -39,7 +41,6 @@ func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestRes
} }
func TestPrometheusMetrics(t *testing.T) { func TestPrometheusMetrics(t *testing.T) {
t.Skip("not implemented now")
// setup // setup
reg := register.NewRegister() reg := register.NewRegister()
brk := broker.NewBroker(broker.Register(reg)) brk := broker.NewBroker(broker.Register(reg))
@ -78,14 +79,16 @@ func TestPrometheusMetrics(t *testing.T) {
*testHandler *testHandler
} }
s.Handle( if err := s.Handle(s.NewHandler(&Test{new(testHandler)})); err != nil {
s.NewHandler(&Test{new(testHandler)}), t.Fatal(err)
) }
if err := s.Start(); err != nil { if err := s.Start(); err != nil {
t.Fatalf("Unexpected error starting server: %v", err) t.Fatalf("Unexpected error starting server: %v", err)
} }
defer s.Stop() defer func() {
_ = s.Stop()
}()
req := c.NewRequest(name, "Test.Method", &TestRequest{IsError: false}, client.RequestContentType("application/json")) req := c.NewRequest(name, "Test.Method", &TestRequest{IsError: false}, client.RequestContentType("application/json"))
rsp := TestResponse{} rsp := TestResponse{}

View File

@ -99,7 +99,9 @@ func TestClient(t *testing.T) {
if err := s.Init(); err != nil { if err := s.Init(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer s.Stop() defer func() {
_ = s.Stop()
}()
type Test struct { type Test struct {
*testHandler *testHandler