Merge lp:~osomon/notes-app/robust-test-server into lp:notes-app

Proposed by Olivier Tilloy
Status: Merged
Approved by: Olivier Tilloy
Approved revision: 230
Merged at revision: 231
Proposed branch: lp:~osomon/notes-app/robust-test-server
Merge into: lp:notes-app
Diff against target: 125 lines (+37/-19)
3 files modified
tests/autopilot/notes_app/tests/__init__.py (+31/-14)
tests/autopilot/notes_app/tests/test_images.py (+3/-3)
tests/autopilot/notes_app/tests/test_parts.py (+3/-2)
To merge this branch: bzr merge lp:~osomon/notes-app/robust-test-server
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Needs Fixing
Ubuntu Phablet Team Pending
Review via email: mp+196174@code.launchpad.net

Commit message

Use a different port for the test server when a zombie process doesn’t release the default one,
and use a cleanup function instead of tearDown() for improved robustness.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ugo Riboni (uriboni) wrote :

The endless loop until a port is found seems a bit dangerous.
It's unlikely to go for a lot of iterations but I feel more confortable if it can be capped to a number of tries before giving up with an exception.

Revision history for this message
Olivier Tilloy (osomon) wrote :

> The endless loop until a port is found seems a bit dangerous.
> It's unlikely to go for a lot of iterations but I feel more confortable if it
> can be capped to a number of tries before giving up with an exception.

Thanks for the suggestion, that’s safer indeed. I added a limit (5 retries) at revision 230.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/autopilot/notes_app/tests/__init__.py'
2--- tests/autopilot/notes_app/tests/__init__.py 2013-11-04 11:12:49 +0000
3+++ tests/autopilot/notes_app/tests/__init__.py 2013-11-26 09:48:03 +0000
4@@ -7,12 +7,15 @@
5
6 """notes-app autopilot tests."""
7
8+import base64
9 import BaseHTTPServer
10+import errno
11+import glob
12+import md5
13+import os
14 import os.path
15-import glob
16+import socket
17 import threading
18-import base64
19-import md5
20
21 from autopilot.input import Mouse, Touch, Pointer
22 from autopilot.matchers import Eventually
23@@ -25,8 +28,6 @@
24 from notes_app.emulators.notesapp import NotesApp
25 import imagedata
26
27-HTTP_SERVER_PORT = 8129
28-
29
30 class NotesAppTestCase(AutopilotTestCase):
31
32@@ -158,12 +159,31 @@
33 A simple custom HTTP server run in a separate thread.
34 """
35
36- def __init__(self, port):
37+ def __init__(self):
38 super(HTTPServerInAThread, self).__init__()
39- self.server = BaseHTTPServer.HTTPServer(("", port), HTTPRequestHandler)
40+ port = 12345
41+ self.server = None
42+ while (self.server is None) and (port < 12350):
43+ try:
44+ self.server = BaseHTTPServer.HTTPServer(("", port),
45+ HTTPRequestHandler)
46+ except socket.error as error:
47+ if (error.errno == errno.EADDRINUSE):
48+ print "Port %d is already in use" % port
49+ port += 1
50+ else:
51+ print os.strerror(error.errno)
52+ raise
53+ if self.server is None:
54+ raise SystemError("Unable to start local HTTP server")
55 self.server.allow_reuse_address = True
56
57+ @property
58+ def port(self):
59+ return self.server.server_port
60+
61 def run(self):
62+ print "now serving on port %d" % self.port
63 self.server.serve_forever()
64
65 def shutdown(self):
66@@ -178,11 +198,8 @@
67 a simple custom HTTP server in a separate thread.
68 """
69
70- def setUp(self):
71- self.server = HTTPServerInAThread(HTTP_SERVER_PORT)
72+ def start_server(self):
73+ self.server = HTTPServerInAThread()
74 self.server.start()
75- super(NotesTestCaseBaseWithHTTPServer, self).setUp()
76-
77- def tearDown(self):
78- super(NotesTestCaseBaseWithHTTPServer, self).tearDown()
79- self.server.shutdown()
80+ self.addCleanup(self.server.shutdown)
81+ self.base_url = "http://localhost:%d" % self.server.port
82
83=== modified file 'tests/autopilot/notes_app/tests/test_images.py'
84--- tests/autopilot/notes_app/tests/test_images.py 2013-11-04 11:12:49 +0000
85+++ tests/autopilot/notes_app/tests/test_images.py 2013-11-26 09:48:03 +0000
86@@ -19,13 +19,13 @@
87
88 class BaseForTestImages(NotesTestCaseBaseWithHTTPServer):
89 def setUp(self):
90+ self.start_server()
91 self.setup_db(self.IMAGE)
92 super(BaseForTestImages, self).setUp()
93
94 def setup_db(self, image):
95- note_data = """{ "elements" : [
96- {"content":"http://localhost:8129/""" + image + """","type":"image"}
97- ]}"""
98+ note_data = '{"elements": [{"content":"%s/%s","type":"image"}]}' % \
99+ (self.base_url, image)
100 path = self.ensure_db()
101 conn = sqlite3.connect(path)
102 cursor = conn.cursor()
103
104=== modified file 'tests/autopilot/notes_app/tests/test_parts.py'
105--- tests/autopilot/notes_app/tests/test_parts.py 2013-11-04 11:12:49 +0000
106+++ tests/autopilot/notes_app/tests/test_parts.py 2013-11-26 09:48:03 +0000
107@@ -24,6 +24,7 @@
108 """ This is needed to wait for the application to start.
109 In the testfarm, the application may take some time to show up."""
110 def setUp(self):
111+ self.start_server()
112 # Setup the database before starting the app
113 self.setup_db()
114 super(TestFocus, self).setUp()
115@@ -31,8 +32,8 @@
116 def setup_db(self):
117 note_data = """{ "elements" : [
118 {"content":"foobar","type":"text"},
119- {"content":"http://localhost:8129/image.png","type":"image"}
120- ]}"""
121+ {"content":"%s/image.png","type":"image"}
122+ ]}""" % self.base_url
123 path = self.ensure_db()
124 conn = sqlite3.connect(path)
125 cursor = conn.cursor()

Subscribers

People subscribed via source and target branches