Skip to content

Commit 8149b31

Browse files
committed
change test_unstable_connection
1 parent 1d452a5 commit 8149b31

File tree

3 files changed

+99
-23
lines changed

3 files changed

+99
-23
lines changed

requirements.dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
nose
22
coverage
3-
maproxy
3+
sevent

run-tests

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
export LOG_LEVEL=debug
44
STATUS=0
55

6+
for file in ./tests/test_*.py
7+
do
8+
echo $file
9+
nosetests $file \
10+
--logging-format='%(asctime)s [%(name)s] %(levelname)-6s %(message)s' \
11+
--with-coverage \
12+
--cover-package=tormysql
13+
if [ $? != 0 ]; then
14+
STATUS=1
15+
fi
16+
done
17+
618
pip install tornado==5.1.1
719

820
for file in ./tests/test_*.py

tests/test_unstable_connection.py

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,105 @@
11
# encoding: utf-8
2+
3+
import os
4+
import time
5+
import threading
26
import socket
37
import os
48
from tornado import gen
5-
from tornado.ioloop import IOLoop
69
from tormysql.pool import ConnectionNotFoundError
710
from pymysql import OperationalError
811
from tornado.testing import gen_test
912
from tormysql import Connection, ConnectionPool
10-
from maproxy.proxyserver import ProxyServer
11-
from maproxy.session import SessionFactory
13+
import sevent
1214
from tests import BaseTestCase
1315

16+
class Request(object):
17+
def __init__(self, conn, host, port):
18+
self.conn = conn
19+
self.pconn = sevent.tcp.Socket()
20+
self.buffer = None
21+
self.connected = False
22+
23+
self.conn.on("data", self.on_data)
24+
self.conn.on("close", self.on_close)
25+
26+
self.pconn.on("connect", self.on_pconnect)
27+
self.pconn.on("data", self.on_pdata)
28+
self.pconn.on("close", self.on_pclose)
29+
30+
self.pconn.connect((host, int(port)))
31+
32+
def on_data(self, conn, data):
33+
if self.connected:
34+
self.pconn.write(data)
35+
else:
36+
self.buffer = data
37+
38+
def on_pdata(self, conn, data):
39+
self.conn.write(data)
40+
41+
def on_close(self, conn):
42+
self.pconn.end()
43+
try:
44+
TestThroughProxy.proxys.remove(self)
45+
except:
46+
pass
47+
48+
def on_pclose(self, conn):
49+
self.conn.end()
50+
try:
51+
TestThroughProxy.proxys.remove(self)
52+
except:
53+
pass
54+
55+
def on_pconnect(self, conn):
56+
self.connected = True
57+
if self.buffer:
58+
self.pconn.write(self.buffer)
59+
self.buffer = None
1460

1561
class TestThroughProxy(BaseTestCase):
62+
proxys = []
63+
1664
def setUp(self):
1765
super(BaseTestCase, self).setUp()
1866
self.PARAMS = dict(self.PARAMS)
1967

2068
s = socket.socket()
2169
s.bind(('127.0.0.1', 0))
22-
23-
self.host, self.port = self.PARAMS['host'], self.PARAMS['port']
2470
_, self.pport = s.getsockname()
2571
s.close()
2672

73+
self.host, self.port = self.PARAMS['host'], self.PARAMS['port']
74+
2775
def init_proxy(self):
28-
self.proxy = ProxyServer(
29-
self.host,
30-
self.port,
31-
session_factory=SessionFactory(),
32-
)
76+
def on_connect(server, conn):
77+
TestThroughProxy.proxys.append(Request(conn, self.host, self.port))
78+
79+
self.proxy_server = sevent.tcp.Server()
80+
self.proxy_server.on("connection", on_connect)
81+
self.proxy_server.listen(("0.0.0.0", self.pport))
82+
3383
self.PARAMS['port'] = self.pport
3484
self.PARAMS['host'] = '127.0.0.1'
3585

36-
self.proxy.listen(self.pport)
37-
3886
def _close_proxy_sessions(self):
39-
for sock in self.proxy.SessionsList:
40-
try:
41-
sock.c2p_stream.close()
42-
except:
43-
pass
87+
for request in TestThroughProxy.proxys:
88+
request.conn.end()
4489

4590
def tearDown(self):
4691
try:
47-
self.proxy.stop()
92+
for request in TestThroughProxy.proxys:
93+
request.conn.end()
94+
self.proxy_server.close()
4895
except:
4996
pass
5097
super(BaseTestCase, self).tearDown()
5198

5299
@gen.coroutine
53100
def _execute_test_connection_closing(self):
54101
self.init_proxy()
102+
55103
connection = yield Connection(**self.PARAMS)
56104
cursor = connection.cursor()
57105
self._close_proxy_sessions()
@@ -62,14 +110,17 @@ def _execute_test_connection_closing(self):
62110
pass
63111
else:
64112
raise AssertionError("Unexpected normal situation")
65-
self.proxy.stop()
113+
114+
self.proxy_server.close()
66115

67116
@gen.coroutine
68117
def _execute_test_connection_closed(self):
69118
self.init_proxy()
119+
70120
conn = yield Connection(**self.PARAMS)
71121
yield conn.close()
72-
self.proxy.stop()
122+
123+
self.proxy_server.close()
73124

74125
try:
75126
yield Connection(**self.PARAMS)
@@ -81,6 +132,7 @@ def _execute_test_connection_closed(self):
81132
@gen.coroutine
82133
def _execute_test_remote_closing(self):
83134
self.init_proxy()
135+
84136
pool = ConnectionPool(
85137
max_connections=int(os.getenv("MYSQL_POOL", "5")),
86138
idle_seconds=7200,
@@ -90,7 +142,9 @@ def _execute_test_remote_closing(self):
90142
try:
91143
conn = yield pool.Connection()
92144
yield conn.do_close()
93-
self.proxy.stop()
145+
146+
self.proxy_server.close()
147+
94148
yield pool.Connection()
95149
except OperationalError:
96150
pass
@@ -102,6 +156,7 @@ def _execute_test_remote_closing(self):
102156
@gen.coroutine
103157
def _execute_test_pool_closing(self):
104158
self.init_proxy()
159+
105160
pool = ConnectionPool(
106161
max_connections=int(os.getenv("MYSQL_POOL", "5")),
107162
idle_seconds=7200,
@@ -118,10 +173,19 @@ def _execute_test_pool_closing(self):
118173
raise AssertionError("Unexpected normal situation")
119174
finally:
120175
yield pool.close()
121-
self.proxy.stop()
176+
177+
self.proxy_server.close()
122178

123179
@gen_test
124180
def test(self):
181+
loop = sevent.instance()
182+
def run():
183+
loop.start()
184+
185+
self.proxy_thread = threading.Thread(target=run)
186+
self.proxy_thread.setDaemon(True)
187+
self.proxy_thread.start()
188+
125189
yield self._execute_test_connection_closing()
126190
yield self._execute_test_connection_closed()
127191
yield self._execute_test_remote_closing()

0 commit comments

Comments
 (0)