← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~lifeless/python-pgbouncer/start-stop into lp:python-pgbouncer

 

Robert Collins has proposed merging lp:~lifeless/python-pgbouncer/start-stop into lp:python-pgbouncer.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~lifeless/python-pgbouncer/start-stop/+merge/69041

Allow start() and stop() on the bouncer test fixture, for tests that want to bounce the bouncer (e.g. lp fastdowntime tests).
-- 
https://code.launchpad.net/~lifeless/python-pgbouncer/start-stop/+merge/69041
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~lifeless/python-pgbouncer/start-stop into lp:python-pgbouncer.
=== modified file 'pgbouncer/fixture.py'
--- pgbouncer/fixture.py	2011-07-18 03:31:27 +0000
+++ pgbouncer/fixture.py	2011-07-25 08:41:39 +0000
@@ -73,8 +73,9 @@
         self.port = _allocate_ports()[0]
         self.configdir = self.useFixture(TempDir())
         self.auth_type = 'trust'
+        self.process_pid = None
         self.setUpConf()
-        self.start_bouncer()
+        self.start()
 
     def setUpConf(self):
         """Create a pgbouncer.ini file."""
@@ -103,7 +104,7 @@
             for user_creds in self.users.items():
                 authfile.write('"%s" "%s"\n' % user_creds)
 
-    def cleanup_process(self):
+    def stop(self):
         if self.process_pid is None:
             return
         os.kill(self.process_pid, signal.SIGTERM)
@@ -112,13 +113,13 @@
         stop = start + 5.0
         while time.time() < stop:
             if not os.path.isfile(self.pidpath):
+                self.process_pid = None
                 return
         # If its not going away, we might want to raise an error, but for now
         # it seems reliable.
 
-    def start_bouncer(self):
-        self.process_pid = None
-        self.addCleanup(self.cleanup_process)
+    def start(self):
+        self.addCleanup(self.stop)
         outputfile = open(self.outputpath, 'wt')
         self.process = subprocess.Popen(['pgbouncer', '-d', self.inipath],
             stdout=outputfile.fileno(), stderr=outputfile.fileno())

=== modified file 'pgbouncer/tests.py'
--- pgbouncer/tests.py	2011-07-18 03:31:27 +0000
+++ pgbouncer/tests.py	2011-07-25 08:41:39 +0000
@@ -53,8 +53,28 @@
         db = self.db
         bouncer.databases[db.database] = 'host=%s' % (db.host,)
         bouncer.users['user1'] = ''
-        # Create a 
-        with bouncer:
-            conn = psycopg2.connect(host=bouncer.host, port=bouncer.port,
-                user='user1', database=db.database)
-            conn.close()
+        with bouncer:
+            conn = psycopg2.connect(host=bouncer.host, port=bouncer.port,
+                user='user1', database=db.database)
+            conn.close()
+
+    def test_stop_start_facility(self):
+        # Once setup the fixture can be stopped, and started again, retaining
+        # its configuration. [Note that dynamically allocated ports could
+        # potentially be used by a different process, so this isn't perfect,
+        # but its pretty reliable as a test helper, and manual port allocation
+        # outside the dynamic range should be fine.
+        bouncer = PGBouncerFixture()
+        db = self.db
+        bouncer.databases[db.database] = 'host=%s' % (db.host,)
+        bouncer.users['user1'] = ''
+        def check_connect():
+            conn = psycopg2.connect(host=bouncer.host, port=bouncer.port,
+                user='user1', database=db.database)
+            conn.close()
+        with bouncer:
+            current_port = bouncer.port
+            bouncer.stop()
+            self.assertRaises(psycopg2.OperationalError, check_connect)
+            bouncer.start()
+            check_connect()

=== modified file 'setup.py'
--- setup.py	2011-07-18 03:31:27 +0000
+++ setup.py	2011-07-25 08:41:39 +0000
@@ -22,7 +22,7 @@
 description = file(os.path.join(os.path.dirname(__file__), 'README'), 'rb').read()
 
 setup(name="pgbouncer",
-      version="0.0.1",
+      version="0.0.2",
       description="Fixture to bring up temporary pgbouncer instance.",
       long_description=description,
       maintainer="Launchpad Developers",