github.com/anycable/anycable-go@v1.5.1/etc/anyt/broadcast_tests/options_test.rb (about)

     1  # frozen_string_literal: true
     2  
     3  feature "Broadcast with options" do
     4    channel do
     5      def subscribed
     6        stream_from "a"
     7      end
     8  
     9      def speak(data)
    10        data.delete("action")
    11        ActionCable.server.broadcast("a", data, to_others: true)
    12      end
    13    end
    14  
    15    let(:client2) { build_client(ignore: %w[ping welcome]) }
    16    let(:client3) { build_client(ignore: %w[ping welcome]) }
    17  
    18    before do
    19      subscribe_request = {command: "subscribe", identifier: {channel: channel}.to_json}
    20  
    21      client.send(subscribe_request)
    22      client2.send(subscribe_request)
    23      client3.send(subscribe_request)
    24  
    25      ack = {
    26        "identifier" => {channel: channel}.to_json, "type" => "confirm_subscription"
    27      }
    28  
    29      assert_message ack, client.receive
    30      assert_message ack, client2.receive
    31      assert_message ack, client3.receive
    32    end
    33  
    34    scenario %(
    35      Only other clients receive the message when broadcasted to others
    36    ) do
    37      perform_request = {
    38        :command => "message",
    39        :identifier => {channel: channel}.to_json,
    40        "data" => {"action" => "speak", "content" => "The Other Side"}.to_json
    41      }
    42  
    43      client.send(perform_request)
    44  
    45      msg = {"identifier" => {channel: channel}.to_json, "message" => {"content" => "The Other Side"}}
    46  
    47      assert_message msg, client2.receive
    48      assert_message msg, client3.receive
    49      assert_raises(Anyt::Client::TimeoutError) do
    50        msg = client.receive(timeout: 0.5)
    51        raise "Client 1 should not receive the message: #{msg}"
    52      end
    53    end
    54  end