Repository: googleapis/google-cloud-ruby (Ruby)

Issue:
[PubSub] Async options ignored when `skip_lookup` is true
When retrieving a topic by name with `skip_lookup: true`, async options are not passed to the underlying async publisher.

#### Environment details

  - Ruby version: 3.1.2
  - Gem name and version: google-cloud-pubsub@2.10.0

#### Steps to reproduce

Setup:

```rb
pubsub = Google::Cloud::PubSub.new(project_id: "dummy-project")
pubsub.create_topic("dummy-topic")
```

❌ When `skip_lookup` is true, async options are _not_ passed as expected:

```rb
topic = pubsub.topic("dummy-topic", skip_lookup: true, async: { interval: 1 })
topic.publish_async("{}") # initialize the async publisher
topic.async_publisher.instance_variable_get("@interval")
#=> 0.01
```

✅ When `skip_lookup` is false, async options are passed as expected:

```rb
topic = pubsub.topic("dummy-topic", async: { interval: 1 })
topic.publish_async("{}") # initialize the async publisher
topic.async_publisher.instance_variable_get("@interval")
#=> 1
```

#### Additional details

It seems the cause of the issue is that unlike the [`Topic.from_grpc`](https://github.com/googleapis/google-cloud-ruby/blob/fe4de7b49ce74d561b291a9ad0802fb025625842/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb#L1073) method, the [`Topic.from_name`](https://github.com/googleapis/google-cloud-ruby/blob/fe4de7b49ce74d561b291a9ad0802fb025625842/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb#L1077-L1084) method doesn't set the topic's `@async_opts` instance variable.


Discussion:
Thanks for reporting 👍 



Patch:
diff --git a/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb b/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb
index 7da7e41a97de..fd955af6a329 100644
--- a/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb
+++ b/google-cloud-pubsub/lib/google/cloud/pubsub/project.rb
@@ -160,7 +160,7 @@ def project_id
         #
         def topic topic_name, project: nil, skip_lookup: nil, async: nil
           ensure_service!
-          options = { project: project }
+          options = { project: project, async: async }
           return Topic.from_name topic_name, service, options if skip_lookup
           grpc = service.get_topic topic_name, options
           Topic.from_grpc grpc, service, async: async
diff --git a/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb b/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb
index d953ad24f3d5..7cab6815ada0 100644
--- a/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb
+++ b/google-cloud-pubsub/lib/google/cloud/pubsub/topic.rb
@@ -1065,7 +1065,7 @@ def self.from_grpc grpc, service, async: nil
         # @private New reference {Topic} object without making an HTTP request.
         def self.from_name name, service, options = {}
           name = service.topic_path name, options
-          from_grpc(nil, service).tap do |t|
+          from_grpc(nil, service, async: options[:async]).tap do |t|
             t.instance_variable_set :@resource_name, name
           end
         end


Test patch:
diff --git a/google-cloud-pubsub/test/google/cloud/pubsub/project_test.rb b/google-cloud-pubsub/test/google/cloud/pubsub/project_test.rb
index 3cefd10b2ddd..aaf91baad03d 100644
--- a/google-cloud-pubsub/test/google/cloud/pubsub/project_test.rb
+++ b/google-cloud-pubsub/test/google/cloud/pubsub/project_test.rb
@@ -18,4 +18,10 @@
   it "knows the project identifier" do
     _(pubsub.project).must_equal project
   end
+
+  it "creates topic with async options when skip_lookup enabled" do
+    topic = pubsub.topic("test", skip_lookup: true, async: { interval: 1 })
+    topic.publish_async("{}")
+    _(topic.async_publisher.instance_variable_get("@interval")).must_equal 1
+  end
 end