thread per received HTTP request. Server keeps receiving next requests. We can treat received request synchronously. We don't have to care subsequent requests.
supports WebSocket. To support it, web application needs asynchronous functions. Why? Because we can’t create threads automatically for WebSocket messages. Why?
messages on the WebSocket connections. Type of messages and amount of messages depend on the application. If we want to send a big message in split chunk. Number of chunks may be 10 thousands. Can we create threads per messages? They will spent huge amount of memory.
create a thread by myself and run the function in it. We wait end of it and send messages on the WebSocket connection. This is an asynchronous function.
WebSocket connection. ws = Faye::WebSocket.new(env) ws.on :open do async_func do |result| # Send results on the WebSocket connection. ws.send result end end ws.rack_response end
exception. But, this assertion runs in sub thread. it 'is expected answer' do async_func do |result| # --- in sub thread --- expect(result).not_to eq('expected answer') # --- in sub thread --- end end
queue = Queue.new async_func do |r| # --- in sub thread --- queue.push r # --- in sub thread --- end # Wait to `queue.push` from other threads. result = queue.pop # Resume next code when `queue.push` is called. expect(result).not_to eq('expected answer') end
https://rubygems.org/gems/async_play it 'result is details' do result = AsyncPlay.opening do |curtain| async_func do |val| curtain.call val end end expect(result).to eq('expected answer') end