func TestBlockGRPC(t *testing.T) {
	test := makeTestData(512 * 1024)
	m := &mockBlockGRPC{
		data: test,
	}
	lis, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		t.Fatal(err)
	}
	grpcSrv := grpc.NewServer()
	models.RegisterTorusStorageServer(grpcSrv, m)
	go grpcSrv.Serve(lis)
	defer grpcSrv.Stop()
	conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure(), grpc.WithTimeout(10*time.Second))
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	client := models.NewTorusStorageClient(conn)
	resp, err := client.Block(context.TODO(), &models.BlockRequest{
		BlockRef: torus.BlockRef{
			INodeRef: torus.NewINodeRef(1, 2),
			Index:    3,
		}.ToProto(),
	})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(test, resp.Data) {
		t.Fatal("unequal response")
	}
}
func grpcRPCDialer(url *url.URL, timeout time.Duration, gmd torus.GlobalMetadata) (protocols.RPC, error) {
	h := url.Host
	if !strings.Contains(h, ":") {
		h = net.JoinHostPort(h, defaultPort)
	}
	conn, err := grpc.Dial(h, grpc.WithInsecure(), grpc.WithTimeout(timeout))
	if err != nil {
		return nil, err
	}
	return &client{
		conn:    conn,
		handler: models.NewTorusStorageClient(conn),
	}, nil
}
func BenchmarkGRPCRebalanceCheck(b *testing.B) {
	test := make([]torus.BlockRef, nchecks)
	m := &mockBlockGRPC{}
	for i, _ := range test {
		test[i].Index = 3
		test[i].INodeRef = torus.NewINodeRef(1, torus.INodeID(rand.Intn(40)))
	}
	lis, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		b.Fatal(err)
	}
	grpcSrv := grpc.NewServer()
	models.RegisterTorusStorageServer(grpcSrv, m)
	go grpcSrv.Serve(lis)
	defer grpcSrv.Stop()
	conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure(), grpc.WithTimeout(10*time.Second))
	if err != nil {
		b.Fatal(err)
	}
	defer conn.Close()
	client := models.NewTorusStorageClient(conn)
	total := 0
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		refs := make([]*models.BlockRef, 0, len(test))
		for _, x := range test {
			refs = append(refs, x.ToProto())
		}
		resp, err := client.RebalanceCheck(context.TODO(), &models.RebalanceCheckRequest{
			BlockRefs: refs,
		})
		if err != nil {
			b.Fatal(err)
		}
		for i, x := range resp.Valid {
			if x {
				if test[i].INode%2 == 0 {
					b.Fatal("unequal")
				}
			} else {
				if test[i].INode%2 == 1 {
					b.Fatal("unequal")
				}
			}
		}
		total += len(resp.Valid)
	}
	b.SetBytes(int64(total / b.N))
}
func BenchmarkGRPCPutBlock(b *testing.B) {
	test := makeTestData(512 * 1024)
	m := &mockBlockGRPC{
		data: test,
	}
	lis, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		b.Fatal(err)
	}
	grpcSrv := grpc.NewServer()
	models.RegisterTorusStorageServer(grpcSrv, m)
	go grpcSrv.Serve(lis)
	defer grpcSrv.Stop()
	conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure(), grpc.WithTimeout(10*time.Second))
	if err != nil {
		b.Fatal(err)
	}
	defer conn.Close()
	client := models.NewTorusStorageClient(conn)
	total := 0
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		resp, err := client.PutBlock(context.TODO(), &models.PutBlockRequest{
			Refs: []*models.BlockRef{
				torus.BlockRef{
					INodeRef: torus.NewINodeRef(1, 2),
					Index:    3,
				}.ToProto(),
			},
			Blocks: [][]byte{
				test,
			},
		})
		if err != nil {
			b.Fatal(err)
		}
		if !resp.Ok {
			b.Fatal("unequal response")
		}
		total += len(test)
	}
	b.SetBytes(int64(total / b.N))
}