|
| 1 | +require "rails_helper" |
| 2 | +require "tmpdir" |
| 3 | +require "timeout" |
| 4 | + |
| 5 | +RSpec.describe Coplan::Notifications::FanoutBudget, "concurrent accounting" do |
| 6 | + self.use_transactional_tests = false |
| 7 | + |
| 8 | + let!(:actor) { create(:coplan_user) } |
| 9 | + |
| 10 | + around do |example| |
| 11 | + previous_cache = Rails.cache |
| 12 | + previous_limit = Coplan.configuration.notification_fanout_limit |
| 13 | + Coplan.configuration.notification_fanout_limit = 6 |
| 14 | + Dir.mktmpdir("coplan-fanout") do |directory| |
| 15 | + Rails.cache = ActiveSupport::Cache::FileStore.new(directory) |
| 16 | + freeze_time { example.run } |
| 17 | + end |
| 18 | + ensure |
| 19 | + Rails.cache = previous_cache |
| 20 | + Coplan.configuration.notification_fanout_limit = previous_limit |
| 21 | + end |
| 22 | + |
| 23 | + after { actor.destroy! } |
| 24 | + |
| 25 | + it "serializes the first spend before reading a missing cache entry" do |
| 26 | + entered = Queue.new |
| 27 | + release = Queue.new |
| 28 | + waiting = Queue.new |
| 29 | + cache = Rails.cache |
| 30 | + first_id = nil |
| 31 | + allow(cache).to receive(:read).and_wrap_original do |method, *args| |
| 32 | + if Thread.current.object_id == first_id |
| 33 | + entered << true |
| 34 | + Timeout.timeout(10) { release.pop } |
| 35 | + end |
| 36 | + method.call(*args) |
| 37 | + end |
| 38 | + |
| 39 | + first = Thread.new do |
| 40 | + first_id = Thread.current.object_id |
| 41 | + ActiveRecord::Base.connection_pool.with_connection do |
| 42 | + described_class.spend?(actor_id: actor.id, cost: 4) |
| 43 | + end |
| 44 | + end |
| 45 | + Timeout.timeout(10) { entered.pop } |
| 46 | + # The second worker reaches its real SQL lock while the first still has |
| 47 | + # not written the new key. A read/write without the row lock would let |
| 48 | + # both workers spend four against a six-delivery budget. |
| 49 | + second = Thread.new do |
| 50 | + ActiveRecord::Base.connection_pool.with_connection do |connection| |
| 51 | + # Signal just before executing the lock query, not after it completes. |
| 52 | + allow(connection).to receive(:log).and_wrap_original do |method, sql, *args, **kwargs, &block| |
| 53 | + waiting << true if sql.match?(/coplan_users.*FOR UPDATE/i) |
| 54 | + method.call(sql, *args, **kwargs, &block) |
| 55 | + end |
| 56 | + described_class.spend?(actor_id: actor.id, cost: 4) |
| 57 | + end |
| 58 | + end |
| 59 | + Timeout.timeout(10) { waiting.pop } |
| 60 | + release << true |
| 61 | + |
| 62 | + expect(first.join(20)).to eq(first) |
| 63 | + expect(second.join(20)).to eq(second) |
| 64 | + expect([ first.value, second.value ]).to eq([ true, false ]) |
| 65 | + expect(described_class.spend?(actor_id: actor.id, cost: 2)).to be(true) |
| 66 | + expect(described_class.spend?(actor_id: actor.id, cost: 1)).to be(false) |
| 67 | + ensure |
| 68 | + release << true if release |
| 69 | + [ first, second ].compact.each do |worker| |
| 70 | + worker.kill unless worker.join(5) |
| 71 | + worker.join |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + it "ignores an outer request's stale local value" do |
| 76 | + # Use Rails' real local-cache strategy over a shared backing store. |
| 77 | + Rails.cache.singleton_class.prepend(ActiveSupport::Cache::Strategy::LocalCache) |
| 78 | + window = Coplan.configuration.notification_fanout_window |
| 79 | + slot = Time.current.to_i / window |
| 80 | + key = "coplan/notification_fanout/v2/#{actor.id}/#{window}/#{slot}" |
| 81 | + Rails.cache.with_local_cache do |
| 82 | + Rails.cache.write(key, 0) |
| 83 | + Rails.cache.with_local_cache { Rails.cache.write(key, 6) } |
| 84 | + expect(Rails.cache.read(key)).to eq(0) |
| 85 | + expect(described_class.spend?(actor_id: actor.id, cost: 1)).to be(false) |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments