Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread local context. #131

Merged
merged 2 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/google/cloud/ndb/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
class _LocalState(threading.local):
"""Thread local state."""

__slots__ = ("context",)

def __init__(self):
self.context = None

Expand Down
30 changes: 30 additions & 0 deletions tests/system/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""
import functools
import operator
import threading

import pytest

Expand Down Expand Up @@ -150,6 +151,35 @@ class SomeKind(ndb.Model):
dispose_of(key._key)


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):
Expand Down