Pergunta

Estou depurando os erros 'O servidor MySQL desapareceu'.Existe uma solução proposta, que funciona mais ou menos:

from twisted.enterprise import adbapi
from twisted.python import log
import MySQLdb

class ReconnectingConnectionPool(adbapi.ConnectionPool):
    """Reconnecting adbapi connection pool for MySQL.

    This class improves on the solution posted at
    http://www.gelens.org/2008/09/12/reinitializing-twisted-connectionpool/
    by checking exceptions by error code and only disconnecting the current
    connection instead of all of them.

    Also see:
    http://twistedmatrix.com/pipermail/twisted-python/2009-July/020007.html

    """
    def _runInteraction(self, interaction, *args, **kw):
        try:
            return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
        except MySQLdb.OperationalError, e:
            if e[0] not in (2006, 2013):
                raise
            log.msg("RCP: got error %s, retrying operation" %(e))
            conn = self.connections.get(self.threadID())
            self.disconnect(conn)
            # try the interaction again
            return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)

Retirado daqui:http://www.gelens.org/2009/09/13/twisted-connectionpool-revisited/

No entanto, a exceção é detectada não apenas em nosso ReconnectingConnectionPool, mas também em _runInteraction.E aciona log.err, que (aparentemente) faz duas coisas:

  • imprima o erro - embora nosso aplicativo funcione bem
  • o que é pior, faz com que o teste falhe, então os testes falham.Não tenho certeza se isso é um problema de log.err, mas acontece mesmo assim.

Eu poderia hackear o adbapi, mas provavelmente não é a melhor ideia.Existem maneiras melhores de lidar com isso?

Foi útil?

Solução

Você está perguntando como fazer um teste de unidade passar mesmo se um erro for registrado?Tentar flushLoggedErrors.Se você estiver perguntando outra coisa, esclareça.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top