Skip to content

Adding a configuration option for topic-subscription validation. #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pkg/googlecloud/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ func TestPublishSubscribeOrdering(t *testing.T) {
)
}

func TestSubscriberAllowedWhenAttachedToAnotherTopic(t *testing.T) {
rand.Seed(time.Now().Unix())
testNumber := rand.Int()
logger := watermill.NewStdLogger(true, true)

subNameFn := func(topic string) string {
return fmt.Sprintf("sub_%d", testNumber)
}

sub1, err := googlecloud.NewSubscriber(googlecloud.SubscriberConfig{
GenerateSubscriptionName: subNameFn,
}, logger)
require.NoError(t, err)

topic1 := fmt.Sprintf("topic1_%d", testNumber)

sub2, err := googlecloud.NewSubscriber(googlecloud.SubscriberConfig{
GenerateSubscriptionName: subNameFn,
DoNotEnforceSubscriptionAttachedToTopic: true,
}, logger)
require.NoError(t, err)
topic2 := fmt.Sprintf("topic2_%d", testNumber)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, err = sub1.Subscribe(ctx, topic1)
require.NoError(t, err)

// without the DoNotEnforceSubscriptionAttachedToTopic, this call would fail because subNameFn will return the
// same value for both sub1 and sub2, and sub1 will create a subscription and topic attached to each other
// making sub2.Subscribe fail because the requested subscription (topic2) is not attached to the GCP topic
_, err = sub2.Subscribe(ctx, topic2)
require.NoError(t, err)
}

func TestSubscriberUnexpectedTopicForSubscription(t *testing.T) {
testNumber := rand.Int()
logger := watermill.NewStdLogger(true, true)
Expand Down
13 changes: 11 additions & 2 deletions pkg/googlecloud/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ type SubscriberConfig struct {
// Otherwise, trying to create a subscription on non-existent topic results in `ErrTopicDoesNotExist`.
DoNotCreateTopicIfMissing bool

// If false (default), when the subscription already exists, `Subscriber` will make sure that the subscription is
// attached to the provided topic, and will return a ErrUnexpectedTopic if not.
// Otherwise, it won't check to which topic the subscription is attached to.
DoNotEnforceSubscriptionAttachedToTopic bool

// deprecated: ConnectTimeout is no longer used, please use timeout on context in Subscribe() method
ConnectTimeout time.Duration

Expand Down Expand Up @@ -437,6 +442,12 @@ func (s *Subscriber) existingSubscription(ctx context.Context, sub *pubsub.Subsc
return nil, errors.Wrap(err, "could not fetch config for existing subscription")
}

sub.ReceiveSettings = s.config.ReceiveSettings

if s.config.DoNotEnforceSubscriptionAttachedToTopic {
return sub, nil
}

fullyQualifiedTopicName := fmt.Sprintf("projects/%s/topics/%s", s.config.topicProjectID(), topic)

if config.Topic.String() != fullyQualifiedTopicName {
Expand All @@ -446,8 +457,6 @@ func (s *Subscriber) existingSubscription(ctx context.Context, sub *pubsub.Subsc
)
}

sub.ReceiveSettings = s.config.ReceiveSettings

return sub, nil
}

Expand Down