From 5fd635d18b77f56f6acd264ccaa1a357e2fa1cdd Mon Sep 17 00:00:00 2001
From: Grzegorz Bizon <grzesiek.bizon@gmail.com>
Date: Thu, 13 Oct 2016 15:22:55 +0200
Subject: [PATCH] Update code coverage for CI build asynchronously

---
 app/workers/build_coverage_worker.rb       |  9 +++++++++
 spec/workers/build_coverage_worker_spec.rb | 23 ++++++++++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100644 app/workers/build_coverage_worker.rb
 create mode 100644 spec/workers/build_coverage_worker_spec.rb

diff --git a/app/workers/build_coverage_worker.rb b/app/workers/build_coverage_worker.rb
new file mode 100644
index 00000000000..0680645a8db
--- /dev/null
+++ b/app/workers/build_coverage_worker.rb
@@ -0,0 +1,9 @@
+class BuildCoverageWorker
+  include Sidekiq::Worker
+  sidekiq_options queue: :default
+
+  def perform(build_id)
+    Ci::Build.find_by(id: build_id)
+      .try(:update_coverage)
+  end
+end
diff --git a/spec/workers/build_coverage_worker_spec.rb b/spec/workers/build_coverage_worker_spec.rb
new file mode 100644
index 00000000000..ba20488f663
--- /dev/null
+++ b/spec/workers/build_coverage_worker_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe BuildCoverageWorker do
+  describe '#perform' do
+    context 'when build exists' do
+      let!(:build) { create(:ci_build) }
+
+      it 'updates code coverage' do
+        expect_any_instance_of(Ci::Build)
+          .to receive(:update_coverage)
+
+        described_class.new.perform(build.id)
+      end
+    end
+
+    context 'when build does not exist' do
+      it 'does not raise exception' do
+        expect { described_class.new.perform(123) }
+          .not_to raise_error
+      end
+    end
+  end
+end
-- 
GitLab