From 7bf4a33d22c1d3ce38c7531026a8fb52d9742f40 Mon Sep 17 00:00:00 2001 From: Ricardo Martins Date: Mon, 9 Oct 2023 14:07:27 +0100 Subject: [PATCH 1/2] Fix decrement on ActiveSupport 7.1 --- lib/prop/interval_strategy.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/prop/interval_strategy.rb b/lib/prop/interval_strategy.rb index c57cacd..78ce84e 100644 --- a/lib/prop/interval_strategy.rb +++ b/lib/prop/interval_strategy.rb @@ -21,7 +21,15 @@ def increment(cache_key, amount, options = {}) def decrement(cache_key, amount, options = {}) raise ArgumentError, "Change amount must be a Integer, was #{amount.class}" unless amount.is_a?(Integer) - cache.decrement(cache_key, amount, expires_in: options.fetch(:interval, nil)) || (cache.write(cache_key, 0, raw: true, expires_in: options.fetch(:interval, nil)) && 0) # WARNING: potential race condition + + new_value = cache.decrement(cache_key, amount, expires_in: options.fetch(:interval, nil)) + + # In ActiveSupport < 7.1, decrement on a new key does nothing and returns nil. + # In ActiveSupport >= 7.1, decrement on a new key sets its value to 0 first and then decrements, returning a negative value. + if new_value.nil? || new_value == -amount + # WARNING: potential race condition + cache.write(cache_key, 0, raw: true, expires_in: options.fetch(:interval, nil)) && 0 + end end def reset(cache_key, options = {}) From cdba2510ae2afedaf358093e275c9a97184de01b Mon Sep 17 00:00:00 2001 From: Ricardo Martins Date: Mon, 9 Oct 2023 14:08:37 +0100 Subject: [PATCH 2/2] Fix missing requires in ActiveSupport 7.1 --- test/helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/helper.rb b/test/helper.rb index d9127a2..56db35a 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -11,6 +11,14 @@ require 'active_support/cache/memory_store' require 'active_support/notifications' +begin + require 'active_support/deprecation' + require 'active_support/deprecator' +rescue LoadError +end + +require 'active_support/core_ext/numeric/time' + Minitest::Test.class_eval do def setup_fake_store Prop.cache = ActiveSupport::Cache::MemoryStore.new