<commit_before>import unittest
import logging
import os
from subprocess import Popen, PIPE
from checks.db.mcache import *

class TestMemCache(unittest.TestCase):
    def setUp(self):
        self.c = Memcache(logging.getLogger(__file__))

    def _countConnections(self, port):
        pid = os.getpid()
        p1 = Popen(['lsof', '-a', '-p{0}'.format(pid), '-i4'], stdout=PIPE)
        p2 = Popen(["grep", ":{0}".format(port)], stdin=p1.stdout, stdout=PIPE)
        p3 = Popen(["wc", "-l"], stdin=p2.stdout, stdout=PIPE)
        output = p3.communicate()[0]
        return int(output.strip())

    def testConnectionLeaks(self):
        for i in range(3):
            # Count open connections to localhost:11211, should be 0
            self.assertEquals(self._countConnections(11211), 0)
            r = self.c.check({"memcache_server": "localhost"})
            # Verify that the count is still 0
            self.assertEquals(self._countConnections(11211), 0)

    def testMetrics(self):
        agent_config = {"memcache_server": "localhost",
                           "memcache_instance_1": "localhost:11211:mytag",
                           "memcache_instance_2": "dummy:11211:myothertag",
                           "memcache_instance_3": "localhost:11211:mythirdtag"}<commit_msg>remote string.format() from memcache tests<commit_after>import unittest
import logging
import os
from subprocess import Popen, PIPE
from checks.db.mcache import *

class TestMemCache(unittest.TestCase):
    def setUp(self):
        self.c = Memcache(logging.getLogger(__file__))

    def _countConnections(self, port):
        pid = os.getpid()
        p1 = Popen(['lsof', '-a', '-p%s' %
            pid, '-i4'], stdout=PIPE)
        p2 = Popen(["grep", ":%s" % port], stdin=p1.stdout, stdout=PIPE)
        p3 = Popen(["wc", "-l"], stdin=p2.stdout, stdout=PIPE)
        output = p3.communicate()[0]
        return int(output.strip())

    def testConnectionLeaks(self):
        for i in range(3):
            # Count open connections to localhost:11211, should be 0
            self.assertEquals(self._countConnections(11211), 0)
            r = self.c.check({"memcache_server": "localhost"})
            # Verify that the count is still 0
            self.assertEquals(self._countConnections(11211), 0)

    def testMetrics(self):
        agent_config = {"memcache_server": "localhost",
                           "memcache_instance_1": "localhost:11211:mytag",
                           "memcache_instance_2": "dummy:11211:myothertag",
                           "memcache_instance_3": "localhost:11211:mythirdtag"}
{'fraction_non_alphanumeric': 0.1322345726331928, 'fraction_numerical': 0.036029129934840934, 'mean_word_length': 2.635097493036212, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2601920', 'n_tokens_mistral': 854, 'n_tokens_neox': 802, 'n_words': 173}