3

I'm running a suite of fairly straightforward test cases using Flask, SQLAlchemy, and PostgreSQL. Using an application factory, I've defined a base unit test class like so:

class BaseTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.app.config.from_object('app.config.test')
        self.api_base = '/api/v1'

        self.ctx = self.app.test_request_context()
        self.ctx.push()

        self.client = self.app.test_client()
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all(app=self.app)
        print db.engine.pool.status()
        if self.ctx is not None:
            self.ctx.pop()

All goes well for a few unit tests, up until:

OperationalError: (OperationalError) FATAL:  remaining connection slots are reserved for non-replication superuser connections

It seems that there is only 1 connection in the pool (db.engine.pool.status() for each test shows: Pool size: 5 Connections in pool: 1 Current Overflow: -4 Current Checked out connections: 0), but somehow the app never disconnects. Clearly a new app instance is created for each test case, which seems fine according to the documentation. If I move the app creation to the module level it works fine.

Does anyone know why this is happening?

Thanks

2
  • 2
    sounds like create_app() is hitting create_engine() and doing something with it. Commented Apr 20, 2013 at 7:06
  • I am seeing the same error, but only intermittently / nondeterministically. Although PostgreSQL is local, I have tests that depend on resources on the network, and this error only seems to occur when our network is flaky. So it's possible that some other network request is causing the test to wait, and in the meantime something about the SQLAlchemy connection times out. Commented Jun 26, 2013 at 13:55

1 Answer 1

3

Add db.get_engine(self.app).dispose() after db.drop_all()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.