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

     1  # frozen_string_literal: true
     2  
     3  feature "Session cache" do
     4    channel do
     5      def subscribed
     6        stream_from "cache_a"
     7      end
     8    end
     9  
    10    before do
    11      client = build_client(ignore: ["ping"], protocol: "actioncable-v1-ext-json")
    12  
    13      welcome_msg = client.receive
    14      assert_message({ "type" => "welcome" }, welcome_msg)
    15  
    16      assert_includes welcome_msg, "sid"
    17  
    18      @sid = welcome_msg["sid"]
    19  
    20      subscribe_request = {command: "subscribe", identifier: {channel: channel}.to_json}
    21      client.send(subscribe_request)
    22  
    23      ack = {
    24        "identifier" => {channel: channel}.to_json, "type" => "confirm_subscription"
    25      }
    26  
    27      assert_equal ack, client.receive
    28  
    29      ActionCable.server.broadcast(
    30        "cache_a",
    31        {data: {user_id: 1, status: "left"}}
    32      )
    33  
    34      msg = {
    35        "identifier" => {channel: channel}.to_json,
    36        "message" => {
    37          "data" => {"user_id" => 1, "status" => "left"}
    38        }
    39      }
    40  
    41      assert_message msg, client.receive
    42    end
    43  
    44    scenario %(
    45      Restore session by session ID
    46    ) do
    47      client.close
    48  
    49      another_client = build_client(
    50        ignore: ["ping"],
    51        headers: {
    52          "X-ANYCABLE-RESTORE-SID" => @sid
    53        },
    54        protocol: "actioncable-v1-ext-json"
    55      )
    56  
    57      assert_message({ "type" => "welcome", "restored" => true }, another_client.receive)
    58  
    59      ActionCable.server.broadcast(
    60        "cache_a",
    61        {data: {user_id: 2, status: "join"}}
    62      )
    63  
    64      msg = {
    65        "identifier" => {channel: channel}.to_json,
    66        "message" => {
    67          "data" => {"user_id" => 2, "status" => "join"}
    68        }
    69      }
    70  
    71      assert_message msg, another_client.receive
    72    end
    73  end