Debian Codegraph @

pubsub/azuresb/example_test.go

raw
1 // Copyright 2018 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 azuresb_test
16
17 import (
18 "context"
19 "log"
20 "os"
21
22 servicebus "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
23 "gocloud.dev/pubsub"
24 "gocloud.dev/pubsub/azuresb"
25 )
26
27 func ExampleOpenTopic() {
28 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
29 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
30 ctx := context.Background()
31
32 // Change these as needed for your application.
33 connString := os.Getenv("SERVICEBUS_CONNECTION_STRING")
34 topicName := "test-topic"
35
36 if connString == "" {
37 log.Fatal("Service Bus ConnectionString is not set")
38 }
39
40 // Connect to Azure Service Bus for the given topic.
41 sbClient, err := azuresb.NewClientFromConnectionString(connString, nil)
42 if err != nil {
43 log.Fatal(err)
44 }
45 sbSender, err := azuresb.NewSender(sbClient, topicName, nil)
46 if err != nil {
47 log.Fatal(err)
48 }
49 defer sbSender.Close(ctx)
50
51 // Construct a *pubsub.Topic.
52 topic, err := azuresb.OpenTopic(ctx, sbSender, nil)
53 if err != nil {
54 log.Fatal(err)
55 }
56 defer topic.Shutdown(ctx)
57 }
58
59 func Example_openTopicFromURL() {
60 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
61 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/azuresb"
62 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
63 ctx := context.Background()
64
65 // pubsub.OpenTopic creates a *pubsub.Topic from a URL.
66 // This URL will open the topic "mytopic" using a connection string
67 // from the environment variable SERVICEBUS_CONNECTION_STRING.
68 topic, err := pubsub.OpenTopic(ctx, "azuresb://mytopic")
69 if err != nil {
70 log.Fatal(err)
71 }
72 defer topic.Shutdown(ctx)
73 }
74
75 func ExampleOpenSubscription() {
76 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
77 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
78 ctx := context.Background()
79
80 // Change these as needed for your application.
81 serviceBusConnString := os.Getenv("SERVICEBUS_CONNECTION_STRING")
82 const topicName = "test-topic"
83 const subscriptionName = "test-subscription"
84
85 // Connect to Azure Service Bus for the given subscription.
86 sbClient, err := azuresb.NewClientFromConnectionString(serviceBusConnString, nil)
87 if err != nil {
88 log.Fatal(err)
89 }
90 sbReceiver, err := azuresb.NewReceiver(sbClient, topicName, subscriptionName, nil)
91 if err != nil {
92 log.Fatal(err)
93 }
94 defer sbReceiver.Close(ctx)
95
96 // Construct a *pubsub.Subscription.
97 subscription, err := azuresb.OpenSubscription(ctx, sbClient, sbReceiver, nil)
98 if err != nil {
99 log.Fatal(err)
100 }
101 defer subscription.Shutdown(ctx)
102 }
103
104 func Example_openSubscriptionFromURL() {
105 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
106 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/azuresb"
107 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
108 ctx := context.Background()
109
110 // pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.
111 // This URL will open the subscription "mysubscription" for the topic
112 // "mytopic" using a connection string from the environment variable
113 // SERVICEBUS_CONNECTION_STRING.
114 subscription, err := pubsub.OpenSubscription(ctx,
115 "azuresb://mytopic?subscription=mysubscription")
116 if err != nil {
117 log.Fatal(err)
118 }
119 defer subscription.Shutdown(ctx)
120 }
121
122 func ExampleOpenSubscription_inReceiveAndDeleteMode() {
123 ctx := context.Background()
124
125 // Change these as needed for your application.
126 serviceBusConnString := os.Getenv("SERVICEBUS_CONNECTION_STRING")
127 const topicName = "test-topic"
128 const subscriptionName = "test-subscription"
129
130 // Connect to Azure Service Bus for the given subscription.
131 sbClient, err := azuresb.NewClientFromConnectionString(serviceBusConnString, nil)
132 if err != nil {
133 log.Fatal(err)
134 }
135 // Create the azuresb.Subscription, configuring it with the
136 // ReceiveAndDelete option.
137 // See https://godoc.org/github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus#SubscriptionWithReceiveAndDelete.
138 sbReceiverOptions := &servicebus.ReceiverOptions{
139 ReceiveMode: servicebus.ReceiveModeReceiveAndDelete,
140 }
141 sbReceiver, err := azuresb.NewReceiver(sbClient, topicName, subscriptionName, sbReceiverOptions)
142 if err != nil {
143 log.Fatal(err)
144 }
145 defer sbReceiver.Close(ctx)
146
147 // Construct a *pubsub.Subscription. Since we configured
148 // the azuresb.Subscription with ReceiveAndDelete mode, we need
149 // to set SubscriptionOptions.ReceiveAndDelete = true.
150 subscription, err := azuresb.OpenSubscription(ctx,
151 sbClient, sbReceiver, &azuresb.SubscriptionOptions{ReceiveAndDelete: true})
152 if err != nil {
153 log.Fatal(err)
154 }
155 defer subscription.Shutdown(ctx)
156 }