whoosh / tests / test_misc.py

from __future__ import with_statement
import threading, time

from nose.tools import assert_equal  #@UnresolvedImport

from whoosh.support.filelock import try_for
from whoosh.util import length_to_byte, byte_to_length
from whoosh.support.testing import TempStorage

def test_filelock_simple():
    with TempStorage("simplefilelock") as st:
        lock1 = st.lock("testlock")
        lock2 = st.lock("testlock")
        assert lock1 is not lock2
        assert lock1.acquire()
        assert st.file_exists("testlock")
        assert not lock2.acquire()
        assert lock2.acquire()
        assert not lock1.acquire()
def test_threaded_filelock():
    with TempStorage("threadedfilelock") as st:
        lock1 = st.lock("testlock")
        result = []
        # The thread function tries to acquire the lock and then quits
        def fn():
            lock2 = st.lock("testlock")
            gotit = try_for(lock2.acquire, 1.0, 0.1)
            if gotit:
        t = threading.Thread(target=fn)
        # Acquire the lock in this thread
        # Start the other thread trying to acquire the lock
        # Wait for a bit
        # Release the lock
        # Wait for the other thread to finish
        # If the other thread got the lock, it should have appended True to the
        # "results" list.
        assert_equal(result, [True])
def test_length_byte():
    source = list(range(11))
    xform = [length_to_byte(n) for n in source]
    result = [byte_to_length(n) for n in xform]
    assert_equal(source, result)
def test_lru_cache():
    from whoosh.util import lru_cache
    def test(n):
        return n * 2
    result = [test(n) for n in (1, 2, 3, 4, 5, 4, 3, 2, 10, 1)]
    assert_equal(result, [2, 4, 6, 8, 10, 8, 6, 4, 20, 2])
    assert_equal(test.cache_info(), (3, 7, 5, 5))
    assert_equal(test.cache_info(), (0, 0, 5, 0))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.