Skip to content

Instantly share code, notes, and snippets.

@mrThe
Created August 3, 2016 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrThe/97c782561e27cf5065d66656db205231 to your computer and use it in GitHub Desktop.
Save mrThe/97c782561e27cf5065d66656db205231 to your computer and use it in GitHub Desktop.
Simple http race condition helper. It can be more usable, but no one cares.
require 'socket'
def create_request(data)
host = '127.0.0.1'
port = 80
request = ''
request << "POST /something.json HTTP/1.1\r\n"
request << "Host: localhost\r\n"
# add here your headers
request << "Content-Length: #{data.size}\r\n"
socket = TCPSocket.open(host,port)
socket.print(request)
socket.print("\r\n")
# socket.print(data)
socket
end
pew_pew = false
tt = []
ss = []
50.times do |i|
tt << Thread.new do
data = "post data here"
socket = create_request(data)
ss << socket
puts "Thread #{i} done!"
sleep 0.00001 until pew_pew
socket.print(data)
socket.print("\r\n\r\n")
socket.flush
puts "\n\n\n==========="
puts socket.read
puts "===========\n\n\n"
end
end
# run it on irb, then wait for all threads spawn, then set pew_pew to true: `pew_pew = true`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment