Skip to content

Commit

Permalink
fix(spanner): fix TransactionPingingPool throwing error ''NoneType' o…
Browse files Browse the repository at this point in the history
…bject is not callable' (#9609)
  • Loading branch information
larkee authored and tseaver committed Nov 11, 2019
1 parent 3285e93 commit 20ff981
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion spanner/google/cloud/spanner_v1/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def put(self, session):
raise queue.Full

txn = session._transaction
if txn is None or txn.committed() or txn._rolled_back:
if txn is None or txn.committed or txn._rolled_back:
session.transaction()
self._pending_sessions.put(session)
else:
Expand Down
32 changes: 14 additions & 18 deletions spanner/tests/unit/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def test_bind(self):
for session in SESSIONS:
session.create.assert_not_called()
txn = session._transaction
self.assertTrue(txn._begun)
txn.begin.assert_called_once_with()

self.assertTrue(pool._pending_sessions.empty())

Expand Down Expand Up @@ -685,7 +685,7 @@ def test_bind_w_timestamp_race(self):
for session in SESSIONS:
session.create.assert_not_called()
txn = session._transaction
self.assertTrue(txn._begun)
txn.begin.assert_called_once_with()

self.assertTrue(pool._pending_sessions.empty())

Expand Down Expand Up @@ -718,7 +718,7 @@ def test_put_non_full_w_active_txn(self):
self.assertIs(queued, session)

self.assertEqual(len(pending._items), 0)
self.assertFalse(txn._begun)
txn.begin.assert_not_called()

def test_put_non_full_w_committed_txn(self):
pool = self._make_one(size=1)
Expand All @@ -727,7 +727,7 @@ def test_put_non_full_w_committed_txn(self):
database = _Database("name")
session = _Session(database)
committed = session.transaction()
committed._committed = True
committed.committed = True

pool.put(session)

Expand All @@ -736,7 +736,7 @@ def test_put_non_full_w_committed_txn(self):
self.assertEqual(len(pending._items), 1)
self.assertIs(pending._items[0], session)
self.assertIsNot(session._transaction, committed)
self.assertFalse(session._transaction._begun)
session._transaction.begin.assert_not_called()

def test_put_non_full(self):
pool = self._make_one(size=1)
Expand All @@ -762,7 +762,7 @@ def test_begin_pending_transactions_non_empty(self):
pool._sessions = _Queue()

database = _Database("name")
TRANSACTIONS = [_Transaction()]
TRANSACTIONS = [_make_transaction(object())]
PENDING_SESSIONS = [_Session(database, transaction=txn) for txn in TRANSACTIONS]

pending = pool._pending_sessions = _Queue(*PENDING_SESSIONS)
Expand All @@ -771,7 +771,7 @@ def test_begin_pending_transactions_non_empty(self):
pool.begin_pending_transactions() # no raise

for txn in TRANSACTIONS:
self.assertTrue(txn._begun)
txn.begin.assert_called_once_with()

self.assertTrue(pending.empty())

Expand Down Expand Up @@ -832,17 +832,13 @@ def test_context_manager_w_kwargs(self):
self.assertEqual(pool._got, {"foo": "bar"})


class _Transaction(object):
def _make_transaction(*args, **kw):
from google.cloud.spanner_v1.transaction import Transaction

_begun = False
_committed = False
_rolled_back = False

def begin(self):
self._begun = True

def committed(self):
return self._committed
txn = mock.create_autospec(Transaction)(*args, **kw)
txn.committed = None
txn._rolled_back = False
return txn


@total_ordering
Expand Down Expand Up @@ -873,7 +869,7 @@ def delete(self):
raise NotFound("unknown session")

def transaction(self):
txn = self._transaction = _Transaction()
txn = self._transaction = _make_transaction(self)
return txn


Expand Down

0 comments on commit 20ff981

Please sign in to comment.