pubsub

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultProjectID is the default project ID for the Pubsub container.
	DefaultProjectID = "test-project"
)

Variables

View Source
var WithProjectID = shared.WithProjectID

WithProjectID re-exports the common GCloud WithProjectID option

Functions

This section is empty.

Types

type Container

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

Container represents the Pubsub container type used in the module

func Run

Run creates an instance of the Pubsub GCloud container type. The URI uses the empty string as the protocol.

Example
// runPubsubContainer {
ctx := context.Background()

pubsubContainer, err := tcpubsub.Run(
	ctx,
	"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators",
	tcpubsub.WithProjectID("pubsub-project"),
)
defer func() {
	if err := testcontainers.TerminateContainer(pubsubContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to run container: %v", err)
	return
}
// }

// pubsubClient {
projectID := pubsubContainer.ProjectID()

conn, err := grpc.NewClient(pubsubContainer.URI(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
	log.Printf("failed to dial: %v", err)
	return
}

options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := pubsub.NewClient(ctx, projectID, options...)
if err != nil {
	log.Printf("failed to create client: %v", err)
	return
}
defer client.Close()
// }

topic, err := client.CreateTopic(ctx, "greetings")
if err != nil {
	log.Printf("failed to create topic: %v", err)
	return
}
subscription, err := client.CreateSubscription(ctx, "subscription",
	pubsub.SubscriptionConfig{Topic: topic})
if err != nil {
	log.Printf("failed to create subscription: %v", err)
	return
}
result := topic.Publish(ctx, &pubsub.Message{Data: []byte("Hello World")})
_, err = result.Get(ctx)
if err != nil {
	log.Printf("failed to publish message: %v", err)
	return
}

var data []byte
cctx, cancel := context.WithCancel(ctx)
err = subscription.Receive(cctx, func(_ context.Context, m *pubsub.Message) {
	data = m.Data
	m.Ack()
	defer cancel()
})
if err != nil {
	log.Printf("failed to receive message: %v", err)
	return
}

fmt.Println(string(data))
Output:

Hello World

func (*Container) ProjectID

func (c *Container) ProjectID() string

ProjectID returns the project ID of the Pubsub container.

func (*Container) URI

func (c *Container) URI() string

URI returns the URI of the Pubsub container.

type Option

type Option = shared.Option

Option aliases the common GCloud option type

Jump to

Keyboard shortcuts

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