-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
60 lines (48 loc) · 1.16 KB
/
example_test.go
File metadata and controls
60 lines (48 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package grpctest_test
import (
"context"
"fmt"
"os"
"time"
"google.golang.org/grpc"
"github.com/tomasbasham/grpctest"
echopb "github.com/tomasbasham/grpctest/testdata/echo/v1"
)
func Example_integration() {
s := grpctest.NewServer()
defer s.Stop()
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
conn, err := s.ClientConn()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create connection: %v", err)
return
}
client := echopb.NewEchoServiceClient(conn)
resp, err := client.Echo(context.Background(), &echopb.EchoRequest{Message: "Hello, world"})
if err != nil {
fmt.Fprintf(os.Stderr, "unexpected error: %v", err)
return
}
fmt.Println(resp.GetMessage())
// Output:
// Hello, world
}
func ExampleServer_Err() {
s := grpctest.NewServer()
s.Serve()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
go func() {
<-ctx.Done()
s.Stop()
}()
fmt.Println("waiting for server to stop...")
if err := s.Err(); err != nil && err != grpc.ErrServerStopped {
// Handle server error.
}
fmt.Println("server stopped")
// Output:
// waiting for server to stop...
// server stopped
}