Ejemplo n.º 1
0
 def test_rfb_basic_export(self):
     ''' basic rotating Bloom Filter export test '''
     blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
     blm.export('test.rbf')
     self.assertEqual(calc_file_md5('test.rbf'),
                      'eb5769ae9babdf7b37d6ce64d58812bc')
     os.remove('test.rbf')
Ejemplo n.º 2
0
    def test_rbf_rotate(self):
        ''' test that the bloom filter rotates the first bloom off the stack '''
        blm = RotatingBloomFilter(est_elements=10,
                                  false_positive_rate=0.05,
                                  max_queue_size=5)
        self.assertEqual(blm.expansions, 0)
        blm.add('test')
        self.assertEqual(blm.expansions, 0)
        for i in range(10):
            blm.add('{}'.format(i), force=True)
        self.assertEqual(blm.expansions, 1)
        self.assertEqual(blm.current_queue_size, 2)
        self.assertEqual(blm.check('test'), True)

        for i in range(10, 20):
            blm.add('{}'.format(i), force=True)
        self.assertEqual(blm.check('test'), True)
        self.assertEqual(blm.current_queue_size, 3)

        for i in range(20, 30):
            blm.add('{}'.format(i), force=True)
        self.assertEqual(blm.check('test'), True)
        self.assertEqual(blm.current_queue_size, 4)

        for i in range(30, 40):
            blm.add('{}'.format(i), force=True)
        self.assertEqual(blm.check('test'), True)
        self.assertEqual(blm.current_queue_size, 5)

        for i in range(40, 50):
            blm.add('{}'.format(i), force=True)
        self.assertEqual(blm.check('test'), False)  # it should roll off
        self.assertEqual(blm.current_queue_size, 5)
Ejemplo n.º 3
0
    def test_rbf_push_pop(self):
        """test forcing push and pop"""
        blm = RotatingBloomFilter(est_elements=10, false_positive_rate=0.05, max_queue_size=5)
        self.assertEqual(blm.current_queue_size, 1)
        blm.add("test")
        blm.push()
        self.assertEqual(blm.current_queue_size, 2)
        self.assertEqual("test" in blm, True)
        blm.push()
        self.assertEqual(blm.current_queue_size, 3)
        self.assertEqual("test" in blm, True)
        blm.push()
        self.assertEqual(blm.current_queue_size, 4)
        self.assertEqual("test" in blm, True)
        blm.push()
        self.assertEqual(blm.current_queue_size, 5)
        self.assertEqual("test" in blm, True)
        blm.push()
        self.assertEqual(blm.current_queue_size, 5)
        self.assertEqual("test" in blm, False)

        # test popping
        blm.add("that")
        blm.pop()
        self.assertEqual(blm.current_queue_size, 4)
        self.assertEqual("that" in blm, True)
        blm.pop()
        self.assertEqual(blm.current_queue_size, 3)
        self.assertEqual("that" in blm, True)
        blm.pop()
        self.assertEqual(blm.current_queue_size, 2)
        self.assertEqual("that" in blm, True)
        blm.pop()
        self.assertEqual(blm.current_queue_size, 1)
        self.assertEqual("that" in blm, True)
Ejemplo n.º 4
0
 def test_rbf_init(self):
     ''' test the initialization of an rotating bloom filter '''
     blm = RotatingBloomFilter(est_elements=10,
                               false_positive_rate=0.05,
                               max_queue_size=10)
     self.assertEqual(blm.expansions, 0)
     self.assertEqual(blm.max_queue_size, 10)
Ejemplo n.º 5
0
 def test_rfb_basic_export(self):
     """ basic rotating Bloom Filter export test """
     blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
     blm.export("test.rbf")
     self.assertEqual(calc_file_md5("test.rbf"),
                      "eb5769ae9babdf7b37d6ce64d58812bc")
     os.remove("test.rbf")
Ejemplo n.º 6
0
    def test_rbf_non_basic_import(self):
        ''' test that the imported rotating Bloom filter is correct '''
        blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
        for i in range(15):
            blm.add('{}'.format(i))
            blm.push()
        blm.export('test.rbf')

        blm2 = RotatingBloomFilter(filepath='test.rbf')
        # test those that should be popped off...
        for i in range(5):
            self.assertEqual('{}'.format(i) in blm2, False)
        # test things that would not be popped
        for i in range(6, 15):
            self.assertEqual('{}'.format(i) in blm2, True)
        self.assertEqual(blm2.current_queue_size, 10)
        self.assertEqual(blm2.expansions, 9)
        os.remove('test.rbf')
Ejemplo n.º 7
0
    def test_rbf_non_basic_import(self):
        """test that the imported rotating Bloom filter is correct"""
        with NamedTemporaryFile(dir=os.getcwd(), suffix=".rbf", delete=DELETE_TEMP_FILES) as fobj:
            blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
            for i in range(15):
                blm.add("{}".format(i))
                blm.push()
            blm.export(fobj.name)

            blm2 = RotatingBloomFilter(filepath=fobj.name)
            # test those that should be popped off...
            for i in range(5):
                self.assertEqual("{}".format(i) in blm2, False)
            # test things that would not be popped
            for i in range(6, 15):
                self.assertEqual("{}".format(i) in blm2, True)
            self.assertEqual(blm2.current_queue_size, 10)
            self.assertEqual(blm2.expansions, 9)
            self.assertEqual(blm2.elements_added, 15)
Ejemplo n.º 8
0
    def test_rbf_import_empty(self):
        """test that rotating Bloom Filter is correct on import"""
        with NamedTemporaryFile(dir=os.getcwd(), suffix=".rbf", delete=DELETE_TEMP_FILES) as fobj:
            blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
            blm.export(fobj.name)
            self.assertEqual(calc_file_md5(fobj.name), "eb5769ae9babdf7b37d6ce64d58812bc")

            blm2 = ExpandingBloomFilter(filepath=fobj.name)
            for bloom in blm2._blooms:
                self.assertEqual(bloom.elements_added, 0)
Ejemplo n.º 9
0
 def test_rbf_pop_exception_msg(self):
     """rotating bloom filter error: check the resulting error message"""
     blm = RotatingBloomFilter(est_elements=10, false_positive_rate=0.05, max_queue_size=5)
     try:
         blm.pop()
     except RotatingBloomFilterError as ex:
         msg = "Popping a Bloom Filter will result in an unusable system!"
         self.assertEqual(str(ex), msg)
     except:
         self.assertEqual(True, False)
Ejemplo n.º 10
0
    def test_rbf_import_empty(self):
        ''' test that rotating Bloom Filter is correct on import '''
        blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
        blm.export('test.rbf')
        self.assertEqual(calc_file_md5('test.rbf'),
                         'eb5769ae9babdf7b37d6ce64d58812bc')

        blm2 = ExpandingBloomFilter(filepath='test.rbf')
        for bloom in blm2._blooms:
            self.assertEqual(bloom.elements_added, 0)

        os.remove('test.rbf')
Ejemplo n.º 11
0
    def test_rfb_from_bytes(self):
        """basic rotating Bloom Filter export bytes test"""
        blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05, max_queue_size=3)
        for i in range(105):
            blm.add(str(i))
        bytes_out = bytes(blm)

        blm2 = RotatingBloomFilter.frombytes(bytes_out, max_queue_size=3)
        self.assertEqual(blm2.expansions, 2)
        self.assertEqual(blm2.false_positive_rate, 0.05000000074505806)
        self.assertEqual(blm2.estimated_elements, 25)
        self.assertEqual(blm2.elements_added, 105)
        self.assertEqual(blm2.current_queue_size, 3)
        self.assertEqual(bytes(blm2), bytes(blm))
        for i in range(105):
            self.assertEqual(blm.check(str(i)), blm2.check(str(i)))
Ejemplo n.º 12
0
 def test_rbf_pop_exception(self):
     ''' ensure the correct exception is thrown '''
     blm = RotatingBloomFilter(est_elements=10,
                               false_positive_rate=0.05,
                               max_queue_size=5)
     self.assertRaises(RotatingBloomFilterError, lambda: blm.pop())
Ejemplo n.º 13
0
 def test_rfb_basic_bytes(self):
     """basic rotating Bloom Filter export bytes test"""
     blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
     self.assertEqual(hashlib.md5(bytes(blm)).hexdigest(), "eb5769ae9babdf7b37d6ce64d58812bc")
Ejemplo n.º 14
0
 def test_rfb_basic_export(self):
     """basic rotating Bloom Filter export test"""
     with NamedTemporaryFile(dir=os.getcwd(), suffix=".rbf", delete=DELETE_TEMP_FILES) as fobj:
         blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05)
         blm.export(fobj.name)
         self.assertEqual(calc_file_md5(fobj.name), "eb5769ae9babdf7b37d6ce64d58812bc")