Commit 5c9ba42e authored by Sean Arnold's avatar Sean Arnold Committed by Peter Leitzen

Add specs for reactive cache set cache spec

Rename file too
parent 117e2614
......@@ -52,6 +52,13 @@ module ReactiveCaching
end
end
def with_reactive_cache_set(resource, opts, &blk)
data = with_reactive_cache(resource, opts, &blk)
save_keys_in_set(resource, opts) if data
data
end
# This method is used for debugging purposes and should not be used otherwise.
def without_reactive_cache(*args, &blk)
return with_reactive_cache(*args, &blk) unless Rails.env.development?
......@@ -65,6 +72,12 @@ module ReactiveCaching
Rails.cache.delete(alive_reactive_cache_key(*args))
end
def clear_reactive_cache_set!(*args)
cache_key = full_reactive_cache_key(args)
reactive_set_cache.clear_cache!(cache_key)
end
def exclusively_update_reactive_cache!(*args)
locking_reactive_cache(*args) do
key = full_reactive_cache_key(*args)
......@@ -86,6 +99,16 @@ module ReactiveCaching
private
def save_keys_in_set(resource, opts)
cache_key = full_reactive_cache_key(resource)
reactive_set_cache.write(cache_key, "#{cache_key}:#{opts}")
end
def reactive_set_cache
Gitlab::ReactiveCacheSetCache.new(expires_in: reactive_cache_lifetime)
end
def refresh_reactive_cache!(*args)
clear_reactive_cache!(*args)
keep_alive_reactive_cache!(*args)
......
......@@ -85,7 +85,7 @@ module ErrorTracking
end
def list_sentry_issues(opts = {})
with_reactive_cache('list_issues', opts.stringify_keys) do |result|
with_reactive_cache_set('list_issues', opts.stringify_keys) do |result|
result
end
end
......@@ -130,6 +130,10 @@ module ErrorTracking
end
end
def expire_issues_cache
clear_reactive_cache_set!('list_issues')
end
# http://HOST/api/0/projects/ORG/PROJECT
# ->
# http://HOST/ORG/PROJECT
......
......@@ -11,6 +11,7 @@ module ErrorTracking
)
compose_response(response) do
project_error_tracking_setting.expire_issues_cache
response[:closed_issue_iid] = update_related_issue&.iid
end
end
......
......@@ -10,9 +10,13 @@ module Gitlab
@expires_in = expires_in
end
def cache_key(key)
"#{cache_type}:#{key}:set"
end
def clear_cache!(key)
with do |redis|
keys = read(key).map { |value| "#{cache_type}#{value}" }
keys = read(key).map { |value| "#{cache_type}:#{value}" }
keys << cache_key(key)
redis.pipelined do
......@@ -24,7 +28,7 @@ module Gitlab
private
def cache_type
"#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:"
Gitlab::Redis::Cache::CACHE_NAMESPACE
end
end
end
......@@ -12,7 +12,7 @@ describe Gitlab::ReactiveCacheSetCache, :clean_gitlab_redis_cache do
subject { cache.cache_key(cache_prefix) }
it 'includes the suffix' do
expect(subject).to eq "#{cache_prefix}:set"
expect(subject).to eq "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:#{cache_prefix}:set"
end
end
......
......@@ -112,6 +112,43 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end
end
describe '#with_reactive_cache_set', :use_clean_rails_redis_caching do
subject(:go!) do
instance.with_reactive_cache_set('resource', {}) do |data|
data
end
end
it 'calls with_reactive_cache' do
expect(instance)
.to receive(:with_reactive_cache)
go!
end
context 'data returned' do
let(:resource) { 'resource' }
let(:set_key) { "#{cache_key}:#{resource}" }
let(:set_cache) { Gitlab::ReactiveCacheSetCache.new }
before do
stub_reactive_cache(instance, true, resource, {})
end
it 'saves keys in set' do
expect(set_cache.read(set_key)).to be_empty
go!
expect(set_cache.read(set_key)).not_to be_empty
end
it 'returns the data' do
expect(go!).to eq(true)
end
end
end
describe '.reactive_cache_worker_finder' do
context 'with default reactive_cache_worker_finder' do
let(:args) { %w(other args) }
......
......@@ -8,7 +8,7 @@ describe ErrorTracking::ProjectErrorTrackingSetting do
let_it_be(:project) { create(:project) }
subject { create(:project_error_tracking_setting, project: project) }
subject(:setting) { create(:project_error_tracking_setting, project: project) }
describe 'Associations' do
it { is_expected.to belong_to(:project) }
......@@ -453,4 +453,23 @@ describe ErrorTracking::ProjectErrorTrackingSetting do
end
end
end
describe '#expire_issues_cache', :use_clean_rails_redis_caching do
let(:issues) { [:some, :issues] }
let(:opt) { 'list_issues' }
let(:params) { { issue_status: 'unresolved', limit: 20, sort: 'last_seen' } }
before do
start_reactive_cache_lifetime(subject, opt, params.stringify_keys)
stub_reactive_cache(subject, issues, opt, params.stringify_keys)
end
it 'clears the cache' do
expect(subject.list_sentry_issues(params)).to eq(issues)
subject.expire_issues_cache
expect(subject.list_sentry_issues(params)).to eq(nil)
end
end
end
......@@ -43,6 +43,12 @@ describe ErrorTracking::IssueUpdateService do
update_service.execute
end
it 'clears the reactive cache' do
expect(error_tracking_setting).to receive(:expire_issues_cache)
result
end
context 'related issue and resolving' do
let(:issue) { create(:issue, project: project) }
let(:sentry_issue) { create(:sentry_issue, issue: issue) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment