eventhubs

package
v0.42.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAcceptEULA

func WithAcceptEULA() testcontainers.CustomizeRequestOption

WithAcceptEULA sets the ACCEPT_EULA environment variable to "Y" for the eventhubs container.

func WithConfig

WithConfig sets the eventhubs config file for the eventhubs container, copying the content of the reader to the container file at "/Eventhubs_Emulator/ConfigFiles/Config.json".

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the Azure Event Hubs container type used in the module

func Run

Run creates an instance of the Azure Event Hubs container type

Example
ctx := context.Background()

eventHubsCtr, err := eventhubs.Run(ctx, "mcr.microsoft.com/azure-messaging/eventhubs-emulator:2.1.0", eventhubs.WithAcceptEULA())
defer func() {
	if err := testcontainers.TerminateContainer(eventHubsCtr); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

state, err := eventHubsCtr.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:
true
Example (SendEventsToEventHub)
ctx := context.Background()

// cfg {
cfg := `{
    "UserConfig": {
        "NamespaceConfig": [
            {
                "Type": "EventHub",
                "Name": "emulatorNs1",
                "Entities": [
                    {
                        "Name": "eh1",
                        "PartitionCount": "1",
                        "ConsumerGroups": [
                            {
                                "Name": "cg1"
                            }
                        ]
                    }
                ]
            }
        ],
        "LoggingConfig": {
            "Type": "File"
        }
    }
}
`
// }

// runEventHubsContainer {
eventHubsCtr, err := eventhubs.Run(ctx, "mcr.microsoft.com/azure-messaging/eventhubs-emulator:2.1.0", eventhubs.WithAcceptEULA(), eventhubs.WithConfig(strings.NewReader(cfg)))
defer func() {
	if err := testcontainers.TerminateContainer(eventHubsCtr); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

// ===== 1. Create an Event Hubs producer client using a connection string to the namespace and the event hub =====
// createProducerClient {
connectionString, err := eventHubsCtr.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

producerClient, err := azeventhubs.NewProducerClientFromConnectionString(connectionString, "eh1", nil)
if err != nil {
	log.Printf("failed to create producer client: %s", err)
	return
}
defer producerClient.Close(context.TODO())
// }

// ===== 2. Create sample events =====
// createSampleEvents {
events := []*azeventhubs.EventData{
	{
		Body: []byte("hello"),
	},
	{
		Body: []byte("world"),
	},
}
// }

// ===== 3. Create a batch object and add sample events to the batch =====
// createBatch {
newBatchOptions := &azeventhubs.EventDataBatchOptions{}

var batch *azeventhubs.EventDataBatch
maxRetries := 3
// Retry creating the event data batch 3 times, because the event hub is created from the configuration
// and Testcontainers cannot add a wait strategy for the event hub to be created.
for retries := 0; retries < maxRetries; retries++ {
	batch, err = producerClient.NewEventDataBatch(context.TODO(), newBatchOptions)
	if err == nil {
		break
	}

	if retries == maxRetries-1 {
		log.Printf("failed to create event data batch after %d attempts: %s", maxRetries, err)
		return
	}
}

for i := range events {
	err = batch.AddEventData(events[i], nil)
	if err != nil {
		log.Printf("failed to add event data to batch: %s", err)
		return
	}
}
// }

// ===== 4. Send the batch of events to the event hub =====
// sendEventDataBatch {
err = producerClient.SendEventDataBatch(context.TODO(), batch, nil)
if err != nil {
	log.Printf("failed to send event data batch: %s", err)
	return
}
// }

fmt.Println(err)
Output:
<nil>

func (*Container) AzuriteContainer

func (c *Container) AzuriteContainer() *azurite.Container

AzuriteContainer returns the azurite container that is used by the eventhubs container

func (*Container) ConnectionString

func (c *Container) ConnectionString(ctx context.Context) (string, error)

ConnectionString returns the connection string for the eventhubs container, using the following format: Endpoint=sb://<hostname>:<port>;SharedAccessKeyName=<key-name>;SharedAccessKey=<key>;UseDevelopmentEmulator=true;

func (*Container) Terminate

func (c *Container) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error

Terminate terminates the eventhubs container, the azurite container, and the network to communicate between them.

type Option

type Option func(*options) error

Option is an option for the EventHubs container.

func WithAzurite

func WithAzurite(img string, opts ...testcontainers.ContainerCustomizer) Option

WithAzurite sets the image and options for the Azurite container. By default, the image is "mcr.microsoft.com/azure-storage/azurite:3.33.0".

func (Option) Customize

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL