Skip to content

Commit

Permalink
chore(gcp): rm use of deprecated fn in test
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerinthenight committed Mar 29, 2024
1 parent f759ddd commit b30872e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/gcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
subscription := "longsub-testtopic"

// Get topic, create if needed.
p, t, err := gcppubsub.GetPublisher(*project, topic)
t, err := gcppubsub.GetTopic(*project, topic)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func main() {
}()

time.Sleep(time.Second * 5) // subscriber should be ready by now
p.PublishRaw(context.Background(), "", []byte("hello world"))
gcppubsub.PublishRaw(ctx, t, []byte("hello world"))

if !*noextend {
time.Sleep(time.Minute * 2) // wait for longCallback()
Expand Down
56 changes: 31 additions & 25 deletions gcppubsub/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ func GetSubscription(project, id string, topic *gpubsub.Topic, ackdeadline ...ti
return sub, nil
}

// DelSubscription converts the client into an utter introvert.
func DelSubscription(project, name string) error {
ctx := context.Background()
client, err := gpubsub.NewClient(ctx, project)
if err != nil {
return fmt.Errorf("NewClient failed: %w", err)
}

defer client.Close()
sub := client.Subscription(name)
exists, err := sub.Exists(ctx)
if err != nil {
return fmt.Errorf("Exists failed: %w", err)
}

if exists {
err = sub.Delete(ctx)
if err != nil {
return fmt.Errorf("Delete failed: %w", err)
}
}

return nil
}

// PublishRaw is a convenience function for publishing raw data to a topic.
func PublishRaw(ctx context.Context, topic *gpubsub.Topic, msg []byte) (string, error) {
res := topic.Publish(ctx, &gpubsub.Message{Data: msg})
return res.Get(ctx)
}

// GetPublisher is a simple wrapper to create a PubSub publisher using gizmo's Publisher interface.
//
// Deprecated: The gizmo package used in this function is now unmaintained.
Expand Down Expand Up @@ -126,28 +157,3 @@ func NewPubsubPublisher(projectId string, topicname string) (*PubsubPublisher, e

return &PubsubPublisher{cp, t}, nil
}

// DelSubscription converts the client into an utter introvert.
func DelSubscription(project, name string) error {
ctx := context.Background()
client, err := gpubsub.NewClient(ctx, project)
if err != nil {
return fmt.Errorf("NewClient failed: %w", err)
}

defer client.Close()
sub := client.Subscription(name)
exists, err := sub.Exists(ctx)
if err != nil {
return fmt.Errorf("Exists failed: %w", err)
}

if exists {
err = sub.Delete(ctx)
if err != nil {
return fmt.Errorf("Delete failed: %w", err)
}
}

return nil
}

0 comments on commit b30872e

Please sign in to comment.