-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(badges): use simple in-memory cache for badges
- Loading branch information
Showing
4 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'pact_broker/logging' | ||
require 'pact_broker/badges/service' | ||
|
||
module PactBroker | ||
module Badges | ||
module CachedService | ||
|
||
extend self | ||
include PactBroker::Logging | ||
extend PactBroker::Services | ||
|
||
CACHE = {} | ||
private_constant :CACHE | ||
|
||
def pact_verification_badge pact, label, initials, verification_status | ||
badge_key = key(pact, label, initials, verification_status) | ||
CACHE[badge_key] ||= PactBroker::Badges::Service.pact_verification_badge(pact, label, initials, verification_status) | ||
end | ||
|
||
private | ||
|
||
def key pact, label, initials, verification_status | ||
"#{pact.consumer.name}-#{pact.provider.name}-#{label}-#{initials}-#{verification_status}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'pact_broker/badges/cached_service' | ||
require 'pact_broker/badges/service' | ||
|
||
module PactBroker | ||
module Badges | ||
describe CachedService do | ||
|
||
let(:consumer) { double('consumer', name: 'foo') } | ||
let(:provider) { double('provider', name: 'bar') } | ||
let(:pact) { double('pact', consumer: consumer, provider: provider) } | ||
let(:label) { 'consumer' } | ||
let(:initials) { false } | ||
let(:verification_status) { 'status' } | ||
|
||
describe "#pact_verification_badge" do | ||
|
||
before do | ||
allow(Service).to receive(:pact_verification_badge).and_return('badge') | ||
stub_const('PactBroker::Badges::CachedService::CACHE', {}) | ||
end | ||
|
||
subject { CachedService.pact_verification_badge pact, label, initials, verification_status } | ||
|
||
it "returns the badge" do | ||
expect(subject).to eq 'badge' | ||
end | ||
|
||
context "when the badge is not in the cache" do | ||
before do | ||
stub_const('PactBroker::Badges::CachedService::CACHE', {}) | ||
end | ||
|
||
it "retrieves the badge from the Badges::Service" do | ||
expect(Service).to receive(:pact_verification_badge) | ||
subject | ||
end | ||
end | ||
|
||
context "when the badge is in the cache" do | ||
it "returns the cached badge" do | ||
expect(Service).to receive(:pact_verification_badge).once | ||
CachedService.pact_verification_badge pact, label, initials, verification_status | ||
CachedService.pact_verification_badge pact, label, initials, verification_status | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |