Commit 0cc85ac2 authored by Denis Bilenko's avatar Denis Bilenko

test__http.py: make the test cases pass totalrefcount check

parent 25fa1c14
...@@ -3,9 +3,9 @@ import gevent ...@@ -3,9 +3,9 @@ import gevent
from gevent import http from gevent import http
import greentest import greentest
import os import os
from urllib2 import urlopen, HTTPError
import socket import socket
import errno import errno
from test__pywsgi import read_http
# add test for "chunked POST input -> chunked output" # add test for "chunked POST input -> chunked output"
...@@ -15,6 +15,7 @@ class BoundTestCase(greentest.TestCase): ...@@ -15,6 +15,7 @@ class BoundTestCase(greentest.TestCase):
address = ('127.0.0.1', 0) address = ('127.0.0.1', 0)
def setUp(self): def setUp(self):
greentest.TestCase.setUp(self)
self.server = http.HTTPServer(self.address, self.handle) self.server = http.HTTPServer(self.address, self.handle)
self.server.start() self.server.start()
...@@ -25,23 +26,23 @@ class BoundTestCase(greentest.TestCase): ...@@ -25,23 +26,23 @@ class BoundTestCase(greentest.TestCase):
self.server.stop() self.server.stop()
finally: finally:
timeout.cancel() timeout.cancel()
#self.print_netstat('after stop')
self.check_refused() self.check_refused()
greentest.TestCase.tearDown(self)
def print_netstat(self, comment=''): def print_netstat(self, comment=''):
cmd = 'sudo netstat -anp | grep %s' % self.server.server_port cmd = 'echo "%s" && netstat -anp 2>&1 | grep %s' % (comment, self.server.server_port)
print cmd, ' # %s' % comment
os.system(cmd) os.system(cmd)
@property
def url(self):
return 'http://%s:%s' % (self.server.server_host, self.server.server_port)
def connect(self): def connect(self):
s = socket.socket() s = socket.socket()
s.connect((self.server.server_host, self.server.server_port)) s.connect((self.server.server_host, self.server.server_port))
return s return s
def urlopen(self, *args, **kwargs):
fd = self.connect().makefile(bufsize=1)
fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
return read_http(fd, *args, **kwargs)
def check_refused(self): def check_refused(self):
try: try:
self.connect() self.connect()
...@@ -52,6 +53,15 @@ class BoundTestCase(greentest.TestCase): ...@@ -52,6 +53,15 @@ class BoundTestCase(greentest.TestCase):
print 'WARNING: instead of ECONNREFUSED got IOError: %s' % e print 'WARNING: instead of ECONNREFUSED got IOError: %s' % e
class TestNoop(BoundTestCase):
def handle(self, r):
pass
def test(self):
self.urlopen(code=500)
class TestClientCloses(BoundTestCase): class TestClientCloses(BoundTestCase):
# this test is useless. currently there's no way to know that the client closed the connection, # this test is useless. currently there's no way to know that the client closed the connection,
...@@ -87,14 +97,16 @@ class TestStop(BoundTestCase): ...@@ -87,14 +97,16 @@ class TestStop(BoundTestCase):
return gevent.spawn_later(0.01, self.reply, r) return gevent.spawn_later(0.01, self.reply, r)
def test(self): def test(self):
server = http.HTTPServer(self.address, self.handle)
server.start()
s = self.connect() s = self.connect()
s.sendall('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') s.sendall('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
s.sendall('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') s.sendall('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
s.close() s.close()
self.server.stop() server.stop()
gevent.sleep(0.02) gevent.sleep(0.02)
# stopping what already stopped is OK # stopping what already stopped is OK
self.server.stop() server.stop()
class TestSendReply(BoundTestCase): class TestSendReply(BoundTestCase):
...@@ -103,11 +115,7 @@ class TestSendReply(BoundTestCase): ...@@ -103,11 +115,7 @@ class TestSendReply(BoundTestCase):
r.send_reply(200, 'OK', 'hello world') r.send_reply(200, 'OK', 'hello world')
def test(self): def test(self):
response = urlopen(self.url) self.urlopen(body='hello world')
assert response.code == 200, response
assert response.msg == 'OK', response
data = response.read()
assert data == 'hello world', data
def test_keepalive(self): def test_keepalive(self):
s = self.connect() s = self.connect()
...@@ -121,11 +129,7 @@ class TestException(BoundTestCase): ...@@ -121,11 +129,7 @@ class TestException(BoundTestCase):
raise greentest.ExpectedException('TestException.handle') raise greentest.ExpectedException('TestException.handle')
def test(self): def test(self):
try: self.urlopen(code=500)
urlopen(self.url)
except HTTPError, e:
assert e.code == 500, e
assert e.msg == 'Internal Server Error', e
class TestSendReplyLater(BoundTestCase): class TestSendReplyLater(BoundTestCase):
...@@ -135,13 +139,7 @@ class TestSendReplyLater(BoundTestCase): ...@@ -135,13 +139,7 @@ class TestSendReplyLater(BoundTestCase):
r.send_reply(200, 'OK', 'hello world') r.send_reply(200, 'OK', 'hello world')
def test(self): def test(self):
response = urlopen(self.url) self.urlopen(body='hello world')
#print 'connected to %s' % self.url
assert response.code == 200, response
assert response.msg == 'OK', response
data = response.read()
#print 'read data from %s' % self.url
assert data == 'hello world', data
def test_client_closes_10(self): def test_client_closes_10(self):
s = self.connect() s = self.connect()
...@@ -171,13 +169,13 @@ class TestDetach(BoundTestCase): ...@@ -171,13 +169,13 @@ class TestDetach(BoundTestCase):
assert input.read() == '' assert input.read() == ''
assert output.read() == '' assert output.read() == ''
self.handled = True self.handled = True
self.current.throw(Exception('test done')) gevent.kill(self.current, Exception('test done'))
def test(self): def test(self):
self.current = gevent.getcurrent() self.current = gevent.getcurrent()
try: try:
try: try:
urlopen(self.url) self.urlopen()
except Exception, ex: except Exception, ex:
assert str(ex) == 'test done', ex assert str(ex) == 'test done', ex
finally: finally:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment