brain/main_test.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-07-02 16:00:04 +00:00
package brain
import (
"context"
"log"
"net"
"testing"
"time"
"github.com/474420502/brain/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)
var lis *bufconn.Listener
func init() {
lis = bufconn.Listen(1024 * 1024)
// lis, err := net.Listen("tcp", ":8090")
s := grpc.NewServer(
grpc.MaxConcurrentStreams(100000),
grpc.MaxRecvMsgSize(10*1024*1024),
)
pb.RegisterBrainServiceServer(s, &BrainService{})
go func() {
if err := s.Serve(lis); err != nil {
log.Fatal(err)
}
}()
}
func bufDialer(ctx context.Context, address string) (net.Conn, error) {
return lis.Dial()
}
// go:generator protoc --go_out=plugins=grpc:./ ./protos/brain.proto
func TestCase(t *testing.T) {
time.Sleep(time.Second)
conn, err := grpc.DialContext(context.TODO(), lis.Addr().String(), grpc.WithContextDialer(bufDialer), grpc.WithInsecure())
if err != nil {
panic(err)
}
// 创建客户端
client := pb.NewBrainServiceClient(conn)
// 调用服务
resp, err := client.Add(context.TODO(), &pb.Request{})
if err != nil {
panic(err)
}
log.Println(resp)
}