|
3 | 3 | require "flores/pki"
|
4 | 4 |
|
5 | 5 | describe LogStash::Outputs::Tcp do
|
6 |
| - subject { described_class.new(config) } |
| 6 | + subject(:instance) { described_class.new(config) } |
7 | 7 | let(:config) { {
|
8 | 8 | "host" => "localhost",
|
9 | 9 | "port" => 2000 + rand(3000),
|
|
73 | 73 | end
|
74 | 74 | end
|
75 | 75 | end
|
| 76 | + |
| 77 | + context 'client mode' do |
| 78 | + context 'transmitting data' do |
| 79 | + let!(:io) { StringIO.new } # somewhere for our server to stash the data it receives |
| 80 | + |
| 81 | + let(:server_host) { 'localhost' } |
| 82 | + let(:server_port) { server.addr[1] } # get actual since we bind to port 0 |
| 83 | + |
| 84 | + let!(:server) { TCPServer.new(server_host, 0) } |
| 85 | + |
| 86 | + let(:config) do |
| 87 | + { 'host' => server_host, 'port' => server_port, 'mode' => 'client' } |
| 88 | + end |
| 89 | + |
| 90 | + let(:event) { LogStash::Event.new({"hello" => "world"})} |
| 91 | + |
| 92 | + subject(:instance) { described_class.new(config) } |
| 93 | + |
| 94 | + before(:each) do |
| 95 | + # accepts ONE connection |
| 96 | + @server_socket_thread = Thread.start do |
| 97 | + client = server.accept |
| 98 | + io.write(client.read) |
| 99 | + end |
| 100 | + instance.register |
| 101 | + end |
| 102 | + |
| 103 | + after(:each) do |
| 104 | + @server_socket_thread&.join |
| 105 | + end |
| 106 | + |
| 107 | + it 'encodes and transmits data' do |
| 108 | + instance.receive(event) |
| 109 | + instance.close # release the connection |
| 110 | + sleep 1 |
| 111 | + expect(io.string).to include('"hello"','"world"') |
| 112 | + end |
| 113 | + |
| 114 | + context 'when payload is very large' do |
| 115 | + let(:one_hundred_megabyte_message) { "a" * 1024 * 1024 * 100 } |
| 116 | + let(:event) { LogStash::Event.new("message" => one_hundred_megabyte_message) } |
| 117 | + |
| 118 | + |
| 119 | + it 'encodes and transmits data' do |
| 120 | + instance.receive(event) |
| 121 | + instance.close # release the connection |
| 122 | + sleep 1 |
| 123 | + expect(io.string).to include('"message"',%Q("#{one_hundred_megabyte_message}")) |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + context 'server mode' do |
| 130 | + context 'transmitting data' do |
| 131 | + let(:server_host) { 'localhost' } |
| 132 | + let(:server_port) { Random.rand(1024...5000) } |
| 133 | + |
| 134 | + let(:config) do |
| 135 | + { 'host' => server_host, 'port' => server_port, 'mode' => 'server' } |
| 136 | + end |
| 137 | + |
| 138 | + subject(:instance) { described_class.new(config) } |
| 139 | + |
| 140 | + before(:each) { instance.register } # start listener |
| 141 | + after(:each) { instance.close } |
| 142 | + |
| 143 | + let(:event) { LogStash::Event.new({"hello" => "world"})} |
| 144 | + |
| 145 | + context 'when one client is connected' do |
| 146 | + let(:io) { StringIO.new } |
| 147 | + let(:client_socket) { TCPSocket.new(server_host, server_port) } |
| 148 | + |
| 149 | + before(:each) do |
| 150 | + @client_socket_thread = Thread.start { io.write client_socket.read } |
| 151 | + sleep 1 # wait for it to actually connect |
| 152 | + end |
| 153 | + |
| 154 | + it 'encodes and transmits data' do |
| 155 | + instance.receive(event) |
| 156 | + |
| 157 | + sleep 1 # wait for the event to get sent... |
| 158 | + instance.close # release the connection |
| 159 | + |
| 160 | + @client_socket_thread.join(30) || fail('client failed to join') |
| 161 | + expect(io.string).to include('"hello"','"world"') |
| 162 | + end |
| 163 | + |
| 164 | + context 'when payload is very large' do |
| 165 | + let(:one_hundred_megabyte_message) { "a" * 1024 * 1024 * 100 } |
| 166 | + let(:event) { LogStash::Event.new("message" => one_hundred_megabyte_message) } |
| 167 | + |
| 168 | + it 'encodes and transmits data' do |
| 169 | + instance.receive(event) |
| 170 | + |
| 171 | + sleep 1 # wait for the event to get sent... |
| 172 | + instance.close # release the connection |
| 173 | + |
| 174 | + @client_socket_thread.join(30) || fail('client failed to join') |
| 175 | + expect(io.string).to include('"message"',%Q("#{one_hundred_megabyte_message}")) |
| 176 | + end |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end |
76 | 181 | end
|
0 commit comments