2023-05-30 17:30:06 +08:00
|
|
|
package grpcall
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2023-05-30 17:47:53 +08:00
|
|
|
"time"
|
2023-05-30 17:30:06 +08:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var gc *Grpcall
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if _gc, err := NewGrpcall("127.0.0.1:8888", nil, grpc.WithInsecure()); err != nil {
|
|
|
|
// if _gc, err := NewGrpcall("192.168.0.6:6644", nil, grpc.WithInsecure()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
gc = _gc
|
|
|
|
}
|
|
|
|
|
|
|
|
// 执行测试
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
// 在这里执行一些清理工作,例如关闭数据库连接等
|
2023-05-30 17:47:53 +08:00
|
|
|
gc.Close()
|
2023-05-30 17:30:06 +08:00
|
|
|
|
|
|
|
// 退出测试
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetServices(t *testing.T) {
|
|
|
|
t.Log(gc.GetServices())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetServiceMethods(t *testing.T) {
|
|
|
|
if s, err := gc.GetServices(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Log(gc.GetServiceMethods(s[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 17:47:53 +08:00
|
|
|
// 数据请求测试
|
2023-05-30 17:30:06 +08:00
|
|
|
func TestInvokeUnary(t *testing.T) {
|
2023-05-30 17:37:45 +08:00
|
|
|
if resp, err := gc.Invoke("User.UserResourceStatus", "TestInt64Value", `123`, nil); err != nil {
|
2023-05-30 17:30:06 +08:00
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Log(resp.Data())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 17:47:53 +08:00
|
|
|
// 服务流测试
|
2023-05-30 17:30:06 +08:00
|
|
|
func TestInvokeServStream(t *testing.T) {
|
2023-05-30 17:37:45 +08:00
|
|
|
if resp, err := gc.Invoke("User.UserResourceStatus", "GetEvent", `{}`, nil); err != nil {
|
2023-05-30 17:30:06 +08:00
|
|
|
t.Error(err)
|
|
|
|
} else if recv, err := resp.Recv(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if done, err := resp.Done(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
flag := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-recv:
|
|
|
|
fmt.Println("msg", msg, resp.Header())
|
|
|
|
case err := <-done:
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
flag <- true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-flag
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 17:47:53 +08:00
|
|
|
|
|
|
|
// 双向流测试
|
2023-05-30 17:30:06 +08:00
|
|
|
func TestInvokeBidiStream(t *testing.T) {
|
2023-05-30 17:37:45 +08:00
|
|
|
if resp, err := gc.Invoke("User.UserResourceStatus", "TestBidiStream", `"hello"`, nil); err != nil {
|
2023-05-30 17:30:06 +08:00
|
|
|
t.Error(err)
|
|
|
|
} else if send, err := resp.Send(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if recv, err := resp.Recv(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if done, err := resp.Done(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
flag := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
2023-05-30 17:54:30 +08:00
|
|
|
timer := time.NewTimer(time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
2023-05-30 17:30:06 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-recv:
|
|
|
|
fmt.Println("msg", msg, resp.Header())
|
|
|
|
case err := <-done:
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
flag <- true
|
|
|
|
return
|
2023-05-30 17:54:30 +08:00
|
|
|
case <-timer.C:
|
2023-05-30 17:47:53 +08:00
|
|
|
resp.Cancel()
|
2023-05-30 17:54:30 +08:00
|
|
|
timer.Reset(time.Second)
|
2023-05-30 17:30:06 +08:00
|
|
|
}
|
2023-05-30 17:54:30 +08:00
|
|
|
|
2023-05-30 17:30:06 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
send <- fmt.Sprintf(`"abc %d"`, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-flag
|
|
|
|
}
|
|
|
|
}
|