2D slices and arrays in Golang

Subtle, unexpected behaviour of Golang's slice "append"

Python leaky unittests Gotcha - Too Many MySQL Connections

OpenWorm - a long way to go

Open Source Software Presentation

I am a software engineer (lapsed physicist) who likes working with open source technology. I spend most of my time writing code in Python but I am currently also learning Go.

Some time ago I noticed that some of my integration tests were failing with a MySQL Too many connections error. The tests were being run with Python's unittest package. Each integration test was opening a MySQL connection, but I couldn't understand the cause of this error - each test was run with a new TestInstance class and I (wrongly) assumed Python's garbage collector would come along and clear the old TestInstance, closing the MySQL connection in the process.

Heading over to Stack Overflow eventually gave me the answer (thanks Kara!)

It turns out that a reference to each test case is kept until the entire test suite is run, preventing the garbage collector from freeing this object

So the following TestCase will have opened 3 simultaneous MySQL connections by the time it terminates:

import unittest
import MySQLdb

class TestMySQLConn(unittest.TestCase):
    def setUp(self):
        self.db_connection = MySQLdb.connect(some_credentials)

    def test1(self):
        # a MySQL connection opens here
        pass

    def test2(self):
        # another MySQL connection opens here
        pass

    def test3(self):
        # yet another MySQL connection opens here
        pass

The best solution is to use a tearDown method in the TestCase:

def tearDown(self):
    self.db_connection.close()

This problem was eventually discussed and resolved in Python3.4