Ejemplo n.º 1
0
    def test_can_deserialize_lock_without_auto_refresher(self):
        bridge_factory = mock.Mock()
        mock_bridge = mock.Mock()
        mock_backend = mock.Mock()
        bridge_factory.create.return_value = (mock_bridge, mock_backend)
        session = Session(
            'table_name',
            backend_bridge_factory=bridge_factory,
        )
        serialized_lock = json.dumps({
            '__version':
            'Lock.1',
            'name':
            'foo',
            'technique': ('{"__version": "VersionLeaseTechinque.1", '
                          '"versions": {"foo": "version-identifier"}}'),
        })
        lock = session.deserialize_lock(serialized_lock, auto_refresh=False)
        assert isinstance(lock, Lock)

        mock_bridge.we_own_lock.assert_called_with('version-identifier')
        mock_backend.update.assert_called_with(
            {'lockKey': 'foo'},
            condition=mock.ANY,
            updates=mock.ANY,
        )
Ejemplo n.º 2
0
    def test_can_deserialize_lock(self):
        bridge_factory = mock.Mock()
        mock_bridge = mock.Mock()
        mock_backend = mock.Mock()
        bridge_factory.create.return_value = (mock_bridge, mock_backend)
        session = Session(
            'table_name',
            backend_bridge_factory=bridge_factory,
        )
        serialized_lock = json.dumps({
            '__version':
            'Lock.1',
            'name':
            'foo',
            'technique': ('{"__version": "VersionLeaseTechinque.1", '
                          '"versions": {"foo": "version-identifier"}}'),
        })
        lock = session.deserialize_lock(serialized_lock)
        assert isinstance(lock, Lock)

        # Ensure that the lock tried to refresh itself asap after being
        # deserialized since we don't know how much time has gone by.
        mock_bridge.we_own_lock.assert_called_with('version-identifier')
        mock_backend.update.assert_called_with(
            {'lockKey': 'foo'},
            condition=mock.ANY,
            updates=mock.ANY,
        )
Ejemplo n.º 3
0
 def test_can_create_lock(self):
     identifier = 'foobar'
     bridge_factory = mock.Mock()
     bridge_factory.create.return_value = (mock.Mock(), mock.Mock())
     session = Session(
         'table_name',
         host_identifier=identifier,
         backend_bridge_factory=bridge_factory,
     )
     lock = session.create_lock('foo')
     assert isinstance(lock, Lock)
Ejemplo n.º 4
0
    def test_does_raise_on_invalid_lock(self):
        bridge_factory = mock.Mock()
        mock_bridge = mock.Mock()
        mock_backend = mock.Mock()
        bridge_factory.create.return_value = (mock_bridge, mock_backend)
        session = Session(
            'table_name',
            backend_bridge_factory=bridge_factory,
        )
        serialized_lock = json.dumps({})

        with pytest.raises(CannotDeserializeError):
            session.deserialize_lock(serialized_lock, auto_refresh=False)
Ejemplo n.º 5
0
 def test_can_create_lock_without_refresher(self):
     identifier = 'foobar'
     bridge_factory = mock.Mock()
     bridge_factory.create.return_value = (mock.Mock(), mock.Mock())
     session = Session(
         'table_name',
         host_identifier=identifier,
         backend_bridge_factory=bridge_factory,
     )
     lock = session.create_lock('foo', auto_refresh=False)
     assert isinstance(lock, Lock)
     # No other easy way to check this without a real backend. So for a
     # simple unit test we will reach into the private varaible to check.
     assert lock._refresher_factory is None
Ejemplo n.º 6
0
    def test_does_raise_on_missing_technique(self):
        bridge_factory = mock.Mock()
        mock_bridge = mock.Mock()
        mock_backend = mock.Mock()
        bridge_factory.create.return_value = (mock_bridge, mock_backend)
        session = Session(
            'table_name',
            backend_bridge_factory=bridge_factory,
        )
        serialized_lock = json.dumps({
            '__version': 'Lock.1',
            'technique': '...',
        })

        with pytest.raises(CannotDeserializeError):
            session.deserialize_lock(serialized_lock, auto_refresh=False)
Ejemplo n.º 7
0
def get_session(table_name, host_identifier=None, backend_bridge_factory=None):
    """Create a new :class:`lynk.session.Session` with default settings.

    This is a convenience function for getting a default configured
    Session.

    :type table_name: str
    :param table_name: Name of the table in the backend.

    :type host_identifier: str
    :param host_identifier: A unique identifier for a host. A host is just
        an unused field in the database. It is for debugging and nothing
        more so any value can be used that has meaning to the developer.
        By default hostname is used.

    :type backend_bridge_factory: Anything with a create method or None.
    :param backend_bridge_factory: A factory that creates our backend and
        its associated bridge class to be injected into our lock. Usually
        these need to be created by a shared factory class because they
        have shared dependencies. If None is provided the default is a
        :class:`lynk.backends.dynamodb.DynamoDBBackendBridgeFactory` which
        will create locks bound to a DynamoDB Table.
    """
    return Session(table_name, host_identifier, backend_bridge_factory)
Ejemplo n.º 8
0
 def test_can_create_session(self):
     session = Session('table_name')
     assert isinstance(session, Session)