Skip to content

Commit

Permalink
tests: Move streaming tests to test_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vasi-stripe committed Nov 8, 2022
1 parent 4a853c8 commit 8beb160
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 63 deletions.
63 changes: 0 additions & 63 deletions tests/basic_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,69 +23,6 @@
end
end

# expected values: the response, in pieces, and a timeout after each piece
STREAMING_PIECES = %w{Hello streamy world}
STREAMING_TIMEOUT = 0.1

def streaming_tests
# expect the full response as a string
# and expect it to take a (timeout * pieces) seconds
tests('simple blocking request on streaming endpoint').returns([STREAMING_PIECES.join(''),'response time ok']) do
start = Time.now
ret = Excon.get('http://127.0.0.1:9292/streamed/simple').body

if Time.now - start <= STREAMING_TIMEOUT*3
[ret, 'streaming response came too quickly']
else
[ret, 'response time ok']
end
end

# expect the full response as a string and expect it to
# take a (timeout * pieces) seconds (with fixed Content-Length header)
tests('simple blocking request on streaming endpoint with fixed length').returns([STREAMING_PIECES.join(''),'response time ok']) do
start = Time.now
ret = Excon.get('http://127.0.0.1:9292/streamed/fixed_length').body

if Time.now - start <= STREAMING_TIMEOUT*3
[ret, 'streaming response came too quickly']
else
[ret, 'response time ok']
end
end

# expect each response piece to arrive to the body right away
# and wait for timeout until next one arrives
def timed_streaming_test(endpoint, timeout)
ret = []
timing = 'response times ok'
start = Time.now
Excon.get(endpoint, :response_block => lambda do |c,r,t|
# add the response
ret.push(c)
# check if the timing is ok
# each response arrives after timeout and before timeout + 1
cur_time = Time.now - start
if cur_time < ret.length * timeout or cur_time > (ret.length+1) * timeout
timing = 'response time not ok!'
end
end)
# validate the final timing
if Time.now - start <= timeout*3
timing = 'final timing was not ok!'
end
[ret, timing]
end

tests('simple request with response_block on streaming endpoint').returns([STREAMING_PIECES,'response times ok']) do
timed_streaming_test('http://127.0.0.1:9292/streamed/simple', STREAMING_TIMEOUT)
end

tests('simple request with response_block on streaming endpoint with fixed length').returns([STREAMING_PIECES,'response times ok']) do
timed_streaming_test('http://127.0.0.1:9292/streamed/fixed_length', STREAMING_TIMEOUT)
end
end

Shindo.tests('Excon streaming basics') do
pending if RUBY_PLATFORM == 'java' # need to find suitable server for jruby
with_unicorn('streaming.ru') do
Expand Down
64 changes: 64 additions & 0 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,70 @@ def basic_tests(url = 'http://127.0.0.1:9292', options = {})
end


# expected values: the response, in pieces, and a timeout after each piece
STREAMING_PIECES = %w{Hello streamy world}
STREAMING_TIMEOUT = 0.1

def streaming_tests
# expect the full response as a string
# and expect it to take a (timeout * pieces) seconds
tests('simple blocking request on streaming endpoint').returns([STREAMING_PIECES.join(''),'response time ok']) do
start = Time.now
ret = Excon.get('http://127.0.0.1:9292/streamed/simple').body

if Time.now - start <= STREAMING_TIMEOUT*3
[ret, 'streaming response came too quickly']
else
[ret, 'response time ok']
end
end

# expect the full response as a string and expect it to
# take a (timeout * pieces) seconds (with fixed Content-Length header)
tests('simple blocking request on streaming endpoint with fixed length').returns([STREAMING_PIECES.join(''),'response time ok']) do
start = Time.now
ret = Excon.get('http://127.0.0.1:9292/streamed/fixed_length').body

if Time.now - start <= STREAMING_TIMEOUT*3
[ret, 'streaming response came too quickly']
else
[ret, 'response time ok']
end
end

# expect each response piece to arrive to the body right away
# and wait for timeout until next one arrives
def timed_streaming_test(endpoint, timeout)
ret = []
timing = 'response times ok'
start = Time.now
Excon.get(endpoint, :response_block => lambda do |c,r,t|
# add the response
ret.push(c)
# check if the timing is ok
# each response arrives after timeout and before timeout + 1
cur_time = Time.now - start
if cur_time < ret.length * timeout or cur_time > (ret.length+1) * timeout
timing = 'response time not ok!'
end
end)
# validate the final timing
if Time.now - start <= timeout*3
timing = 'final timing was not ok!'
end
[ret, timing]
end

tests('simple request with response_block on streaming endpoint').returns([STREAMING_PIECES,'response times ok']) do
timed_streaming_test('http://127.0.0.1:9292/streamed/simple', STREAMING_TIMEOUT)
end

tests('simple request with response_block on streaming endpoint with fixed length').returns([STREAMING_PIECES,'response times ok']) do
timed_streaming_test('http://127.0.0.1:9292/streamed/fixed_length', STREAMING_TIMEOUT)
end
end


PROXY_ENV_VARIABLES = %w{http_proxy https_proxy no_proxy} # All lower-case

def env_init(env={})
Expand Down

0 comments on commit 8beb160

Please sign in to comment.