|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "database_test_helper" |
| 4 | +require "timeout" |
| 5 | + |
| 6 | +class PostgresqlWakeUpTest < ActiveSupport::TestCase |
| 7 | + setup do |
| 8 | + skip unless postgresql? |
| 9 | + |
| 10 | + @adapter = SolidObjects::WakeUpAdapters::Postgresql.new |
| 11 | + end |
| 12 | + |
| 13 | + teardown { @adapter&.stop } |
| 14 | + |
| 15 | + test "a signal from another connection wakes a waiter" do |
| 16 | + waiting = Queue.new |
| 17 | + woken = Queue.new |
| 18 | + waiter = Thread.new do |
| 19 | + @adapter.listen |
| 20 | + waiting << true |
| 21 | + started = monotonic_now |
| 22 | + @adapter.wait(timeout: 5) |
| 23 | + woken << monotonic_now - started |
| 24 | + end |
| 25 | + Timeout.timeout(5) { waiting.pop } |
| 26 | + |
| 27 | + signal_from_another_connection |
| 28 | + |
| 29 | + elapsed = Timeout.timeout(5) { woken.pop } |
| 30 | + waiter.join(5) |
| 31 | + assert_operator elapsed, :<, 1.0, |
| 32 | + "a cross-connection signal should wake the waiter well before the timeout" |
| 33 | + end |
| 34 | + |
| 35 | + test "waiting returns after the timeout when nothing signals" do |
| 36 | + @adapter.listen |
| 37 | + started = monotonic_now |
| 38 | + |
| 39 | + @adapter.wait(timeout: 0.2) |
| 40 | + |
| 41 | + elapsed = monotonic_now - started |
| 42 | + assert_operator elapsed, :>=, 0.15 |
| 43 | + assert_operator elapsed, :<, 2.0 |
| 44 | + end |
| 45 | + |
| 46 | + test "every waiter wakes on one signal" do |
| 47 | + waiters = 3 |
| 48 | + waiting = Queue.new |
| 49 | + woken = Queue.new |
| 50 | + adapters = Array.new(waiters) { SolidObjects::WakeUpAdapters::Postgresql.new } |
| 51 | + threads = adapters.map do |adapter| |
| 52 | + Thread.new do |
| 53 | + adapter.listen |
| 54 | + waiting << true |
| 55 | + adapter.wait(timeout: 5) |
| 56 | + woken << true |
| 57 | + end |
| 58 | + end |
| 59 | + waiters.times { Timeout.timeout(5) { waiting.pop } } |
| 60 | + |
| 61 | + signal_from_another_connection |
| 62 | + |
| 63 | + waiters.times { Timeout.timeout(5) { woken.pop } } |
| 64 | + threads.each { |thread| thread.join(5) } |
| 65 | + assert_equal 0, woken.size |
| 66 | + ensure |
| 67 | + adapters&.each(&:stop) |
| 68 | + end |
| 69 | + |
| 70 | + test "signalling never raises into the caller" do |
| 71 | + broken = SolidObjects::WakeUpAdapters::Postgresql.new(channel: "solid_objects_missing") |
| 72 | + broken.define_singleton_method(:notify_channel) { raise "boom" } |
| 73 | + |
| 74 | + assert_nothing_raised { broken.signal } |
| 75 | + ensure |
| 76 | + broken&.stop |
| 77 | + end |
| 78 | + |
| 79 | + test "waiting never raises into the caller" do |
| 80 | + broken = SolidObjects::WakeUpAdapters::Postgresql.new |
| 81 | + broken.define_singleton_method(:listening_connection) { raise "boom" } |
| 82 | + started = monotonic_now |
| 83 | + |
| 84 | + assert_nothing_raised { broken.wait(timeout: 0.1) } |
| 85 | + |
| 86 | + assert_operator monotonic_now - started, :>=, 0.05, |
| 87 | + "a failed wait must still pace itself rather than spin" |
| 88 | + ensure |
| 89 | + broken&.stop |
| 90 | + end |
| 91 | + |
| 92 | + test "the adapter satisfies the wake-up contract" do |
| 93 | + assert_respond_to @adapter, :signal |
| 94 | + assert_respond_to @adapter, :wait |
| 95 | + SolidObjects.configuration.wake_up_adapter = @adapter |
| 96 | + |
| 97 | + assert_same @adapter, SolidObjects.wake_up |
| 98 | + ensure |
| 99 | + SolidObjects.configuration.wake_up_adapter = nil |
| 100 | + SolidObjects.instance_variable_set(:@wake_up, nil) |
| 101 | + end |
| 102 | + |
| 103 | + private |
| 104 | + |
| 105 | + def postgresql? |
| 106 | + SolidObjects::Record.connection.adapter_name.match?(/postgres/i) |
| 107 | + end |
| 108 | + |
| 109 | + def monotonic_now |
| 110 | + ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) |
| 111 | + end |
| 112 | + |
| 113 | + # Models a commit in a different process: a plain NOTIFY on its own connection. |
| 114 | + def signal_from_another_connection |
| 115 | + Thread.new { SolidObjects::WakeUpAdapters::Postgresql.new.signal }.join(5) |
| 116 | + end |
| 117 | +end |
0 commit comments