115 lines
2.2 KiB
Go
115 lines
2.2 KiB
Go
package grpcall
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"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()
|
|
|
|
// 在这里执行一些清理工作,例如关闭数据库连接等
|
|
gc.Close()
|
|
|
|
// 退出测试
|
|
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]))
|
|
}
|
|
}
|
|
|
|
// 数据请求测试
|
|
func TestInvokeUnary(t *testing.T) {
|
|
if resp, err := gc.Invoke("User.UserResourceStatus", "TestInt64Value", `123`, nil); err != nil {
|
|
t.Error(err)
|
|
} else {
|
|
// t.Log(ioutil.ReadAll(resp.Recv()))
|
|
str := ""
|
|
decoder := resp.RecvDecoder()
|
|
t.Log(decoder.Decode(&str), str)
|
|
}
|
|
}
|
|
|
|
// 服务流测试
|
|
func TestInvokeServStream(t *testing.T) {
|
|
if resp, err := gc.Invoke("User.UserResourceStatus", "GetEvent", `{}`, nil); err != nil {
|
|
t.Error(err)
|
|
} else {
|
|
defer resp.Close()
|
|
decoder := resp.RecvDecoder()
|
|
|
|
for {
|
|
data := struct {
|
|
Uuid string `json:"uuid"`
|
|
Type string `json:"type"`
|
|
}{}
|
|
|
|
if err := decoder.Decode(&data); err != nil {
|
|
if err != io.EOF && err != io.ErrClosedPipe {
|
|
t.Error(err)
|
|
}
|
|
break
|
|
}
|
|
fmt.Println(data)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 双向流测试
|
|
func TestInvokeBidiStream(t *testing.T) {
|
|
if resp, err := gc.Invoke("User.UserResourceStatus", "TestBidiStream", `"hello"`, nil); err != nil {
|
|
t.Error(err)
|
|
} else {
|
|
defer resp.Close()
|
|
|
|
decoder := resp.RecvDecoder()
|
|
|
|
go func() {
|
|
for {
|
|
data := struct {
|
|
Uuid string `json:"uuid"`
|
|
Type string `json:"type"`
|
|
}{}
|
|
|
|
if err := decoder.Decode(&data); err != nil {
|
|
if err != io.EOF && err != io.ErrClosedPipe {
|
|
t.Error(err)
|
|
}
|
|
return
|
|
}
|
|
fmt.Println(333, data)
|
|
}
|
|
}()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
fmt.Println("消息发送, ", i, resp.Send(fmt.Sprintf(`"abc %d"`, i)))
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|