You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ja...@apache.org on 2015/04/27 04:14:12 UTC

[4/4] trafficserver git commit: Fix chunked keep-alive tests

Fix chunked keep-alive tests


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/f54516c7
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/f54516c7
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/f54516c7

Branch: refs/heads/master
Commit: f54516c7b3d1edc16a68681eb0f31c97f8404625
Parents: 0d439bf
Author: Thomas Jackson <ja...@gmail.com>
Authored: Fri Apr 24 19:00:36 2015 -0700
Committer: Thomas Jackson <ja...@apache.org>
Committed: Sun Apr 26 19:13:41 2015 -0700

----------------------------------------------------------------------
 ci/new_tsqa/tests/test_chunked.py | 102 +++++++++++++++++++--------------
 1 file changed, 59 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/f54516c7/ci/new_tsqa/tests/test_chunked.py
----------------------------------------------------------------------
diff --git a/ci/new_tsqa/tests/test_chunked.py b/ci/new_tsqa/tests/test_chunked.py
index 645a372..9b7fdc0 100644
--- a/ci/new_tsqa/tests/test_chunked.py
+++ b/ci/new_tsqa/tests/test_chunked.py
@@ -22,7 +22,6 @@ import requests
 import time
 import logging
 import json
-import threading
 import uuid
 import socket
 
@@ -56,7 +55,10 @@ class ChunkedHandler(SocketServer.BaseRequestHandler):
                 log.info('Client disconnected')
                 break
             inc_lines = data.splitlines()
-            uri = inc_lines[0].split()[1]
+            try:
+                uri = inc_lines[0].split()[1]
+            except IndexError:
+                break
             parts = 5  # how many things to send
             sleep_time = 0.2  # how long to sleep between parts
             close = True  # whether to close properly
@@ -68,10 +70,10 @@ class ChunkedHandler(SocketServer.BaseRequestHandler):
                     sleep_time = float(uri_parts[1])
                 if len(uri_parts) >= 3:
                     close = json.loads(uri_parts[2])
-
             resp = ('HTTP/1.1 200 OK\r\n'
                     'X-Conn-Id: ' + str(conn_id) + '\r\n'
                     'Transfer-Encoding: chunked\r\n'
+                    'Connection: keep-alive\r\n'
                     '\r\n')
             self.request.sendall(resp)
             for x in xrange(0, parts):
@@ -94,10 +96,10 @@ class TestChunked(helpers.EnvironmentCase):
 
         # create a socket server
         cls.port = tsqa.utils.bind_unused_port()[1]
-        server = SocketServer.TCPServer(('127.0.0.1', cls.port), ChunkedHandler)
-        t = threading.Thread(target=server.serve_forever)
-        t.daemon = True
-        t.start()
+        cls.server = tsqa.endpoint.SocketServerDaemon(ChunkedHandler, port=cls.port)
+        cls.server.start()
+        cls.server.ready.wait()
+
         cls.configs['remap.config'].add_line('map / http://127.0.0.1:{0}/'.format(cls.port))
 
         cls.configs['records.config']['CONFIG'].update({
@@ -114,23 +116,53 @@ class TestChunked(helpers.EnvironmentCase):
         '''
         Test that the origin does in fact support keepalive
         '''
-        with requests.Session() as s:
-            url = 'http://127.0.0.1:{0}/'.format(self.port)
-            ret = s.get(url)
-            conn_id = ret.headers['x-conn-id']
-            self.assertEqual(ret.text, '01234')
-
-            url = 'http://127.0.0.1:{0}/2'.format(self.port)
-            ret = s.get(url)
-            self.assertEqual(ret.text, '01')
-            self.assertEqual(ret.headers['x-conn-id'], conn_id)
-
-            url = 'http://127.0.0.1:{0}/2/1'.format(self.port)
-            start = time.time()
-            ret = s.get(url)
-            self.assertEqual(ret.text, '01')
-            self.assertEqual(ret.headers['x-conn-id'], conn_id)
-            self.assertTrue(time.time() - start > 2)
+        self._client_test_chunked_keepalive(self.port)
+        self._client_test_chunked_keepalive(self.port, num_bytes=2)
+        self._client_test_chunked_keepalive(self.port, num_bytes=2, sleep=1)
+
+    def _client_test_chunked_keepalive(self,
+                                       port=None,
+                                       times=3,
+                                       num_bytes=None,
+                                       sleep=None,
+                                       ):
+        if port is None:
+            port = int(self.configs['records.config']['CONFIG']['proxy.config.http.server_ports'])
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        s.connect(('127.0.0.1', port))
+
+        url = '/'
+        if num_bytes is not None:
+            url += str(num_bytes)
+        if sleep is not None:
+            if num_bytes is None:
+                raise Exception()
+            url += '/' + str(sleep)
+
+        request = ('GET ' + url + ' HTTP/1.1\r\n'
+                   'Host: 127.0.0.1\r\n'
+                   '\r\n')
+        uuid = None
+        # test basic
+        for x in xrange(1, times):
+            s.send(request)
+            resp = ''
+            while True:
+                response = s.recv(4096)
+                for line in response.splitlines():
+                    line = line.strip()
+                    if line.startswith('X-Conn-Id:'):
+                        r_uuid = line.replace('X-Conn-Id:', '')
+                        if uuid is None:
+                            uuid = r_uuid
+                        else:
+                            self.assertEqual(uuid, r_uuid)
+                resp += response
+                if resp.endswith('\r\n0\r\n\r\n'):
+                    break
+            for x in xrange(0, num_bytes or 4):
+                self.assertIn('1\r\n{0}\r\n'.format(x), resp)
+        s.close()
 
     def test_chunked_basic(self):
         url = 'http://127.0.0.1:{0}'.format(self.port)
@@ -154,27 +186,11 @@ class TestChunked(helpers.EnvironmentCase):
         self.assertEqual(conn_id, ret.headers['x-conn-id'])
 
     def test_chunked_keepalive_client(self):
-        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.connect(('127.0.0.1', int(self.configs['records.config']['CONFIG']['proxy.config.http.server_ports'])))
-
-        request = ('GET / HTTP/1.1\r\n'
-                   'Host: 127.0.0.1\r\n'
-                   '\r\n')
-        for x in xrange(1, 10):
-            s.send(request)
-            resp = ''
-            while True:
-                response = s.recv(4096)
-                if '0\r\n\r\n' in response:
-                    break
-                else:
-                    resp += response
-            for x in xrange(0, 4):
-                self.assertIn('1\r\n{0}\r\n'.format(x), resp)
+        self._client_test_chunked_keepalive()
+        self._client_test_chunked_keepalive(num_bytes=2)
+        self._client_test_chunked_keepalive(num_bytes=2, sleep=1)
 
     def test_chunked_bad_close(self):
         url = 'http://127.0.0.1:{0}/5/0.1/false'.format(self.port)
         with self.assertRaises(requests.exceptions.ConnectionError):
             ret = requests.get(url, proxies=self.proxies, timeout=2)
-
-