def test_next_object(self): """Test that L{oidres.OIDRes.next} works in the common case.""" with Counter(self, 4) as counter: class xMockRAMCloud(self.MockRAMCloud): def real_read(self): i = counter.bump([0, 2]) if i == 0: return (70, 54321) elif i == 2: return (900, 123450) def real_update(self, next_avail, want_version): i = counter.bump([1, 3]) if i == 1: self.tc.assertEqual(next_avail, 70 + 737) self.tc.assertEqual(want_version, 54321) elif i == 3: self.tc.assertEqual(next_avail, 900 + 737) self.tc.assertEqual(want_version, 123450) rc = xMockRAMCloud(self) res = oidres.OIDRes(rc, self.table, self.oid, delta=737) for i in range(737): retry_strategy = MockRetry(self) self.assertEqual(res.next(retry_strategy), i + 70) retry_strategy.done() self.assertEqual(counter.count, 1) self.assertEqual(res.next(), 900)
def test_delta(self): """Test that L{oidres.OIDRes.delta} is set correctly.""" res = oidres.OIDRes(rc=Opaque(), table=self.table, oid=self.oid, delta=737) self.assertEqual(res.delta, 737)
def test_next_no_object(self): """Test that L{oidres.OIDRes.next} works when there is no object.""" with Counter(self, 2) as counter: class xMockRAMCloud(self.MockRAMCloud): def real_read(self): counter.bump(0) raise ramcloud.NoObjectError() def real_create(self, next_avail): counter.bump(1) self.tc.assertEqual(next_avail, 737) rc = xMockRAMCloud(self) res = oidres.OIDRes(rc, self.table, self.oid, delta=737) for i in range(737): retry_strategy = MockRetry(self) self.assertEqual(res.next(retry_strategy), i) retry_strategy.done()
def test_mismatched_version(self): """Test that L{oidres.OIDRes.next} works under contention.""" with Counter(self, 2) as counter: class xMockRAMCloud(self.MockRAMCloud): def real_read(self): counter.bump(0) return (70, 54321) def real_update(self, next_avail, want_version): counter.bump(1) self.tc.assertEqual(next_avail, 70 + 737) self.tc.assertEqual(want_version, 54321) raise ramcloud.VersionError(54321, 54322) rc = xMockRAMCloud(self) res = oidres.OIDRes(rc, self.table, self.oid, delta=737) retry_strategy = MockRetry(self, expect_later=True) self.assertRaises(BreakException, lambda: res.next(retry_strategy)) retry_strategy.done()
def test_reserve_lazily(self): """Test that L{oidres.OIDRes.reserve_lazily} works.""" with Counter(self, 4) as counter: class xMockRAMCloud(self.MockRAMCloud): def real_read(self): i = counter.bump([0, 2]) if i == 0: return (70, 54321) elif i == 2: return (900, 123450) def real_update(self, next_avail, want_version): i = counter.bump([1, 3]) if i == 1: self.tc.assertEqual(next_avail, 70 + 737) self.tc.assertEqual(want_version, 54321) elif i == 3: self.tc.assertEqual(next_avail, 900 + 737) self.tc.assertEqual(want_version, 123450) rc = xMockRAMCloud(self) res = oidres.OIDRes(rc, self.table, self.oid, delta=737) l = [] for i in range(740): l.append(res.reserve_lazily()) self.assertEqual(counter.count, -1) self.assertEqual(int(l[30]), 70) self.assertEqual(counter.count, 1) self.assertEqual(int(l[739]), 71) self.assertEqual(int(l[0]), 72) self.assertEqual(counter.count, 1) for i in range(740): int(l[i]) self.assertEqual(int(l[737]), 901) self.assertEqual(int(l[738]), 902)