'If an idle connection is put back in the cache and the max number of
persistent connections has been exceeded, one of the connections is
closed and removed from the cache.'

def test_putExceedsMaxPersistent(self):

    pool = self.pool
    origCached = [StubHTTPProtocol(), StubHTTPProtocol()]
    for p in origCached:
        p.makeConnection(StringTransport())
        pool._putConnection(('http', 'example.com', 80), p)
    self.assertEqual(pool._connections[('http', 'example.com', 80)], origCached)
    timeouts = pool._timeouts.copy()
    newProtocol = StubHTTPProtocol()
    newProtocol.makeConnection(StringTransport())
    pool._putConnection(('http', 'example.com', 80), newProtocol)
    newCached = pool._connections[('http', 'example.com', 80)]
    self.assertEqual(len(newCached), 2)
    self.assertEqual(newCached, [origCached[1], newProtocol])
    self.assertEqual([p.transport.disconnecting for p in newCached], [False, False])
    self.assertEqual(origCached[0].transport.disconnecting, True)
    self.assertTrue(timeouts[origCached[0]].cancelled)
    self.assertNotIn(origCached[0], pool._timeouts)
