forked from brandur/rocket-rides-unified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.rb
69 lines (56 loc) · 1.27 KB
/
simulator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require "net/http"
require "securerandom"
require_relative "./api"
class Simulator
def initialize(port:)
self.port = port
end
def run
count=1
loop do
# 50.times do
run_once
if count<10
duration = rand * 2
else
duration=600
end
# duration = rand(0.001..0.003)
$stdout.puts "Sleeping for #{duration}"
sleep(duration)
count+=1
end
end
def run_once
http = Net::HTTP.new("localhost", port)
request = Net::HTTP::Post.new("/rides")
distance = "#%02x%02x%02x" % [rand(MIN_COLOR..MAX_COLOR),rand(MIN_COLOR..MAX_COLOR) ,rand(MIN_COLOR..MAX_COLOR) ]
distance.upcase!
request.set_form_data({
#"color" => rand * (MAX_COLOR - MIN_COLOR) + MIN_COLOR
"distance" => distance
})
response = http.request(request)
$stdout.puts "Response: status=#{response.code} body=#{response.body}"
end
#
# private
#
MAX_COLOR = 255
private_constant :MAX_COLOR
MIN_COLOR = 1
private_constant :MIN_COLOR
attr_accessor :port
end
#
# run
#
if __FILE__ == $0
# so output appears in Forego
$stderr.sync = true
$stdout.sync = true
port = ENV["API_PORT"] || abort("need API_PORT")
# wait a moment for the API to come up
sleep(3)
Simulator.new(port: port).run
end