Debian Codegraph @

pubsub/mempubsub/example_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
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 // https://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 mempubsub_test
16
17 import (
18 "context"
19 "log"
20 "time"
21
22 "gocloud.dev/pubsub"
23 "gocloud.dev/pubsub/mempubsub"
24 )
25
26 func ExampleNewSubscription() {
27 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
28 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
29 ctx := context.Background()
30
31 // Construct a *pubsub.Topic.
32 topic := mempubsub.NewTopic()
33 defer topic.Shutdown(ctx)
34
35 // Construct a *pubsub.Subscription for the topic.
36 subscription := mempubsub.NewSubscription(topic, 1*time.Minute /* ack deadline */)
37 defer subscription.Shutdown(ctx)
38 }
39
40 func ExampleNewTopic() {
41 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
42 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
43 ctx := context.Background()
44
45 topic := mempubsub.NewTopic()
46 defer topic.Shutdown(ctx)
47 }
48
49 func Example_openSubscriptionFromURL() {
50 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
51 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/mempubsub"
52 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
53 ctx := context.Background()
54
55 // Create a topic.
56 topic, err := pubsub.OpenTopic(ctx, "mem://topicA")
57 if err != nil {
58 log.Fatal(err)
59 }
60 defer topic.Shutdown(ctx)
61
62 // Create a subscription connected to that topic.
63 subscription, err := pubsub.OpenSubscription(ctx, "mem://topicA")
64 if err != nil {
65 log.Fatal(err)
66 }
67 defer subscription.Shutdown(ctx)
68 }
69
70 func Example_openTopicFromURL() {
71 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
72 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/mempubsub"
73 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
74 ctx := context.Background()
75
76 topic, err := pubsub.OpenTopic(ctx, "mem://topicA")
77 if err != nil {
78 log.Fatal(err)
79 }
80 defer topic.Shutdown(ctx)
81 }