def context(self):
        """Establish a context for a set of NDB calls.

        This method provides a context manager which establishes the runtime
        state for using NDB.

        For example:

        .. code-block:: python

            from google.cloud import ndb

            client = ndb.Client()
            with client.context():
                # Use NDB for some stuff
                pass

        Use of a context is required--NDB can only be used inside a running
        context. The context is used to manage the connection to Google Cloud
        Datastore, an event loop for asynchronous API calls, runtime caching
        policy, and other essential runtime state.

        Code within an asynchronous context should be single threaded.
        Internally, a :class:`threading.local` instance is used to track the
        current event loop.

        In a web application, it is recommended that a single context be used
        per HTTP request. This can typically be accomplished in a middleware
        layer.
        """
        return _runstate.state_context(self)
def test_get_event_loop():
    with pytest.raises(exceptions.ContextError):
        _eventloop.get_event_loop()
    with _runstate.state_context(None):
        loop = _eventloop.get_event_loop()
        assert isinstance(loop, _eventloop.EventLoop)
        assert _eventloop.get_event_loop() is loop
Beispiel #3
0
 def test_insecure_channel(datastore_pb2_grpc, grpc):
     channel = grpc.insecure_channel.return_value
     client = mock.Mock(
         secure=False, host="thehost", spec=("secure", "host")
     )
     with _runstate.state_context(client):
         stub = _api.stub()
     assert stub is datastore_pb2_grpc.DatastoreStub.return_value
     datastore_pb2_grpc.DatastoreStub.assert_called_once_with(channel)
     grpc.insecure_channel.assert_called_once_with("thehost")
def test_state_context():
    assert _runstate.states.current() is None

    client1 = object()
    client2 = object()
    with _runstate.state_context(client1):
        one = _runstate.current()
        assert one.client is client1

        with _runstate.state_context(client2):
            two = _runstate.current()
            assert two.client is client2
            assert one is not two
            two.eventloop = unittest.mock.Mock(spec=("run", ))
            two.eventloop.run.assert_not_called()

        assert _runstate.current() is one
        two.eventloop.run.assert_called_once_with()

    assert _runstate.states.current() is None
Beispiel #5
0
 def test_secure_channel(datastore_pb2_grpc, _helpers):
     channel = _helpers.make_secure_channel.return_value
     client = mock.Mock(
         _credentials="creds",
         secure=True,
         host="thehost",
         spec=("_credentials", "secure", "host"),
     )
     with _runstate.state_context(client):
         stub = _api.stub()
         assert _api.stub() is stub  # one stub per context
     assert stub is datastore_pb2_grpc.DatastoreStub.return_value
     datastore_pb2_grpc.DatastoreStub.assert_called_once_with(channel)
     _helpers.make_secure_channel.assert_called_once_with(
         "creds", _http.DEFAULT_USER_AGENT, "thehost")
def test_run1(EventLoop):
    EventLoop.return_value = loop = unittest.mock.Mock(spec=("run", "run1"))
    with _runstate.state_context(None):
        _eventloop.run1()
        loop.run1.assert_called_once_with()
def test_queue_rpc(EventLoop):
    EventLoop.return_value = loop = unittest.mock.Mock(spec=("run",
                                                             "queue_rpc"))
    with _runstate.state_context(None):
        _eventloop.queue_rpc("foo", "bar")
        loop.queue_rpc.assert_called_once_with("foo", "bar")
def test_add_idle(EventLoop):
    EventLoop.return_value = loop = unittest.mock.Mock(spec=("run",
                                                             "add_idle"))
    with _runstate.state_context(None):
        _eventloop.add_idle("foo", "bar", baz="qux")
        loop.add_idle.assert_called_once_with("foo", "bar", baz="qux")
Beispiel #9
0
def runstate():
    client = None
    with _runstate.state_context(client) as state:
        yield state
def with_runstate_context():
    client = None
    with _runstate.state_context(client):
        yield