Skip to content

Commit

Permalink
add: bufnet test grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
seth-shi committed Sep 6, 2024
1 parent 9d0aea5 commit 0d79eda
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 125 deletions.
15 changes: 6 additions & 9 deletions app/id/rpc/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ import (
"google.golang.org/grpc"
)

var getConfig = loadConfigByFile

func loadConfigByFile(filename string) config.Config {
var c config.Config
conf.MustLoad(filename, &c)
return c
}
var (
configFile = "etc/id.yaml"
)

func main() {

cfg := getConfig("etc/id.yaml")
ctx := svc.NewServiceContext(cfg)
var c config.Config
conf.MustLoad(configFile, &c)
ctx := svc.NewServiceContext(c)

s := zrpc.MustNewServer(
ctx.Config.RpcServerConf, func(grpcServer *grpc.Server) {
Expand Down
34 changes: 10 additions & 24 deletions app/id/rpc/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,13 @@ import (
"os"
"testing"

"github.com/samber/lo"
"github.com/seth-shi/go-zero-testing-example/app/id/rpc/id"
"github.com/seth-shi/go-zero-testing-example/app/id/rpc/internal/config"
"github.com/seth-shi/go-zero-testing-example/pkg"
"github.com/stretchr/testify/require"
"github.com/zeromicro/go-zero/zrpc"
)

func Test_loadConfigByFile(t *testing.T) {
remove, tmp, err := pkg.CreateTempFile(
".yaml", `
Name: id.rpc
ListenOn: 0.0.0.0:9502
`,
)
require.NoError(t, err)
defer remove()

svcCtx := loadConfigByFile(tmp)
require.NoError(t, err)
require.Equal(t, "id.rpc", svcCtx.Name)
}

func Test_get(t *testing.T) {
var (
ctx = context.Background()
Expand All @@ -47,14 +32,15 @@ var (
func TestMain(m *testing.M) {

testListenOn = fmt.Sprintf(":%d", pkg.GetAvailablePort())

getConfig = func(filename string) config.Config {
return config.Config{
RpcServerConf: zrpc.RpcServerConf{
ListenOn: testListenOn,
},
}
}
data := fmt.Sprintf(
`
Name: id.rpc
ListenOn: %s
`, testListenOn,
)
remove, tmp := lo.Must2(pkg.CreateTempFile(".yaml", data))
defer remove()
configFile = tmp

go main()

Expand Down
23 changes: 15 additions & 8 deletions app/post/rpc/internal/faker/db.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package faker

import (
"context"

"github.com/samber/lo"
"github.com/seth-shi/go-zero-testing-example/app/id/rpc/id"
"github.com/seth-shi/go-zero-testing-example/app/post/rpc/internal/model/entity"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/driver/mysql"
"google.golang.org/grpc"
"gorm.io/gorm"
)

type fakerModels struct {
PostModel *entity.Post
}

func makeDatabase(dsn string, gen *idGenerator) *fakerModels {

db, err := gorm.Open(
mysql.Open(dsn),
)
logx.Must(err)
func makeDatabase(db *gorm.DB) *fakerModels {

// 创建表结构
logx.Must(db.Migrator().CreateTable(&entity.Post{}))

// 插入测试数据
entity.SetIdGenerator(gen)
entity.SetIdGenerator(&databaseSeeder{})
postModel := &entity.Post{
Title: lo.ToPtr("test"),
Content: lo.ToPtr("content"),
Expand All @@ -33,3 +31,12 @@ func makeDatabase(dsn string, gen *idGenerator) *fakerModels {

return &fakerModels{PostModel: postModel}
}

type databaseSeeder struct {
id uint64
}

func (d *databaseSeeder) Get(ctx context.Context, in *id.IdRequest, opts ...grpc.CallOption) (*id.IdResponse, error) {
d.id++
return &id.IdResponse{Id: d.id}, nil
}
27 changes: 0 additions & 27 deletions app/post/rpc/internal/faker/id.go

This file was deleted.

74 changes: 74 additions & 0 deletions app/post/rpc/internal/faker/idserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package faker

import (
"context"
"math/rand"
"net"
"sync"
"time"

"github.com/samber/lo"
"github.com/seth-shi/go-zero-testing-example/app/id/rpc/id"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)

type idServer struct {
id.UnimplementedIdServer
id uint64
locker sync.Locker
conn *bufconn.Listener
srv *grpc.Server
}

func (r *idServer) Get(ctx context.Context, request *id.IdRequest) (*id.IdResponse, error) {
r.locker.Lock()
defer r.locker.Unlock()

r.id++

return &id.IdResponse{
Id: r.id,
Node: 1,
}, nil
}

const (
buffSize = 1024 * 1024
)

func newIdServer() *idServer {
service := &idServer{
conn: bufconn.Listen(buffSize),
srv: grpc.NewServer(),
id: uint64(rand.Int() + startId),
locker: &sync.RWMutex{},
}
id.RegisterIdServer(service.srv, service)
go service.Serve()
time.Sleep(time.Second)

return service
}

func (r *idServer) Serve() {
lo.Must0(r.srv.Serve(r.conn))

}

func (r *idServer) Connect() *grpc.ClientConn {

// Create the dialer
dialer := func(context.Context, string) (net.Conn, error) {
return r.conn.Dial()
}

var opts []grpc.DialOption
opts = append(opts, grpc.WithContextDialer(dialer))
// Disable transport security
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))

// Connect https://stackoverflow.com/questions/78485578/how-to-use-the-bufconn-package-with-grpc-newclient
return lo.Must(grpc.NewClient("passthrough://bufnet", opts...))
}
58 changes: 39 additions & 19 deletions app/post/rpc/internal/faker/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,69 @@ package faker

import (
"fmt"
"math/rand"
"sync"

"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/samber/lo"
"github.com/seth-shi/go-zero-testing-example/app/id/rpc/id"
"github.com/seth-shi/go-zero-testing-example/app/post/rpc/internal/config"
"github.com/seth-shi/go-zero-testing-example/app/post/rpc/internal/model/do"
"github.com/seth-shi/go-zero-testing-example/app/post/rpc/internal/svc"
"github.com/seth-shi/go-zero-testing-example/pkg"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

const (
startId = 100000
)

type value struct {
IdServer *idGenerator
IdServer *idServer
Redis *miniredis.Miniredis
Models *fakerModels
Gorm *gorm.DB
DatabaseDsn string
RedisAddr string
RpcListen string
DatabaseDsn string
SvcCtx *svc.ServiceContext
}

var GetValue = sync.OnceValue(
func() value {

redis, redisAddr := pkg.FakerRedisServer()
dsn := pkg.FakerDatabaseServer()

rpcPort := pkg.GetAvailablePort()
redisMock, redisAddr := pkg.FakerRedisServer()
mysqlDsn := pkg.FakerDatabaseServer()
dbConn := lo.Must(gorm.Open(mysql.Open(mysqlDsn)))

conn, err := gorm.Open(mysql.Open(dsn))
logx.Must(err)
testIdServer := newIdServer()
listenOn := fmt.Sprintf(":%d", rpcPort)

idGen := &idGenerator{
startId: uint64(rand.Int() + 1),
locker: &sync.RWMutex{},
}
return value{
IdServer: idGen,
Redis: redis,
IdServer: testIdServer,
Redis: redisMock,
RedisAddr: redisAddr,
DatabaseDsn: dsn,
Models: makeDatabase(dsn, idGen),
RpcListen: fmt.Sprintf(":%d", rpcPort),
Gorm: conn,
Models: makeDatabase(dbConn),
RpcListen: listenOn,
DatabaseDsn: mysqlDsn,
Gorm: dbConn,
SvcCtx: &svc.ServiceContext{
Config: config.Config{
RpcServerConf: zrpc.RpcServerConf{
ListenOn: listenOn,
ServiceConf: service.ServiceConf{
Mode: service.TestMode,
},
},
},
Redis: redis.NewClient(&redis.Options{Addr: redisAddr}),
IdRpc: id.NewIdClient(testIdServer.Connect()),
Query: do.Use(dbConn),
},
}
},
)
6 changes: 3 additions & 3 deletions app/post/rpc/internal/svc/servicecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
conn, err := gorm.Open(mysql.Open(c.DataSource))
logx.Must(err)

idClient := id.NewIdClient(zrpc.MustNewClient(c.IdRpc).Conn())
entity.SetIdGenerator(idClient)

rdb := redis.NewClient(
&redis.Options{
Addr: c.RedisConf.Host,
Expand All @@ -36,6 +33,9 @@ func NewServiceContext(c config.Config) *ServiceContext {
},
)

idClient := id.NewIdClient(zrpc.MustNewClient(c.IdRpc).Conn())
entity.SetIdGenerator(idClient)

return &ServiceContext{
Config: c,
Redis: rdb,
Expand Down
31 changes: 31 additions & 0 deletions app/post/rpc/internal/svc/servicecontext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package svc

import (
"testing"

"github.com/seth-shi/go-zero-testing-example/app/post/rpc/internal/config"
"github.com/seth-shi/go-zero-testing-example/pkg"
"github.com/stretchr/testify/require"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/zrpc"
)

func TestNewServiceContext(t *testing.T) {
var (
dsn = pkg.FakerDatabaseServer()
_, redisAddr = pkg.FakerRedisServer()
c = config.Config{
RpcServerConf: zrpc.RpcServerConf{},
DataSource: dsn,
RedisConf: redis.RedisConf{
Host: redisAddr,
},
IdRpc: zrpc.RpcClientConf{
Target: "127.0.0.1:3000",
NonBlock: true,
},
}
)
ctx := NewServiceContext(c)
require.NotNil(t, ctx)
}
Loading

0 comments on commit 0d79eda

Please sign in to comment.