from summitidp import locking

def fake_start_response(*args, **kwargs):
    pass

class FakeException(Exception):
    pass

def test_synced_app_works_consecutively():
    @locking.synced_app
    def myapp(environ, start_response):
        assert start_response == fake_start_response
        assert isinstance(environ, dict)
        return []

    assert myapp({}, fake_start_response) == []
    assert myapp({}, fake_start_response) == []

def test_synced_app_throws_exception():
    @locking.synced_app
    def myapp(environ, start_response):
        assert start_response == fake_start_response
        assert isinstance(environ, dict)
        raise FakeException()

    try:
        myapp({}, fake_start_response)
        raise Exception("exception should have been raised!")
    except FakeException:
        pass

    # Ensure the lock was released when the exception was thrown.

    try:
        myapp({}, fake_start_response)
        raise Exception("exception should have been raised!")
    except FakeException:
        pass
