forked from Azure/azure-event-hubs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub_examples_test.go
157 lines (132 loc) · 3.38 KB
/
hub_examples_test.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package eventhub_test
import (
"context"
"fmt"
"os"
"time"
"github.com/joho/godotenv"
eventhub "github.com/Azure/azure-event-hubs-go/v3"
)
func init() {
if err := godotenv.Load(); err != nil {
fmt.Println("FATAL: ", err)
}
}
func ExampleHub_helloWorld() {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
connStr := os.Getenv("EVENTHUB_CONNECTION_STRING")
if connStr == "" {
fmt.Println("FATAL: expected environment variable EVENTHUB_CONNECTION_STRING not set")
return
}
hubManager, err := eventhub.NewHubManagerFromConnectionString(connStr)
if err != nil {
fmt.Println(err)
return
}
hubEntity, err := ensureHub(ctx, hubManager, "ExampleHub_helloWorld")
if err != nil {
fmt.Println(err)
return
}
// Create a client to communicate with EventHub
hub, err := eventhub.NewHubFromConnectionString(connStr + ";EntityPath=" + hubEntity.Name)
if err != nil {
fmt.Println(err)
return
}
err = hub.Send(ctx, eventhub.NewEventFromString("Hello World!"))
if err != nil {
fmt.Println(err)
return
}
exit := make(chan struct{})
handler := func(ctx context.Context, event *eventhub.Event) error {
text := string(event.Data)
fmt.Println(text)
exit <- struct{}{}
return nil
}
for _, partitionID := range *hubEntity.PartitionIDs {
_, err = hub.Receive(ctx, partitionID, handler)
}
// wait for the first handler to get called with "Hello World!"
select {
case <-exit:
// test completed
case <-ctx.Done():
// test timed out
}
err = hub.Close(ctx)
if err != nil {
fmt.Println(err)
return
}
// Output: Hello World!
}
func ExampleHub_webSocket() {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
connStr := os.Getenv("EVENTHUB_CONNECTION_STRING")
if connStr == "" {
fmt.Println("FATAL: expected environment variable EVENTHUB_CONNECTION_STRING not set")
return
}
hubManager, err := eventhub.NewHubManagerFromConnectionString(connStr)
if err != nil {
fmt.Println(err)
return
}
hubEntity, err := ensureHub(ctx, hubManager, "ExampleHub_helloWorld")
if err != nil {
fmt.Println(err)
return
}
// Create a client to communicate with EventHub
hub, err := eventhub.NewHubFromConnectionString(connStr+";EntityPath="+hubEntity.Name, eventhub.HubWithWebSocketConnection())
if err != nil {
fmt.Println(err)
return
}
err = hub.Send(ctx, eventhub.NewEventFromString("this message was sent and received via web socket!!"))
if err != nil {
fmt.Println(err)
return
}
exit := make(chan struct{})
handler := func(ctx context.Context, event *eventhub.Event) error {
text := string(event.Data)
fmt.Println(text)
exit <- struct{}{}
return nil
}
for _, partitionID := range *hubEntity.PartitionIDs {
_, err = hub.Receive(ctx, partitionID, handler)
}
// wait for the first handler to get called with "Hello World!"
select {
case <-exit:
// test completed
case <-ctx.Done():
// test timed out
}
err = hub.Close(ctx)
if err != nil {
fmt.Println(err)
return
}
// Output: this message was sent and received via web socket!!
}
func ensureHub(ctx context.Context, em *eventhub.HubManager, name string, opts ...eventhub.HubManagementOption) (*eventhub.HubEntity, error) {
_, err := em.Get(ctx, name)
if err == nil {
_ = em.Delete(ctx, name)
}
he, err := em.Put(ctx, name, opts...)
if err != nil {
fmt.Println(err)
return nil, err
}
return he, nil
}