Debian Codegraph @

pubsub/v2/pstest/examples_test.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package pstest_test
16
17 import (
18 "context"
19
20 "cloud.google.com/go/pubsub/v2"
21 "cloud.google.com/go/pubsub/v2/pstest"
22 "google.golang.org/api/option"
23 "google.golang.org/grpc"
24 "google.golang.org/grpc/credentials/insecure"
25 )
26
27 func ExampleNewServer() {
28 ctx := context.Background()
29 // Start a fake server running locally.
30 srv := pstest.NewServer()
31 defer srv.Close()
32 // Connect to the server without using TLS.
33 conn, err := grpc.Dial(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
34 if err != nil {
35 // TODO: Handle error.
36 }
37 defer conn.Close()
38 // Use the connection when creating a pubsub client.
39 client, err := pubsub.NewClient(ctx, "project", option.WithGRPCConn(conn))
40 if err != nil {
41 // TODO: Handle error.
42 }
43 defer client.Close()
44 _ = client // TODO: Use the client.
45 }
46
47 func ExampleNewServerWithPort() {
48 ctx := context.Background()
49 // Start a fake server running locally at 9001.
50 srv := pstest.NewServerWithPort(9001)
51 defer srv.Close()
52 // Connect to the server without using TLS.
53 conn, err := grpc.Dial(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
54 if err != nil {
55 // TODO: Handle error.
56 }
57 defer conn.Close()
58 // Use the connection when creating a pubsub client.
59 client, err := pubsub.NewClient(ctx, "project", option.WithGRPCConn(conn))
60 if err != nil {
61 // TODO: Handle error.
62 }
63 defer client.Close()
64 _ = client // TODO: Use the client.
65 }