Add with_lock context manager and accompanying tests