<NME> async_reporter_test.rb
<BEF> # Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


require "helper"

describe Google::Cloud::Trace::AsyncReporter, :mock_trace do
  let(:reporter) {
    Google::Cloud::Trace::AsyncReporter.new tracer.service
  }

  describe "#patch_traces" do
    let(:queue) { reporter.instance_variable_get :@queue }
    let(:queue_resource) { reporter.instance_variable_get :@queue_resource }

    it "puts the trace on queue" do
      trace = Object.new

      reporter.async_suspend

      reporter.patch_traces trace
      queue.pop.must_equal trace
    end

    it "takes array of traces" do
      trace1 = Object.new
      trace2 = Object.new
      traces = [trace1, trace2]

      reporter.async_suspend

      reporter.patch_traces traces
      queue.pop.must_equal [trace1, trace2]
    end

    it "doesn't exceed max_queue_size" do
      max_queue_size = 3
      reporter.max_queue_size = max_queue_size
      reporter.async_suspend

      max_queue_size.times do |i|
        reporter.patch_traces i
      end

      reporter.patch_traces nil

      queue.size.must_equal max_queue_size

      # Empty the queue so that the reporter doesn't try to execute these
      # bogus items when it is flushed.
      max_queue_size.times do |i|
        queue.pop
      end
    end

    it "wakes up the child thread to dequeue the events" do
      trace = Object.new
      mocked_service = Minitest::Mock.new
      mocked_service.expect :patch_traces, nil, [trace]

      reporter = Google::Cloud::Trace::AsyncReporter.new mocked_service

      reporter.patch_traces trace

      wait_until_true do
        reporter.instance_variable_get(:@queue).size == 0
      end

      mocked_service.verify
    end
  end

  describe "#project" do
    it "returns that of #service" do
      reporter.service.stub :project, "service-project" do
        reporter.project.must_equal "service-project"
      end
    end
  end
end
  end

  describe "error cases" do
    after do
      Google::Cloud::Trace.configure.on_error = nil
    end

    it "reports a service error" do
      got_error = false
      async_reporter.on_error do |error|
        got_error = true
      end
      mocked_service.expect :project, "my-project"
      mocked_service.expect :patch_traces, nil, [[:whoops]]
      async_reporter.patch_traces trace1
      async_reporter.stop! 1
      assert got_error
    end

    it "uses the config's error handler" do
      got_error = false
      Google::Cloud::Trace.configure.on_error = proc do |error|
        got_error = true
      end
      mocked_service.expect :project, "my-project"
      mocked_service.expect :patch_traces, nil, [[:whoops]]
      async_reporter.patch_traces trace1
      async_reporter.stop! 1
      assert got_error
    end
  end
end

<MSG> AsyncReporter buffer traces and make batch API calls (#2736)

* Update AsyncReporter to buffer traces and batch API calls.
* Back pressure is applied by limiting the number of queued API calls.
* Errors will now be raised when there are not enough resources.
* Errors are reported using the AsyncReporter#on_error callback.
* Pending traces are sent before the process closes using at_exit.
<DFF> @@ -16,76 +16,65 @@
 require "helper"
 
 describe Google::Cloud::Trace::AsyncReporter, :mock_trace do
-  let(:reporter) {
-    Google::Cloud::Trace::AsyncReporter.new tracer.service
-  }
-
-  describe "#patch_traces" do
-    let(:queue) { reporter.instance_variable_get :@queue }
-    let(:queue_resource) { reporter.instance_variable_get :@queue_resource }
-
-    it "puts the trace on queue" do
-      trace = Object.new
-
-      reporter.async_suspend
-
-      reporter.patch_traces trace
-      queue.pop.must_equal trace
+  let(:mocked_service) { Minitest::Mock.new }
+  let(:async_reporter) { Google::Cloud::Trace::AsyncReporter.new mocked_service }
+  let(:trace_context) { Stackdriver::Core::TraceContext.new trace_id: "0123456789abcdef0123456789abcdef" }
+  let(:trace1) do
+    Google::Cloud::Trace::TraceRecord.new(project, trace_context).tap do |trace|
+      trace.create_span "first span",
+                        span_id: 12345,
+                        kind: Google::Cloud::Trace::SpanKind::RPC_SERVER,
+                        start_time: Time.now - 1,
+                        end_time: Time.now
     end
+  end
+  let(:trace2) do
+    Google::Cloud::Trace::TraceRecord.new(project, trace_context).tap do |trace|
+      trace.create_span "second span",
+                        span_id: 12346,
+                        kind: Google::Cloud::Trace::SpanKind::RPC_SERVER,
+                        start_time: Time.now - 1,
+                        end_time: Time.now
+    end
+  end
 
-    it "takes array of traces" do
-      trace1 = Object.new
-      trace2 = Object.new
-      traces = [trace1, trace2]
-
-      reporter.async_suspend
-
-      reporter.patch_traces traces
-      queue.pop.must_equal [trace1, trace2]
+  before do
+    async_reporter.on_error do |error|
+      raise error.inspect
     end
+  end
+
+  it "patches a single trace" do
+    mocked_service.expect :project, "my-project"
+    mocked_service.expect :patch_traces, nil, [[trace1]]
 
-    it "doesn't exceed max_queue_size" do
-      max_queue_size = 3
-      reporter.max_queue_size = max_queue_size
-      reporter.async_suspend
+    async_reporter.patch_traces trace1
 
-      max_queue_size.times do |i|
-        reporter.patch_traces i
-      end
+    async_reporter.stop! 1
 
-      reporter.patch_traces nil
+    mocked_service.verify
+  end
 
-      queue.size.must_equal max_queue_size
+  it "patches multiple traces" do
+    mocked_service.expect :project, "my-project"
+    mocked_service.expect :patch_traces, nil, [[trace1, trace2]]
 
-      # Empty the queue so that the reporter doesn't try to execute these
-      # bogus items when it is flushed.
-      max_queue_size.times do |i|
-        queue.pop
-      end
-    end
+    async_reporter.patch_traces [trace1, trace2]
 
-    it "wakes up the child thread to dequeue the events" do
-      trace = Object.new
-      mocked_service = Minitest::Mock.new
-      mocked_service.expect :patch_traces, nil, [trace]
+    async_reporter.stop! 1
 
-      reporter = Google::Cloud::Trace::AsyncReporter.new mocked_service
+    mocked_service.verify
+  end
 
-      reporter.patch_traces trace
+  it "buffers multiple patch_traces calls" do
+    mocked_service.expect :project, "my-project"
+    mocked_service.expect :patch_traces, nil, [[trace1, trace2]]
 
-      wait_until_true do
-        reporter.instance_variable_get(:@queue).size == 0
-      end
+    async_reporter.patch_traces trace1
+    async_reporter.patch_traces [trace2]
 
-      mocked_service.verify
-    end
-  end
+    async_reporter.stop! 1
 
-  describe "#project" do
-    it "returns that of #service" do
-      reporter.service.stub :project, "service-project" do
-        reporter.project.must_equal "service-project"
-      end
-    end
+    mocked_service.verify
   end
 end

{'addition_count': 46, 'commit_subject': 'AsyncReporter buffer traces and make batch API calls (#2736)', 'deletion_count': 57, 'file_extension': '.rb', 'lang': 'rb', 'license': 'apache-2.0', 'repo_name': 'googleapis/google-cloud-ruby'}