<s> import threading <EOL> import unittest <EOL> import mock <EOL> from nose . tools import assert_raises <EOL> from nose . tools import eq_ <EOL> from nose . tools import raises <EOL> class TestThreadingHandler ( unittest . TestCase ) : <EOL> def _makeOne ( self , * args ) : <EOL> from kazoo . handlers . threading import SequentialThreadingHandler <EOL> return SequentialThreadingHandler ( * args ) <EOL> def _getAsync ( self , * args ) : <EOL> from kazoo . handlers . threading import AsyncResult <EOL> return AsyncResult <EOL> def test_proper_threading ( self ) : <EOL> h = self . _makeOne ( ) <EOL> h . start ( ) <EOL> event_class = getattr ( threading , '<STR_LIT>' , threading . Event ) <EOL> assert isinstance ( h . event_object ( ) , event_class ) <EOL> def test_matching_async ( self ) : <EOL> h = self . _makeOne ( ) <EOL> h . start ( ) <EOL> async = self . _getAsync ( ) <EOL> assert isinstance ( h . async_result ( ) , async ) <EOL> def test_exception_raising ( self ) : <EOL> h = self . _makeOne ( ) <EOL> @ raises ( h . timeout_exception ) <EOL> def testit ( ) : <EOL> raise h . timeout_exception ( "<STR_LIT>" ) <EOL> testit ( ) <EOL> def test_double_start_stop ( self ) : <EOL> h = self . _makeOne ( ) <EOL> h . start ( ) <EOL> self . assertTrue ( h . _running ) <EOL> h . start ( ) <EOL> h . stop ( ) <EOL> h . stop ( ) <EOL> self . assertFalse ( h . _running ) <EOL> class TestThreadingAsync ( unittest . TestCase ) : <EOL> def _makeOne ( self , * args ) : <EOL> from kazoo . handlers . threading import AsyncResult <EOL> return AsyncResult ( * args ) <EOL> def _makeHandler ( self ) : <EOL> from kazoo . handlers . threading import SequentialThreadingHandler <EOL> return SequentialThreadingHandler ( ) <EOL> def test_ready ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> eq_ ( async . ready ( ) , False ) <EOL> async . set ( '<STR_LIT>' ) <EOL> eq_ ( async . ready ( ) , True ) <EOL> eq_ ( async . successful ( ) , True ) <EOL> eq_ ( async . exception , None ) <EOL> def test_callback_queued ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> mock_handler . completion_queue = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> async . rawlink ( lambda a : a ) <EOL> async . set ( '<STR_LIT>' ) <EOL> assert mock_handler . completion_queue . put . called <EOL> def test_set_exception ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> mock_handler . completion_queue = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> async . rawlink ( lambda a : a ) <EOL> async . set_exception ( ImportError ( '<STR_LIT>' ) ) <EOL> assert isinstance ( async . exception , ImportError ) <EOL> assert mock_handler . completion_queue . put . called <EOL> def test_get_wait_while_setting ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> bv = threading . Event ( ) <EOL> cv = threading . Event ( ) <EOL> def wait_for_val ( ) : <EOL> bv . set ( ) <EOL> val = async . get ( ) <EOL> lst . append ( val ) <EOL> cv . set ( ) <EOL> th = threading . Thread ( target = wait_for_val ) <EOL> th . start ( ) <EOL> bv . wait ( ) <EOL> async . set ( '<STR_LIT>' ) <EOL> cv . wait ( ) <EOL> eq_ ( lst , [ '<STR_LIT>' ] ) <EOL> th . join ( ) <EOL> def test_get_with_nowait ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> timeout = self . _makeHandler ( ) . timeout_exception <EOL> @ raises ( timeout ) <EOL> def test_it ( ) : <EOL> async . get ( block = False ) <EOL> test_it ( ) <EOL> @ raises ( timeout ) <EOL> def test_nowait ( ) : <EOL> async . get_nowait ( ) <EOL> test_nowait ( ) <EOL> def test_get_with_exception ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> bv = threading . Event ( ) <EOL> cv = threading . Event ( ) <EOL> def wait_for_val ( ) : <EOL> bv . set ( ) <EOL> try : <EOL> val = async . get ( ) <EOL> except ImportError : <EOL> lst . append ( '<STR_LIT>' ) <EOL> else : <EOL> lst . append ( val ) <EOL> cv . set ( ) <EOL> th = threading . Thread ( target = wait_for_val ) <EOL> th . start ( ) <EOL> bv . wait ( ) <EOL> async . set_exception ( ImportError ) <EOL> cv . wait ( ) <EOL> eq_ ( lst , [ '<STR_LIT>' ] ) <EOL> th . join ( ) <EOL> def test_wait ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> bv = threading . Event ( ) <EOL> cv = threading . Event ( ) <EOL> def wait_for_val ( ) : <EOL> bv . set ( ) <EOL> try : <EOL> val = async . wait ( <NUM_LIT:10> ) <EOL> except ImportError : <EOL> lst . append ( '<STR_LIT>' ) <EOL> else : <EOL> lst . append ( val ) <EOL> cv . set ( ) <EOL> th = threading . Thread ( target = wait_for_val ) <EOL> th . start ( ) <EOL> bv . wait ( <NUM_LIT:10> ) <EOL> async . set ( "<STR_LIT>" ) <EOL> cv . wait ( <NUM_LIT:15> ) <EOL> eq_ ( lst , [ True ] ) <EOL> th . join ( ) <EOL> def test_set_before_wait ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> cv = threading . Event ( ) <EOL> async . set ( '<STR_LIT>' ) <EOL> def wait_for_val ( ) : <EOL> val = async . get ( ) <EOL> lst . append ( val ) <EOL> cv . set ( ) <EOL> th = threading . Thread ( target = wait_for_val ) <EOL> th . start ( ) <EOL> cv . wait ( ) <EOL> eq_ ( lst , [ '<STR_LIT>' ] ) <EOL> th . join ( ) <EOL> def test_set_exc_before_wait ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> cv = threading . Event ( ) <EOL> async . set_exception ( ImportError ) <EOL> def wait_for_val ( ) : <EOL> try : <EOL> val = async . get ( ) <EOL> except ImportError : <EOL> lst . append ( '<STR_LIT>' ) <EOL> else : <EOL> lst . append ( val ) <EOL> cv . set ( ) <EOL> th = threading . Thread ( target = wait_for_val ) <EOL> th . start ( ) <EOL> cv . wait ( ) <EOL> eq_ ( lst , [ '<STR_LIT>' ] ) <EOL> th . join ( ) <EOL> def test_linkage ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> cv = threading . Event ( ) <EOL> lst = [ ] <EOL> def add_on ( ) : <EOL> lst . append ( True ) <EOL> def wait_for_val ( ) : <EOL> async . get ( ) <EOL> cv . set ( ) <EOL> th = threading . Thread ( target = wait_for_val ) <EOL> th . start ( ) <EOL> async . rawlink ( add_on ) <EOL> async . set ( '<STR_LIT>' ) <EOL> assert mock_handler . completion_queue . put . called <EOL> async . unlink ( add_on ) <EOL> cv . wait ( ) <EOL> eq_ ( async . value , '<STR_LIT>' ) <EOL> th . join ( ) <EOL> def test_linkage_not_ready ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> def add_on ( ) : <EOL> lst . append ( True ) <EOL> async . set ( '<STR_LIT>' ) <EOL> assert not mock_handler . completion_queue . called <EOL> async . rawlink ( add_on ) <EOL> assert mock_handler . completion_queue . put . called <EOL> def test_link_and_unlink ( self ) : <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> def add_on ( ) : <EOL> lst . append ( True ) <EOL> async . rawlink ( add_on ) <EOL> assert not mock_handler . completion_queue . put . called <EOL> async . unlink ( add_on ) <EOL> async . set ( '<STR_LIT>' ) <EOL> assert not mock_handler . completion_queue . put . called <EOL> def test_captured_exception ( self ) : <EOL> from kazoo . handlers . utils import capture_exceptions <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> @ capture_exceptions ( async ) <EOL> def exceptional_function ( ) : <EOL> return <NUM_LIT:1> / <NUM_LIT:0> <EOL> exceptional_function ( ) <EOL> assert_raises ( ZeroDivisionError , async . get ) <EOL> def test_no_capture_exceptions ( self ) : <EOL> from kazoo . handlers . utils import capture_exceptions <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> def add_on ( ) : <EOL> lst . append ( True ) <EOL> async . rawlink ( add_on ) <EOL> @ capture_exceptions ( async ) <EOL> def regular_function ( ) : <EOL> return True <EOL> regular_function ( ) <EOL> assert not mock_handler . completion_queue . put . called <EOL> def test_wraps ( self ) : <EOL> from kazoo . handlers . utils import wrap <EOL> mock_handler = mock . Mock ( ) <EOL> async = self . _makeOne ( mock_handler ) <EOL> lst = [ ] <EOL> def add_on ( result ) : <EOL> lst . append ( result . get ( ) ) <EOL> async . rawlink ( add_on ) <EOL> @ wrap ( async ) <EOL> def regular_function ( ) : <EOL> return '<STR_LIT:hello>' <EOL> assert regular_function ( ) == '<STR_LIT:hello>' <EOL> assert mock_handler . completion_queue . put . called <EOL> assert async . get ( ) == '<STR_LIT:hello>' </s>
