Skip to content

Commit

Permalink
etc: Rails-specific test examples
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Sep 14, 2024
1 parent 5f2c3c2 commit 727ed90
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
107 changes: 107 additions & 0 deletions etc/tests/interceptors_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: true

feature "Streams" do
channel do
def subscribed
transformer = params[:transformer].to_sym

stream_from "a", coder: ActiveSupport::JSON do |message|
message["text"] = message["text"].send(transformer)
transmit(message)
end

# No coder
stream_from "b" do |message|
# transmit as is
transmit(message)
end

# No interceptor
stream_from "c"
end
end

let(:client2) { build_client(ignore: %w[ping welcome]) }

before do
@identifier = {channel:, transformer: "reverse"}.to_json

subscribe_request = {command: "subscribe", identifier: @identifier}

client.send(subscribe_request)

ack = {
"identifier" => @identifier, "type" => "confirm_subscription"
}

assert_message ack, client.receive

@identifier2 = {channel:, transformer: "upcase"}.to_json

subscribe_request = {command: "subscribe", identifier: @identifier2}

client2.send(subscribe_request)

ack = {
"identifier" => @identifier2, "type" => "confirm_subscription"
}

assert_message ack, client2.receive
end

scenario %(
Intercept with coder
) do
ActionCable.server.broadcast("a", {text: "cable"})

msg = {
"identifier" => @identifier,
"message" => {"text" => "elbac"}
}

assert_message msg, client.receive

msg = {
"identifier" => @identifier2,
"message" => {"text" => "CABLE"}
}

assert_message msg, client2.receive
end

scenario %(
Intercept without coder
) do
ActionCable.server.broadcast("b", {text: "cable"})

msg = {
"identifier" => @identifier,
"message" => {"text" => "cable"}.to_json
}

assert_message msg, client.receive

msg = {
"identifier" => @identifier2,
"message" => {"text" => "cable"}.to_json
}

assert_message msg, client2.receive

ActionCable.server.broadcast("c", {text: "cable"})

msg = {
"identifier" => @identifier,
"message" => {"text" => "cable"}
}

assert_message msg, client.receive

msg = {
"identifier" => @identifier2,
"message" => {"text" => "cable"}
}

assert_message msg, client2.receive
end
end
44 changes: 44 additions & 0 deletions etc/tests/periodic_timers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

feature "Subscriptions" do
channel do
periodically :transmit_progress, every: 1.second

state_attr_accessor :progress

def subscribed
self.progress = 0
end

def transmit_progress
self.progress += 1

transmit({progress:})
end
end

before do
@identifier = {channel:}.to_json

subscribe_request = {command: "subscribe", identifier: @identifier}

client.send(subscribe_request)

ack = {
"identifier" => @identifier, "type" => "confirm_subscription"
}

assert_message ack, client.receive
end

scenario %(
Periodic timers
) do

msg = client.receive
assert_equal 1, msg["message"]["progress"]

msg = client.receive
assert_equal 2, msg["message"]["progress"]
end
end

0 comments on commit 727ed90

Please sign in to comment.