Search
Project
General
Profile
Sign in
Register
Home
Projects
Help
Search
:
Ruby 1.8
All Projects
Ruby
»
Ruby 1.8
Overview
Activity
Roadmap
Issues
Wiki
Like
Download (1.35 KB)
Bug #286
ยป openuritest.rb
Anonymous, 07/18/2008 09:26 AM
require
'net/http'
require
'uri'
require
'open-uri'
# can use 'lsof -p PID | wc -l' to watch file handles leak
# sleep is included to help track open file handles as script processes
class
Leaker
def
initialize
while
(
true
)
sleep
(
10
)
use_net_http
(
"http://www.amazon.com/gp/offer-listing/B000OHBBS4/"
)
# 404, no leak
sleep
(
10
)
use_open_uri
(
"http://www.amazon.com/gp/offer-listing/B000OHBBS4/"
)
# 404, leaks
sleep
(
10
)
use_open_uri
(
"http://www.example.com/invalid/"
)
# 404, no leak
sleep
(
10
)
use_open_uri
(
"http://www.amazon.com/gp/offer-listing/0974514055/"
)
# 200, no leak
end
end
def
use_net_http
(
uri
)
url
=
URI
.
parse
(
uri
)
Net
::
HTTP
.
version_1_1
req
=
Net
::
HTTP
::
Get
.
new
(
url
.
path
)
res
=
Net
::
HTTP
.
start
(
url
.
host
,
url
.
port
)
{
|
http
|
http
.
request
(
req
)
}
if
res
.
kind_of?
(
Net
::
HTTPSuccess
)
puts
"Retrieved response at
#{
url
}
"
else
puts
"Failure retrieving response at
#{
url
}
:
#{
res
.
code
}
"
end
rescue
=>
ex
puts
"Error with Net::HTTP retrieving response at
#{
url
}
:
#{
ex
}
"
end
def
use_open_uri
(
url
)
open
(
url
,
"User-Agent"
=>
"Open-uri for Ruby(Leaky Lib)"
)
do
|
handle
|
html
=
handle
.
read
puts
"Retrieved response at
#{
url
}
"
end
rescue
=>
ex
puts
"Error with open-uri retrieving response at
#{
url
}
:
#{
ex
}
"
end
end
Leaker
.
new
(1-1/1)
Loading...