Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
theseanything committed Nov 12, 2024
1 parent 5ff28b0 commit c1a2b2f
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions spec/integration/notify_route_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

def postgres_listener(channel)
queue = Queue.new
p "Creating postgres listenter"

Thread.new do
thr = Thread.new do
p "Created new thread"
conn = ActiveRecord::Base.lease_connection
conn.execute("LISTEN #{channel}")
p "Starting loop"
loop do
conn.raw_connection.wait_for_notify do |event, _id, _data|
p "Got notify event"
queue << event
end
end
ensure
p "Closing thread"
conn.execute "UNLISTEN #{channel}"
end

p "Sleeping...."
# Wait for the thread to be ready to listen for nofitications
sleep 0.1
sleep 1

queue
p "Slept"
[queue, thr]
end

describe "Postgres trigger", type: :request, skip_db_cleaner: true do
Expand All @@ -29,49 +36,59 @@ def postgres_listener(channel)
end

it "sends a notification when content item created" do
listener = postgres_listener("route_changes")
p "start test"
listener, thr = postgres_listener("route_changes")

p "create item"
create(:content_item)

p "expectation"
expect(listener.pop).to eq("route_changes")
thr.exit
end

it "sends a notification when content item updated" do
content_item = create(:content_item)

listener = postgres_listener("route_changes")
listener, thr = postgres_listener("route_changes")
content_item.update!(base_path: "/foo")

expect(listener.pop).to eq("route_changes")
thr.exit
end

it "sends a notification when content item destroyed" do
content_item = create(:content_item)
listener = postgres_listener("route_changes")
listener, thr = postgres_listener("route_changes")
content_item.destroy!

expect(listener.pop).to eq("route_changes")
thr.exit
end

it "sends a notification when publish intent created" do
listener = postgres_listener("route_changes")
listener, thr = postgres_listener("route_changes")
create(:publish_intent)

expect(listener.pop).to eq("route_changes")
thr.exit
end

it "sends a notification when publish intent updated" do
publish_intent = create(:publish_intent)
listener = postgres_listener("route_changes")
listener, thr = postgres_listener("route_changes")
publish_intent.update!(publish_time: 10.minutes.from_now)

expect(listener.pop).to eq("route_changes")
thr.exit
end

it "sends a notification when publish intent destroyed" do
publish_intent = create(:publish_intent)
listener = postgres_listener("route_changes")
listener, thr = postgres_listener("route_changes")
publish_intent.destroy!

expect(listener.pop).to eq("route_changes")
thr.exit
end
end

0 comments on commit c1a2b2f

Please sign in to comment.