From 54756fb994fae7f9233ffe8021475cfc51447161 Mon Sep 17 00:00:00 2001 From: Chris Rossi Date: Mon, 1 Jul 2019 14:12:38 -0400 Subject: [PATCH 1/2] Fix thread local context. It turns out that if you use ``__slots__`` in a ``threading.local`` subclass, it no longer works as a ``threading.local`` instance. The more you know... Fixes #128. --- src/google/cloud/ndb/context.py | 2 -- tests/system/test_crud.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/google/cloud/ndb/context.py b/src/google/cloud/ndb/context.py index 289147d8..740ed6b6 100644 --- a/src/google/cloud/ndb/context.py +++ b/src/google/cloud/ndb/context.py @@ -36,8 +36,6 @@ class _LocalState(threading.local): """Thread local state.""" - __slots__ = ("context",) - def __init__(self): self.context = None diff --git a/tests/system/test_crud.py b/tests/system/test_crud.py index bd10d85a..69ee1ca0 100644 --- a/tests/system/test_crud.py +++ b/tests/system/test_crud.py @@ -17,6 +17,7 @@ """ import functools import operator +import threading import pytest @@ -150,6 +151,36 @@ class SomeKind(ndb.Model): dispose_of(key._key) +@pytest.mark.usefixtures("client_context") +def test_parallel_threads(dispose_of, namespace): + client = ndb.Client(namespace=namespace) + + class SomeKind(ndb.Model): + foo = ndb.IntegerProperty() + bar = ndb.StringProperty() + + def insert(foo): + with client.context(cache_policy=False): + entity = SomeKind(foo=foo, bar="none") + + key = entity.put() + + retrieved = key.get() + assert retrieved.foo == foo + assert retrieved.bar == "none" + + dispose_of(key._key) + + thread1 = threading.Thread(target=insert, args=[42], name="one") + thread2 = threading.Thread(target=insert, args=[144], name="two") + + thread1.start() + thread2.start() + + thread1.join() + thread2.join() + + @pytest.mark.usefixtures("client_context") def test_large_json_property(dispose_of, ds_client): class SomeKind(ndb.Model): From a52eee7a327d03407f5f3ce6b0d85174afb04928 Mon Sep 17 00:00:00 2001 From: Chris Rossi Date: Mon, 1 Jul 2019 14:16:15 -0400 Subject: [PATCH 2/2] Don't create unnecessary context. --- tests/system/test_crud.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/system/test_crud.py b/tests/system/test_crud.py index 69ee1ca0..177ba2a4 100644 --- a/tests/system/test_crud.py +++ b/tests/system/test_crud.py @@ -151,7 +151,6 @@ class SomeKind(ndb.Model): dispose_of(key._key) -@pytest.mark.usefixtures("client_context") def test_parallel_threads(dispose_of, namespace): client = ndb.Client(namespace=namespace)