github.com/anycable/anycable-go@v1.5.1/etc/anyt/broker_tests/history_test.rb (about) 1 # frozen_string_literal: true 2 3 feature "History" do 4 channel do 5 def subscribed 6 stream_from "history_a" 7 end 8 end 9 10 before do 11 ActionCable.server.broadcast( 12 "history_a", 13 {data: {user_id: 1, status: "left"}} 14 ) 15 16 ActionCable.server.broadcast( 17 "history_a", 18 {data: {user_id: 2, status: "join"}} 19 ) 20 21 # Make sure broadcasts have been published 22 sleep 2 23 end 24 25 scenario %( 26 Fetching by timestamp 27 ) do 28 subscribe_request = {command: "subscribe", identifier: {channel: channel}.to_json} 29 30 client.send(subscribe_request) 31 32 ack = { 33 "identifier" => {channel: channel}.to_json, "type" => "confirm_subscription" 34 } 35 36 assert_equal ack, client.receive 37 38 history_request = { 39 command: "history", 40 identifier: {channel: channel}.to_json, 41 history: { 42 since: Time.now.to_i - 30 43 } 44 } 45 46 client.send(history_request) 47 48 msg = { 49 "identifier" => {channel: channel}.to_json, 50 "message" => { 51 "data" => {"user_id" => 1, "status" => "left"} 52 } 53 } 54 55 msg_2 = { 56 "identifier" => {channel: channel}.to_json, 57 "message" => { 58 "data" => {"user_id" => 2, "status" => "join"} 59 } 60 } 61 62 assert_message msg, client.receive 63 assert_message msg_2, client.receive 64 end 65 66 scenario %( 67 Fetching by offset 68 ) do 69 subscribe_request = {command: "subscribe", identifier: {channel: channel}.to_json} 70 71 client.send(subscribe_request) 72 73 ack = { 74 "identifier" => {channel: channel}.to_json, "type" => "confirm_subscription" 75 } 76 77 assert_equal ack, client.receive 78 79 ActionCable.server.broadcast( 80 "history_a", 81 {data: {user_id: 42, status: "alive"}} 82 ) 83 84 msg = { 85 "identifier" => {channel: channel}.to_json, 86 "message" => { 87 "data" => {"user_id" => 42, "status" => "alive"} 88 } 89 } 90 91 received = client.receive 92 93 assert_message msg, received 94 95 assert_includes received, "stream_id" 96 assert_includes received, "offset" 97 assert_includes received, "epoch" 98 99 another_client = build_client(ignore: %w[ping welcome]) 100 another_client.send(subscribe_request) 101 assert_equal ack, another_client.receive 102 103 history_request = { 104 command: "history", 105 identifier: {channel: channel}.to_json, 106 history: { 107 streams: { 108 received["stream_id"] => { 109 offset: received["offset"] - 1, 110 epoch: received["epoch"] 111 } 112 } 113 } 114 } 115 116 another_client.send(history_request) 117 118 assert_message msg, another_client.receive 119 end 120 121 scenario %( 122 Subscribing with history 123 ) do 124 subscribe_request = { 125 command: "subscribe", 126 identifier: {channel: channel}.to_json, 127 history: { 128 since: Time.now.to_i - 30 129 } 130 } 131 132 client.send(subscribe_request) 133 134 ack = { 135 "identifier" => {channel: channel}.to_json, "type" => "confirm_subscription" 136 } 137 138 assert_equal ack, client.receive 139 140 msg = { 141 "identifier" => {channel: channel}.to_json, 142 "message" => { 143 "data" => {"user_id" => 1, "status" => "left"} 144 } 145 } 146 147 msg_2 = { 148 "identifier" => {channel: channel}.to_json, 149 "message" => { 150 "data" => {"user_id" => 2, "status" => "join"} 151 } 152 } 153 154 assert_message msg, client.receive 155 assert_message msg_2, client.receive 156 end 157 end