コード例 #1
0
async def test_is_modified():
    sess = SessionDict(session=MockSession(master_interface=MockInterface()),
                       warn_lock=False)

    # Doesn't set is_modified upon instantiation
    assert sess.is_modified is False

    # Doesn't set is modified with ctx manager alone
    async with sess:
        pass
    assert sess.is_modified is False

    # sets is_modified False when exiting context manager
    async with sess:
        sess["foo"] = "bar"
        assert sess.is_modified is True
    assert sess.is_modified is False

    # Sets is_modified on write
    sess["foo"] = "baz"
    assert sess.is_modified is True

    # Doesn't set is_modified on read
    sess.is_modified = False
    sess["foo"]
    assert sess.is_modified is False
コード例 #2
0
async def test_awaits_locked_session_dict(Session, Interface):
    sess_man = Session(master_interface=Interface())
    SID = "yet_another_sid"
    session_dict = SessionDict(sid=SID, session=sess_man)

    lock = asyncio.Lock()
    await lock.acquire()

    lock_keeper.acquired_locks[SID] = lock

    assert lock_keeper.acquired_locks[SID].locked() is True

    # It seems that there has been a change in the asyncio.Lock implementation
    # In Python 3.8. When there are no awaiters in Py38, the value of _waiters
    # is None and in py37, the value of _waiters is an empty deque
    minor_version = sys.version_info[1]
    if minor_version == 7:
        assert len(lock_keeper.acquired_locks[SID]._waiters) == 0
    else:
        assert lock_keeper.acquired_locks[SID]._waiters is None

    with pytest.raises(asyncio.TimeoutError):
        async with timeout(0.1):
            async with session_dict:
                session_dict["asd"]
                assert len(lock_keeper.acquired_locks[SID]._waiters) == 1
コード例 #3
0
async def test_aenter_returns_same_instance():
    interface = MockInterface()
    session = MockSession()
    session.set_master_interface(interface)
    sess = SessionDict(sid="asd", session=session)

    async with sess as ret_sess:
        assert ret_sess is sess
コード例 #4
0
def test_custom_getattr():
    sess = SessionDict()

    for key in ("pop", "popitem", "update", "clear", "setdefault"):
        assert getattr(sess, key)

    for key in ("asd", "fasdaif", "gapisdaoisd"):
        with pytest.raises(AttributeError):
            getattr(sess, key)
コード例 #5
0
def test_custom_getattr():
    sess = SessionDict()

    for key in ('pop', 'popitem', 'update', 'clear', 'setdefault'):
        assert getattr(sess, key)

    for key in ('asd', 'fasdaif', 'gapisdaoisd'):
        with pytest.raises(AttributeError):
            getattr(sess, key)
コード例 #6
0
async def test_warns_with_unlocked_access(Session, Interface):
    sess_man = Session(master_interface=Interface())
    SID = 'an_sid'
    session_dict = SessionDict(sid=SID, session=sess_man, warn_lock=True)

    with pytest.warns(RuntimeWarning):
        session_dict['foo'] = 'bar'

    with pytest.warns(RuntimeWarning):
        session_dict['foo']
コード例 #7
0
async def test_only_saves_with_ctx(Session, Interface):
    sess_man = Session(master_interface=Interface())
    SID = 'another_sid'
    session_dict = SessionDict(sid=SID,
                               session=sess_man,
                               request=MockRequest(),
                               warn_lock=False)

    session_dict['foo'] = 'bar'

    with pytest.warns(RuntimeWarning):  # Mixed access warning
        async with session_dict as sess:
            # Should find None, because when you access without a context manager
            # the only time it will save is after the response middleware is called
            sess.get('foo') is None
コード例 #8
0
async def test_session_dict_locked_by_sid(Session, Interface):
    mock_interface = Interface()
    sess_man = Session(master_interface=mock_interface)
    SID = 'sid'
    session_dict = SessionDict(sid=SID, session=sess_man)

    async with session_dict as sess:
        assert lock_keeper.acquired_locks[SID].locked() is True
        sess['foo'] = 'bar'
        assert sess['foo'] == 'bar'

    sess = await sess_man._fetch_sess(SID)
    assert sess['foo'] == 'bar'

    async with session_dict as sess:
        assert sess['foo'] == 'bar'
    assert lock_keeper.acquired_locks.get(SID) is None
コード例 #9
0
async def test_awaits_locked_session_dict(Session, Interface):
    sess_man = Session(master_interface=Interface())
    SID = 'yet_another_sid'
    session_dict = SessionDict(sid=SID, session=sess_man)

    lock = asyncio.Lock()
    await lock.acquire()

    lock_keeper.acquired_locks[SID] = lock

    assert lock_keeper.acquired_locks[SID].locked() is True

    assert len(lock_keeper.acquired_locks[SID]._waiters) == 0

    with pytest.raises(asyncio.TimeoutError):
        async with timeout(0.1):
            async with session_dict:
                session_dict['asd']
                assert len(lock_keeper.acquired_locks[SID]._waiters) == 1