Skip to content

Commit

Permalink
Removed the extension_bytes feature and updated many tests to use Tra…
Browse files Browse the repository at this point in the history
…nsactionMetaData
  • Loading branch information
Jim Fulton committed Nov 15, 2016
1 parent b3a389f commit 32c8436
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 92 deletions.
30 changes: 14 additions & 16 deletions src/ZODB/Connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import six

from .mvccadapter import HistoricalStorageAdapter
from ._compat import dumps, loads, _protocol

from . import valuedoc
from . import _compat

global_reset_counter = 0

Expand Down Expand Up @@ -1291,10 +1291,9 @@ class TransactionMetaData:
def __init__(self, user=u'', description=u'', extension=b''):
self.user = user
self.description = description
if isinstance(extension, bytes):
self.extension_bytes = extension
else:
self.extension = extension
if not isinstance(extension, dict):
extension = _compat.loads(extension) if extension else {}
self.extension = extension

@property
def user(self):
Expand All @@ -1316,23 +1315,22 @@ def description(self, description):
description = description.encode('utf-8')
self.__description = description

def note(self, text): # for tests
text = text.strip()
if not isinstance(text, bytes):
text = text.encode('utf-8')
if self.description:
self.description = self.description.strip() + b' ' + text
else:
self.description = text


@property
def extension(self):
return self.__extension

@extension.setter
def extension(self, v):
self.__extension = v
self.__extension_bytes = dumps(v, _protocol) if v else b''

_extension = extension

@property
def extension_bytes(self):
return self.__extension_bytes

@extension_bytes.setter
def extension_bytes(self, v):
d = loads(v) if v else {}
self.__extension_bytes = v if d else b''
self.__extension = d
42 changes: 17 additions & 25 deletions src/ZODB/tests/BasicStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
All storages should be able to pass these tests.
"""
from ZODB import POSException
from ZODB.Connection import TransactionMetaData
from ZODB.tests.MinPO import MinPO
from ZODB.tests.StorageTestBase import zodb_unpickle, zodb_pickle

import threading
import time
import transaction
import zope.interface
import zope.interface.verify

Expand All @@ -36,7 +36,7 @@ class BasicStorage:
def checkBasics(self):
self.assertEqual(self._storage.lastTransaction(), ZERO)

t = transaction.Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self.assertRaises(POSException.StorageTransactionError,
self._storage.tpc_begin, t)
Expand All @@ -48,22 +48,22 @@ def checkBasics(self):
self.assertRaises(
POSException.StorageTransactionError,
self._storage.store,
ZERO, ZERO, b'', '', transaction.Transaction())
ZERO, ZERO, b'', '', TransactionMetaData())

self.assertRaises(
POSException.StorageTransactionError,
self._storage.store,
ZERO, 1, b'2', '', transaction.Transaction())
ZERO, 1, b'2', '', TransactionMetaData())

self.assertRaises(
POSException.StorageTransactionError,
self._storage.tpc_vote, transaction.Transaction())
self._storage.tpc_vote, TransactionMetaData())
self._storage.tpc_abort(t)

def checkSerialIsNoneForInitialRevision(self):
eq = self.assertEqual
oid = self._storage.new_oid()
txn = transaction.Transaction()
txn = TransactionMetaData()
self._storage.tpc_begin(txn)
# Use None for serial. Don't use _dostore() here because that coerces
# serial=None to serial=ZERO.
Expand Down Expand Up @@ -106,7 +106,7 @@ def checkConflicts(self):

def checkWriteAfterAbort(self):
oid = self._storage.new_oid()
t = transaction.Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(oid, ZERO, zodb_pickle(MinPO(5)), '', t)
# Now abort this transaction
Expand All @@ -119,7 +119,7 @@ def checkAbortAfterVote(self):
oid1 = self._storage.new_oid()
revid1 = self._dostore(oid=oid1, data=MinPO(-2))
oid = self._storage.new_oid()
t = transaction.Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(oid, ZERO, zodb_pickle(MinPO(5)), '', t)
# Now abort this transaction
Expand Down Expand Up @@ -180,7 +180,7 @@ def checkGetSize(self):

def checkNote(self):
oid = self._storage.new_oid()
t = transaction.Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
t.note('this is a test')
self._storage.store(oid, ZERO, zodb_pickle(MinPO(5)), '', t)
Expand All @@ -194,18 +194,14 @@ def checkInterfaces(self):
def checkMultipleEmptyTransactions(self):
# There was a bug in handling empty transactions in mapping
# storage that caused the commit lock not to be released. :(
transaction.begin()
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.tpc_vote(t)
self._storage.tpc_finish(t)
t.commit()
transaction.begin()
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t) # Hung here before
self._storage.tpc_vote(t)
self._storage.tpc_finish(t)
t.commit()

def _do_store_in_separate_thread(self, oid, revid, voted):
# We'll run the competing trans in a separate thread:
Expand All @@ -224,8 +220,7 @@ def check_checkCurrentSerialInTransaction(self):

#----------------------------------------------------------------------
# stale read
transaction.begin()
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
try:
self._storage.store(b'\0\0\0\0\0\0\0\xf1',
Expand All @@ -243,8 +238,7 @@ def check_checkCurrentSerialInTransaction(self):

#----------------------------------------------------------------------
# non-stale read, no stress. :)
transaction.begin()
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(b'\0\0\0\0\0\0\0\xf2',
b'\0\0\0\0\0\0\0\0', data, '', t)
Expand All @@ -255,8 +249,7 @@ def check_checkCurrentSerialInTransaction(self):
#----------------------------------------------------------------------
# non-stale read, competition after vote. The competing
# transaction must produce a tid > this transaction's tid
transaction.begin()
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(b'\0\0\0\0\0\0\0\xf3',
b'\0\0\0\0\0\0\0\0', data, '', t)
Expand All @@ -275,8 +268,7 @@ def check_checkCurrentSerialInTransaction(self):

#----------------------------------------------------------------------
# non-stale competing trans after checkCurrentSerialInTransaction
transaction.begin()
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(b'\0\0\0\0\0\0\0\xf4',
b'\0\0\0\0\0\0\0\0', data, '', t)
Expand Down Expand Up @@ -312,7 +304,7 @@ def check_tid_ordering_w_commit(self):
# verify that a storage gets it right.

# First, some initial data.
t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(ZERO, ZERO, b'x', '', t)
self._storage.tpc_vote(t)
Expand All @@ -322,7 +314,7 @@ def check_tid_ordering_w_commit(self):
# OK, now we'll start a new transaction, take it to finish,
# and then block finish while we do some other operations.

t = transaction.get()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.store(ZERO, tids[0], b'y', '', t)
self._storage.tpc_vote(t)
Expand Down
7 changes: 4 additions & 3 deletions src/ZODB/tests/ConflictResolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"""Tests for application-level conflict resolution."""

from ZODB import DB
from ZODB.Connection import TransactionMetaData
from ZODB.POSException import ConflictError, UndoError
from persistent import Persistent
from transaction import Transaction, TransactionManager
from transaction import TransactionManager

from ZODB.tests.StorageTestBase import zodb_unpickle, zodb_pickle

Expand Down Expand Up @@ -148,7 +149,7 @@ def checkUndoConflictResolution(self):
# Start the undo
info = self._storage.undoInfo()
tid = info[1]['id']
t = Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
self._storage.undo(tid, t)
self._storage.tpc_vote(t)
Expand All @@ -170,6 +171,6 @@ def checkUndoUnresolvable(self):
# Start the undo
info = self._storage.undoInfo()
tid = info[1]['id']
t = Transaction()
t = TransactionMetaData()
self.assertRaises(UndoError, self._begin_undos_vote, t, tid)
self._storage.tpc_abort(t)
7 changes: 3 additions & 4 deletions src/ZODB/tests/IteratorStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
"""

from ZODB.Connection import TransactionMetaData
from ZODB.tests.MinPO import MinPO
from ZODB.tests.StorageTestBase import zodb_pickle, zodb_unpickle
from ZODB.utils import U64, p64, load_current

from transaction import Transaction

import ZODB.blob

try:
Expand Down Expand Up @@ -67,7 +66,7 @@ def checkUndoZombie(self):
info = self._storage.undoInfo()
tid = info[0]['id']
# Undo the creation of the object, rendering it a zombie
t = Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
oids = self._storage.undo(tid, t)
self._storage.tpc_vote(t)
Expand Down Expand Up @@ -105,7 +104,7 @@ def checkIterationIntraTransaction(self):
# Then the code in FileIterator.next() hasn't yet been fixed.
# Should automate that check.
oid = self._storage.new_oid()
t = Transaction()
t = TransactionMetaData()
data = zodb_pickle(MinPO(0))
try:
self._storage.tpc_begin(t)
Expand Down
3 changes: 2 additions & 1 deletion src/ZODB/tests/MTStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import transaction

import ZODB
from ZODB.Connection import TransactionMetaData
from ZODB.tests.StorageTestBase import zodb_pickle, zodb_unpickle
from ZODB.tests.MinPO import MinPO
from ZODB.POSException import ConflictError
Expand Down Expand Up @@ -140,7 +141,7 @@ def oid(self):

def dostore(self, i):
data = zodb_pickle(MinPO((self.getName(), i)))
t = transaction.Transaction()
t = TransactionMetaData()
oid = self.oid()
self.pause()

Expand Down
4 changes: 2 additions & 2 deletions src/ZODB/tests/ReadOnlyStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from ZODB.Connection import TransactionMetaData
from ZODB.POSException import ReadOnlyError, Unsupported
import transaction

from ZODB.utils import load_current

Expand Down Expand Up @@ -48,7 +48,7 @@ def checkReadMethods(self):
def checkWriteMethods(self):
self._make_readonly()
self.assertRaises(ReadOnlyError, self._storage.new_oid)
t = transaction.Transaction()
t = TransactionMetaData()
self.assertRaises(ReadOnlyError, self._storage.tpc_begin, t)

self.assertRaises(ReadOnlyError, self._storage.store,
Expand Down
6 changes: 3 additions & 3 deletions src/ZODB/tests/RecoveryStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""More recovery and iterator tests."""

import transaction
from transaction import Transaction
from ZODB.Connection import TransactionMetaData
from ZODB.tests.IteratorStorage import IteratorDeepCompare
from ZODB.tests.StorageTestBase import MinPO, snooze
from ZODB import DB
Expand Down Expand Up @@ -147,7 +147,7 @@ def checkRestoreWithMultipleObjectsInUndoRedo(self):
# Undo the attribute creation.
info = self._storage.undoInfo()
tid = info[0]['id']
t = Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
oids = self._storage.undo(tid, t)
self._storage.tpc_vote(t)
Expand All @@ -171,7 +171,7 @@ def checkRestoreWithMultipleObjectsInUndoRedo(self):
# Undo the undo (restore the attributes).
info = self._storage.undoInfo()
tid = info[0]['id']
t = Transaction()
t = TransactionMetaData()
self._storage.tpc_begin(t)
oids = self._storage.undo(tid, t)
self._storage.tpc_vote(t)
Expand Down
5 changes: 2 additions & 3 deletions src/ZODB/tests/RevisionStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
##############################################################################
"""Check loadSerial() on storages that support historical revisions."""

from ZODB.Connection import TransactionMetaData
from ZODB.tests.MinPO import MinPO
from ZODB.tests.StorageTestBase import zodb_unpickle, zodb_pickle, snooze
from ZODB.utils import p64, u64, load_current

import transaction

ZERO = '\0'*8

class RevisionStorage:
Expand Down Expand Up @@ -142,7 +141,7 @@ def checkLoadBeforeConsecutiveTids(self):
oid = self._storage.new_oid()
def helper(tid, revid, x):
data = zodb_pickle(MinPO(x))
t = transaction.Transaction()
t = TransactionMetaData()
try:
self._storage.tpc_begin(t, p64(tid))
self._storage.store(oid, revid, data, '', t)
Expand Down
6 changes: 3 additions & 3 deletions src/ZODB/tests/StorageTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from __future__ import print_function
import sys
import time
import transaction

from ZODB.Connection import TransactionMetaData
from ZODB.utils import u64, z64
from ZODB.tests.MinPO import MinPO
from ZODB._compat import PersistentPickler, Unpickler, BytesIO, _protocol
Expand Down Expand Up @@ -144,7 +144,7 @@ def _dostore(self, oid=None, revid=None, data=None,
if not already_pickled:
data = zodb_pickle(data)
# Begin the transaction
t = transaction.Transaction()
t = TransactionMetaData()
if user is not None:
t.user = user
if description is not None:
Expand All @@ -170,7 +170,7 @@ def _dostoreNP(self, oid=None, revid=None, data=None,
def _undo(self, tid, expected_oids=None, note=None):
# Undo a tid that affects a single object (oid).
# This is very specialized.
t = transaction.Transaction()
t = TransactionMetaData()
t.note(note or "undo")
self._storage.tpc_begin(t)
undo_result = self._storage.undo(tid, t)
Expand Down
Loading

0 comments on commit 32c8436

Please sign in to comment.