|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Hyperloop::Connection do |
| 4 | + |
| 5 | + before(:all) do |
| 6 | + Hyperloop.configuration do |config| |
| 7 | + end |
| 8 | + end |
| 9 | + |
| 10 | + before(:each) do |
| 11 | + Timecop.return |
| 12 | + end |
| 13 | + |
| 14 | + if ActiveRecord::Base.connection.respond_to? :data_sources |
| 15 | + it 'creates the tables (rails 5.x)' do |
| 16 | + ActiveRecord::Base.connection.data_sources.should include('hyperloop_connections') |
| 17 | + ActiveRecord::Base.connection.data_sources.should include('hyperloop_queued_messages') |
| 18 | + described_class.column_names.should =~ ['id', 'channel', 'session', 'created_at', 'expires_at', 'refresh_at'] |
| 19 | + end |
| 20 | + else |
| 21 | + it 'creates the tables (rails 4.x)' do |
| 22 | + ActiveRecord::Base.connection.tables.should include('hyperloop_connections') |
| 23 | + ActiveRecord::Base.connection.tables.should include('hyperloop_queued_messages') |
| 24 | + described_class.column_names.should =~ ['id', 'channel', 'session', 'created_at', 'expires_at', 'refresh_at'] |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + it 'creates the messages queue' do |
| 29 | + channel = described_class.new |
| 30 | + channel.messages << Hyperloop::Connection::QueuedMessage.new |
| 31 | + channel.save |
| 32 | + channel.reload |
| 33 | + channel.messages.should eq(Hyperloop::Connection::QueuedMessage.all) |
| 34 | + end |
| 35 | + |
| 36 | + it 'can set the root path' do |
| 37 | + described_class.root_path = 'foobar' |
| 38 | + expect(described_class.root_path).to eq('foobar') |
| 39 | + end |
| 40 | + |
| 41 | + it 'adding new connection' do |
| 42 | + described_class.open('TestChannel', 0) |
| 43 | + expect(described_class.active).to eq(['TestChannel']) |
| 44 | + end |
| 45 | + |
| 46 | + it 'new connections expire' do |
| 47 | + described_class.open('TestChannel', 0) |
| 48 | + expect(described_class.active).to eq(['TestChannel']) |
| 49 | + Timecop.travel(Time.now+described_class.transport.expire_new_connection_in) |
| 50 | + expect(described_class.active).to eq([]) |
| 51 | + end |
| 52 | + |
| 53 | + it 'can send and read data from a channel' do |
| 54 | + described_class.open('TestChannel', 0) |
| 55 | + described_class.open('TestChannel', 1) |
| 56 | + described_class.open('AnotherChannel', 0) |
| 57 | + described_class.send_to_channel('TestChannel', 'data') |
| 58 | + expect(described_class.read(0, 'path')).to eq(['data']) |
| 59 | + expect(described_class.read(0, 'path')).to eq([]) |
| 60 | + expect(described_class.read(1, 'path')).to eq(['data']) |
| 61 | + expect(described_class.read(1, 'path')).to eq([]) |
| 62 | + expect(described_class.read(0, 'path')).to eq([]) |
| 63 | + end |
| 64 | + |
| 65 | + it 'will update the expiration time after reading' do |
| 66 | + described_class.open('TestChannel', 0) |
| 67 | + described_class.send_to_channel('TestChannel', 'data') |
| 68 | + described_class.read(0, 'path') |
| 69 | + Timecop.travel(Time.now+described_class.transport.expire_new_connection_in) |
| 70 | + expect(described_class.active).to eq(['TestChannel']) |
| 71 | + end |
| 72 | + |
| 73 | + it 'will expire a polled connection' do |
| 74 | + described_class.open('TestChannel', 0) |
| 75 | + described_class.send_to_channel('TestChannel', 'data') |
| 76 | + described_class.read(0, 'path') |
| 77 | + Timecop.travel(Time.now+described_class.transport.expire_polled_connection_in) |
| 78 | + expect(described_class.active).to eq([]) |
| 79 | + end |
| 80 | + |
| 81 | + context 'after connecting to the transport' do |
| 82 | + before(:each) do |
| 83 | + described_class.open('TestChannel', 0) |
| 84 | + described_class.open('TestChannel', 1) |
| 85 | + described_class.send_to_channel('TestChannel', 'data') |
| 86 | + end |
| 87 | + |
| 88 | + it "will pass any pending data back" do |
| 89 | + expect(described_class.connect_to_transport('TestChannel', 0, nil)).to eq(['data']) |
| 90 | + end |
| 91 | + |
| 92 | + it "will have the root path set for console access" do |
| 93 | + described_class.connect_to_transport('TestChannel', 0, "some_path") |
| 94 | + expect(Hyperloop::Connection.root_path).to eq("some_path") |
| 95 | + end |
| 96 | + |
| 97 | + it "the channel will still be active even after initial connection time is expired" do |
| 98 | + described_class.connect_to_transport('TestChannel', 0, nil) |
| 99 | + Timecop.travel(Time.now+described_class.transport.expire_new_connection_in) |
| 100 | + expect(described_class.active).to eq(['TestChannel']) |
| 101 | + end |
| 102 | + |
| 103 | + it "will only effect the session being connected" do |
| 104 | + described_class.connect_to_transport('TestChannel', 0, nil) |
| 105 | + expect(described_class.read(1, 'path')).to eq(['data']) |
| 106 | + end |
| 107 | + |
| 108 | + it "will begin refreshing the channel list" do |
| 109 | + allow(Hyperloop).to receive(:refresh_channels) {['AnotherChannel']} |
| 110 | + described_class.open('AnotherChannel', 0) |
| 111 | + described_class.connect_to_transport('TestChannel', 0, nil) |
| 112 | + described_class.connect_to_transport('AnotherChannel', 0, nil) |
| 113 | + expect(described_class.active).to eq(['TestChannel', 'AnotherChannel']) |
| 114 | + Timecop.travel(Time.now+described_class.transport.refresh_channels_every) |
| 115 | + expect(described_class.active).to eq(['AnotherChannel']) |
| 116 | + end |
| 117 | + |
| 118 | + it "refreshing will not effect channels not connected to the transport" do |
| 119 | + allow(Hyperloop).to receive(:refresh_channels) {['AnotherChannel']} |
| 120 | + described_class.open('AnotherChannel', 0) |
| 121 | + described_class.connect_to_transport('TestChannel', 0, nil) |
| 122 | + Timecop.travel(Time.now+described_class.transport.refresh_channels_every-1) |
| 123 | + described_class.read(1, 'path') |
| 124 | + described_class.connect_to_transport('AnotherChannel', 0, nil) |
| 125 | + expect(described_class.active).to eq(['TestChannel', 'AnotherChannel']) |
| 126 | + Timecop.travel(Time.now+1) |
| 127 | + described_class.active |
| 128 | + expect(described_class.active).to eq(['TestChannel', 'AnotherChannel']) |
| 129 | + end |
| 130 | + |
| 131 | + it "refreshing will not effect channels added during the refresh" do |
| 132 | + allow(Hyperloop).to receive(:refresh_channels) do |
| 133 | + described_class.connect_to_transport('TestChannel', 0, nil) |
| 134 | + ['AnotherChannel'] |
| 135 | + end |
| 136 | + described_class.open('AnotherChannel', 0) |
| 137 | + Timecop.travel(Time.now+described_class.transport.refresh_channels_every) |
| 138 | + described_class.read(0, 'path') |
| 139 | + described_class.connect_to_transport('AnotherChannel', 0, nil) |
| 140 | + expect(described_class.active).to eq(['TestChannel', 'AnotherChannel']) |
| 141 | + described_class.open('TestChannel', 2) |
| 142 | + expect(described_class.active).to eq(['TestChannel', 'AnotherChannel']) |
| 143 | + end |
| 144 | + |
| 145 | + it "sends messages to the transport as well as open channels" do |
| 146 | + expect(Hyperloop).to receive(:send_data).with('TestChannel', 'data2') |
| 147 | + described_class.connect_to_transport('TestChannel', 0, nil) |
| 148 | + described_class.send_to_channel('TestChannel', 'data2') |
| 149 | + expect(described_class.read(1, 'path')).to eq(['data', 'data2']) |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments