examples: remove usage of WithBlock in examples (#4858)

This commit is contained in:
Hrishi Hiraskar
2021-11-01 23:36:39 +05:30
committed by GitHub
parent 6d465fe912
commit 467630fc24
18 changed files with 101 additions and 50 deletions

View File

@ -20,6 +20,9 @@ set +e
export TMPDIR=$(mktemp -d)
trap "rm -rf ${TMPDIR}" EXIT
export SERVER_PORT=50051
export UNIX_ADDR=abstract-unix-socket
clean () {
for i in {1..10}; do
jobs -p | xargs -n1 pkill -P
@ -61,6 +64,36 @@ EXAMPLES=(
"features/unix_abstract"
)
declare -A SERVER_ARGS=(
["features/unix_abstract"]="-addr $UNIX_ADDR"
["default"]="-port $SERVER_PORT"
)
declare -A CLIENT_ARGS=(
["features/unix_abstract"]="-addr $UNIX_ADDR"
["default"]="-addr localhost:$SERVER_PORT"
)
declare -A SERVER_WAIT_COMMAND=(
["features/unix_abstract"]="lsof -U | grep $UNIX_ADDR"
["default"]="lsof -i :$SERVER_PORT | grep $SERVER_PORT"
)
wait_for_server () {
example=$1
wait_command=${SERVER_WAIT_COMMAND[$example]:-${SERVER_WAIT_COMMAND["default"]}}
echo "$(tput setaf 4) waiting for server to start $(tput sgr 0)"
for i in {1..10}; do
eval "$wait_command" 2>&1 &>/dev/null
if [ $? -eq 0 ]; then
pass "server started"
return
fi
sleep 1
done
fail "cannot determine if server started"
}
declare -A EXPECTED_SERVER_OUTPUT=(
["helloworld"]="Received: world"
["route_guide"]=""
@ -105,6 +138,13 @@ for example in ${EXAMPLES[@]}; do
pass "successfully built server"
fi
# Start server
SERVER_LOG="$(mktemp)"
server_args=${SERVER_ARGS[$example]:-${SERVER_ARGS["default"]}}
go run ./$example/*server/*.go $server_args &> $SERVER_LOG &
wait_for_server $example
# Build client
if ! go build -o /dev/null ./${example}/*client/*.go; then
fail "failed to build client"
@ -112,12 +152,10 @@ for example in ${EXAMPLES[@]}; do
pass "successfully built client"
fi
# Start server
SERVER_LOG="$(mktemp)"
go run ./$example/*server/*.go &> $SERVER_LOG &
# Start client
CLIENT_LOG="$(mktemp)"
if ! timeout 20 go run ${example}/*client/*.go &> $CLIENT_LOG; then
client_args=${CLIENT_ARGS[$example]:-${CLIENT_ARGS["default"]}}
if ! timeout 20 go run ${example}/*client/*.go $client_args &> $CLIENT_LOG; then
fail "client failed to communicate with server
got server log:
$(cat $SERVER_LOG)

View File

@ -66,7 +66,6 @@ func main() {
grpc.WithTransportCredentials(creds),
}
opts = append(opts, grpc.WithBlock())
conn, err := grpc.Dial(*addr, opts...)
if err != nil {
log.Fatalf("did not connect: %v", err)

View File

@ -37,7 +37,7 @@ func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -72,7 +72,7 @@ func streamingCall(c pb.EchoClient, requestID int, message string, want codes.Co
func main() {
flag.Parse()
conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -21,9 +21,9 @@ package main
import (
"context"
"flag"
"log"
"net"
"os"
"time"
"google.golang.org/grpc"
@ -38,9 +38,15 @@ const (
defaultName = "world"
)
var (
addr = flag.String("addr", "localhost:50051", "the address to connect to")
name = flag.String("name", defaultName, "Name to greet")
)
func main() {
flag.Parse()
/***** Set up the server serving channelz service. *****/
lis, err := net.Listen("tcp", ":50052")
lis, err := net.Listen("tcp", *addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
@ -64,17 +70,13 @@ func main() {
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
/***** Make 100 SayHello RPCs *****/
for i := 0; i < 100; i++ {
// Setting a 150ms timeout on the RPC.
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Printf("could not greet: %v", err)
} else {

View File

@ -50,7 +50,7 @@ func main() {
altsTC := alts.NewClientCreds(alts.DefaultClientOptions())
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(altsTC), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(altsTC))
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -54,7 +54,7 @@ func main() {
}
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(creds), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -38,7 +38,7 @@ func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -153,7 +153,7 @@ func main() {
}
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(creds), grpc.WithUnaryInterceptor(unaryInterceptor), grpc.WithStreamInterceptor(streamInterceptor), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(creds), grpc.WithUnaryInterceptor(unaryInterceptor), grpc.WithStreamInterceptor(streamInterceptor))
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -59,7 +59,6 @@ func main() {
pickfirstConn, err := grpc.Dial(
fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName),
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Fatalf("did not connect: %v", err)
@ -76,7 +75,6 @@ func main() {
fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName),
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`), // This sets the initial balancing policy.
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Fatalf("did not connect: %v", err)

View File

@ -286,7 +286,7 @@ const message = "this is examples/metadata"
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -58,7 +58,7 @@ func callUnaryEcho(client ecpb.EchoClient, message string) {
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -58,7 +58,6 @@ func main() {
passthroughConn, err := grpc.Dial(
fmt.Sprintf("passthrough:///%s", backendAddr), // Dial to "passthrough:///localhost:50051"
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Fatalf("did not connect: %v", err)
@ -73,7 +72,6 @@ func main() {
exampleConn, err := grpc.Dial(
fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName), // Dial to "example:///resolver.example.grpc.io"
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Fatalf("did not connect: %v", err)

View File

@ -25,6 +25,7 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"time"
@ -33,6 +34,14 @@ import (
ecpb "google.golang.org/grpc/examples/features/proto/echo"
)
var (
// A dial target of `unix:@abstract-unix-socket` should also work fine for
// this example because of golang conventions (net.Dial behavior). But we do
// not recommend this since we explicitly added the `unix-abstract` scheme
// for cross-language compatibility.
addr = flag.String("addr", "abstract-unix-socket", "The unix abstract socket address")
)
func callUnaryEcho(c ecpb.EchoClient, message string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
@ -51,18 +60,15 @@ func makeRPCs(cc *grpc.ClientConn, n int) {
}
func main() {
// A dial target of `unix:@abstract-unix-socket` should also work fine for
// this example because of golang conventions (net.Dial behavior). But we do
// not recommend this since we explicitly added the `unix-abstract` scheme
// for cross-language compatibility.
addr := "unix-abstract:abstract-unix-socket"
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
flag.Parse()
sockAddr := fmt.Sprintf("unix-abstract:%v", *addr)
cc, err := grpc.Dial(sockAddr, grpc.WithInsecure())
if err != nil {
log.Fatalf("grpc.Dial(%q) failed: %v", addr, err)
log.Fatalf("grpc.Dial(%q) failed: %v", sockAddr, err)
}
defer cc.Close()
fmt.Printf("--- calling echo.Echo/UnaryEcho to %s\n", addr)
fmt.Printf("--- calling echo.Echo/UnaryEcho to %s\n", sockAddr)
makeRPCs(cc, 10)
fmt.Println()
}

View File

@ -25,6 +25,7 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net"
@ -34,6 +35,10 @@ import (
pb "google.golang.org/grpc/examples/features/proto/echo"
)
var (
addr = flag.String("addr", "abstract-unix-socket", "The unix abstract socket address")
)
type ecServer struct {
pb.UnimplementedEchoServer
addr string
@ -44,13 +49,15 @@ func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.Echo
}
func main() {
netw, addr := "unix", "\x00abstract-unix-socket"
lis, err := net.Listen(netw, addr)
flag.Parse()
netw := "unix"
socketAddr := fmt.Sprintf("\x00%v", *addr)
lis, err := net.Listen(netw, socketAddr)
if err != nil {
log.Fatalf("net.Listen(%q, %q) failed: %v", netw, addr, err)
log.Fatalf("net.Listen(%q, %q) failed: %v", netw, socketAddr, err)
}
s := grpc.NewServer()
pb.RegisterEchoServer(s, &ecServer{addr: addr})
pb.RegisterEchoServer(s, &ecServer{addr: socketAddr})
log.Printf("serving on %s\n", lis.Addr().String())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)

View File

@ -21,8 +21,8 @@ package main
import (
"context"
"flag"
"log"
"os"
"time"
"google.golang.org/grpc"
@ -30,13 +30,18 @@ import (
)
const (
address = "localhost:50051"
defaultName = "world"
)
var (
addr = flag.String("addr", "localhost:50051", "the address to connect to")
name = flag.String("name", defaultName, "Name to greet")
)
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
@ -44,13 +49,9 @@ func main() {
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}

View File

@ -21,6 +21,8 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net"
@ -28,8 +30,8 @@ import (
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
port = ":50051"
var (
port = flag.Int("port", 50051, "The server port")
)
// server is used to implement helloworld.GreeterServer.
@ -44,7 +46,8 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
}
func main() {
lis, err := net.Listen("tcp", port)
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

View File

@ -39,7 +39,7 @@ import (
var (
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
caFile = flag.String("ca_file", "", "The file containing the CA root cert file")
serverAddr = flag.String("server_addr", "localhost:10000", "The server address in the format of host:port")
serverAddr = flag.String("addr", "localhost:50051", "The server address in the format of host:port")
serverHostOverride = flag.String("server_host_override", "x.test.example.com", "The server name used to verify the hostname returned by the TLS handshake")
)
@ -167,7 +167,6 @@ func main() {
opts = append(opts, grpc.WithInsecure())
}
opts = append(opts, grpc.WithBlock())
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)