Example #1
0
    def test__init__readfile(self):
        test_pool = set(TestObject(s) for s in range(32768, 40000))
        with mock.patch.object(ia.ItemAllocator, '_read') as read:
            read.return_value = ["da873ca2,10\n"]
            a = ia.ItemAllocator('/file', TestObject, test_pool)

        self.assertIn('da873ca2', a.remembered)
        self.assertEqual({}, a.allocations)
    def test_allocate(self):
        test_pool = set([TestObject(33000), TestObject(33001)])
        a = ia.ItemAllocator('/file', TestObject, test_pool)
        with mock.patch.object(ia.ItemAllocator, '_write') as write:
            test_object = a.allocate('test')

        self.assertIn('test', a.allocations)
        self.assertIn(test_object, a.allocations.values())
        self.assertNotIn(test_object, a.pool)
        self.assertTrue(write.called)
Example #3
0
    def test__init__(self):
        test_pool = set(TestObject(s) for s in range(32768, 40000))
        with mock.patch.object(ia.ItemAllocator, '_write') as write:
            a = ia.ItemAllocator('/file', TestObject, test_pool)
            test_object = a.allocate('test')

        self.assertIn('test', a.allocations)
        self.assertIn(test_object, a.allocations.values())
        self.assertNotIn(test_object, a.pool)
        self.assertTrue(write.called)
Example #4
0
    def test_release(self):
        test_pool = set([TestObject(33000), TestObject(33001)])
        with mock.patch.object(ia.ItemAllocator, '_write') as write:
            a = ia.ItemAllocator('/file', TestObject, test_pool)
            allocation = a.allocate('deadbeef')
            write.reset_mock()
            a.release('deadbeef')

        self.assertNotIn('deadbeef', a.allocations)
        self.assertIn(allocation, a.pool)
        self.assertEqual({}, a.allocations)
        write.assert_called_once_with([])
Example #5
0
    def test_allocate_exhausted_pool(self):
        test_pool = set([TestObject(33000)])
        with mock.patch.object(ia.ItemAllocator, '_read') as read:
            read.return_value = ["deadbeef,33000\n"]
            a = ia.ItemAllocator('/file', TestObject, test_pool)

        with mock.patch.object(ia.ItemAllocator, '_write') as write:
            allocation = a.allocate('abcdef12')

        self.assertNotIn('deadbeef', a.allocations)
        self.assertNotIn(allocation, a.pool)
        self.assertTrue(write.called)
Example #6
0
    def test_allocate_and_lookup(self):
        test_pool = set([TestObject(33000), TestObject(33001)])
        a = ia.ItemAllocator('/file', TestObject, test_pool)
        with mock.patch.object(ia.ItemAllocator, '_write') as write:
            test_object = a.allocate('test')

        # a lookup should find the same object
        lookup_object = a.lookup('test')

        self.assertIn('test', a.allocations)
        self.assertIn(test_object, a.allocations.values())
        self.assertNotIn(test_object, a.pool)
        self.assertTrue(write.called)
        self.assertEqual(test_object, lookup_object)
Example #7
0
    def test_allocate_from_file(self):
        test_pool = set([TestObject(33000), TestObject(33001)])
        with mock.patch.object(ia.ItemAllocator, '_read') as read:
            read.return_value = ["deadbeef,33000\n"]
            a = ia.ItemAllocator('/file', TestObject, test_pool)

        with mock.patch.object(ia.ItemAllocator, '_write') as write:
            t_obj = a.allocate('deadbeef')

        self.assertEqual('33000', t_obj._value)
        self.assertIn('deadbeef', a.allocations)
        self.assertIn(t_obj, a.allocations.values())
        self.assertNotIn(33000, a.pool)
        self.assertFalse(write.called)
Example #8
0
    def test__init__readfile_error(self):
        test_pool = set(TestObject(s) for s in range(32768, 40000))
        with mock.patch.object(ia.ItemAllocator, '_read') as read,\
                mock.patch.object(ia.ItemAllocator, '_write') as write:
            read.return_value = ["da873ca2,10\n",
                                 "corrupt_entry_no_delimiter\n",
                                 "42c9daf7,11\n"]
            a = ia.ItemAllocator('/file', TestObject, test_pool)

        self.assertIn('da873ca2', a.remembered)
        self.assertIn('42c9daf7', a.remembered)
        self.assertNotIn('corrupt_entry_no_delimiter', a.remembered)
        self.assertEqual({}, a.allocations)
        self.assertTrue(write.called)
Example #9
0
    def test_allocate_repeated_call_with_same_key(self):
        test_pool = set([TestObject(33000), TestObject(33001),
                         TestObject(33002), TestObject(33003),
                         TestObject(33004), TestObject(33005)])
        a = ia.ItemAllocator('/file', TestObject, test_pool)
        with mock.patch.object(ia.ItemAllocator, '_write'):
            test_object = a.allocate('test')
            test_object1 = a.allocate('test')
            test_object2 = a.allocate('test')
            test_object3 = a.allocate('test1')

        # same value for same key on repeated calls
        self.assertEqual(test_object, test_object1)
        self.assertEqual(test_object1, test_object2)
        # values for different keys should be diffent
        self.assertNotEqual(test_object, test_object3)