Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from Azure/issue-101
bump amqp version and ensure sender is using unsettled
- Loading branch information
Showing
8 changed files
with
123 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package eventhub_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/joho/godotenv" | ||
|
||
"github.com/Azure/azure-event-hubs-go" | ||
) | ||
|
||
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!" | ||
<-exit | ||
err = hub.Close(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
// Output: Hello World! | ||
} | ||
|
||
func ensureHub(ctx context.Context, em *eventhub.HubManager, name string, opts ...eventhub.HubManagementOption) (*eventhub.HubEntity, error) { | ||
he, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters