Skip to content

Commit

Permalink
Merge pull request #32 from 11wizards/feat/skip-topic-check
Browse files Browse the repository at this point in the history
feature: skip topic check
  • Loading branch information
m110 authored Jul 16, 2024
2 parents 6bc9eb2 + 0719451 commit d0995da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/googlecloud/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type PublisherConfig struct {
// ProjectID is the Google Cloud Engine project ID.
ProjectID string

// If true, `Publisher` does not check if the topic exists before publishing.
DoNotCheckTopicExistence bool

// If false (default), `Publisher` tries to create a topic if there is none with the requested name.
// Otherwise, trying to subscribe to non-existent subscription results in `ErrTopicDoesNotExist`.
DoNotCreateTopicIfMissing bool
Expand Down Expand Up @@ -222,6 +225,10 @@ func (p *Publisher) topic(ctx context.Context, topic string) (t *pubsub.Topic, e
t.PublishSettings = *p.config.PublishSettings
}

if p.config.DoNotCheckTopicExistence {
return t, nil
}

exists, err := t.Exists(ctx)
if err != nil {
return nil, errors.Wrapf(err, "could not check if topic %s exists", topic)
Expand Down
18 changes: 18 additions & 0 deletions pkg/googlecloud/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ func TestPublishedMessageIdMatchesReceivedMessageId(t *testing.T) {
}
}

func TestPublisherDoesNotAttemptToCreateTopic(t *testing.T) {
topic := fmt.Sprintf("missing_topic_%d", rand.Int())

// Set up publisher
pub, err := googlecloud.NewPublisher(googlecloud.PublisherConfig{
// DoNotCheckTopicExistence is set to true, so the publisher will not check
// if the topic exists and will also not attempt to create it.
DoNotCheckTopicExistence: true,
DoNotCreateTopicIfMissing: false,
}, nil)
require.NoError(t, err)
defer pub.Close()

// Publish a message
publishedMsg := message.NewMessage(watermill.NewUUID(), []byte{})
require.Error(t, pub.Publish(topic, publishedMsg), googlecloud.ErrTopicDoesNotExist)
}

func produceMessages(t *testing.T, topic string, howMany int) {
pub, err := googlecloud.NewPublisher(googlecloud.PublisherConfig{}, nil)
require.NoError(t, err)
Expand Down

0 comments on commit d0995da

Please sign in to comment.