<s> import os <EOL> import zmq <EOL> import time <EOL> import shutil <EOL> import threading <EOL> import unittest2 <EOL> from elevator . db import DatabaseStore <EOL> from elevator . backend . supervisor import Supervisor <EOL> from elevator . backend . worker import Worker <EOL> from elevator . backend . protocol import WORKER_STATUS <EOL> from . . fakers import gen_test_config <EOL> class SupervisorTest ( unittest2 . TestCase ) : <EOL> def setUp ( self ) : <EOL> zmq_context = zmq . Context ( ) <EOL> config = gen_test_config ( ) <EOL> self . database_store = config [ '<STR_LIT>' ] <EOL> self . databases_storage_path = config [ '<STR_LIT>' ] <EOL> if not os . path . exists ( self . databases_storage_path ) : <EOL> os . mkdir ( self . databases_storage_path ) <EOL> self . db_handler = DatabaseStore ( config ) <EOL> self . socket = zmq_context . socket ( zmq . DEALER ) <EOL> self . socket . bind ( '<STR_LIT>' ) <EOL> self . supervisor = Supervisor ( zmq_context , self . db_handler ) <EOL> def tearDown ( self ) : <EOL> self . supervisor . stop_all ( ) <EOL> os . remove ( self . database_store ) <EOL> shutil . rmtree ( self . databases_storage_path ) <EOL> def test_init_workers_with_positive_count ( self ) : <EOL> start_thread_count = len ( threading . enumerate ( ) ) <EOL> workers_count = <NUM_LIT:4> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( threading . enumerate ( ) ) , <EOL> start_thread_count + workers_count ) <EOL> self . assertEqual ( len ( self . supervisor . workers ) , workers_count ) <EOL> for _id , worker in self . supervisor . workers . iteritems ( ) : <EOL> self . assertIn ( '<STR_LIT>' , worker ) <EOL> self . assertIsInstance ( worker [ '<STR_LIT>' ] , threading . Thread ) <EOL> self . assertTrue ( worker [ '<STR_LIT>' ] . isAlive ( ) ) <EOL> self . assertIn ( '<STR_LIT>' , worker ) <EOL> def test_stop_specific_worker ( self ) : <EOL> start_thread_count = len ( threading . enumerate ( ) ) <EOL> workers_count = <NUM_LIT:4> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> worker_to_stop = self . supervisor . workers . keys ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( len ( threading . enumerate ( ) ) , <EOL> start_thread_count + workers_count ) <EOL> self . supervisor . stop ( worker_to_stop ) <EOL> self . assertEqual ( len ( threading . enumerate ( ) ) , <EOL> ( start_thread_count + workers_count ) - <NUM_LIT:1> ) <EOL> self . assertNotIn ( worker_to_stop , self . supervisor . workers ) <EOL> def test_stop_all ( self ) : <EOL> start_thread_count = len ( threading . enumerate ( ) ) <EOL> workers_count = <NUM_LIT:4> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( threading . enumerate ( ) ) , <EOL> start_thread_count + workers_count ) <EOL> self . supervisor . stop_all ( ) <EOL> self . assertEqual ( len ( threading . enumerate ( ) ) , <EOL> start_thread_count ) <EOL> self . assertEqual ( len ( self . supervisor . workers ) , <NUM_LIT:0> ) <EOL> def test_specific_worker_status ( self ) : <EOL> workers_count = <NUM_LIT:4> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> worker_to_ask = self . supervisor . workers . keys ( ) [ <NUM_LIT:0> ] <EOL> status = self . supervisor . status ( worker_to_ask ) <EOL> self . assertIsInstance ( status , list ) <EOL> self . assertEqual ( len ( status ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( status [ <NUM_LIT:0> ] , ( str ( Worker . STATES . IDLE ) , ) ) <EOL> def test_workers_status ( self ) : <EOL> workers_count = <NUM_LIT:4> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> status = self . supervisor . statuses ( ) <EOL> self . assertIsInstance ( status , list ) <EOL> self . assertEqual ( len ( status ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( status , [ ( str ( Worker . STATES . IDLE ) , ) ] * <NUM_LIT:4> ) <EOL> def test_inactive_worker_last_activity ( self ) : <EOL> workers_count = <NUM_LIT:2> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> worker_to_ask = self . supervisor . workers . keys ( ) [ <NUM_LIT:0> ] <EOL> status = self . supervisor . last_activity ( worker_to_ask ) <EOL> self . assertIsInstance ( status , list ) <EOL> self . assertEqual ( len ( status ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( status [ <NUM_LIT:0> ] , ( None , None ) ) <EOL> def test_inactive_workers_last_activities ( self ) : <EOL> workers_count = <NUM_LIT:2> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> status = self . supervisor . last_activity_all ( ) <EOL> self . assertIsInstance ( status , list ) <EOL> self . assertEqual ( len ( status ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( status [ <NUM_LIT:0> ] , ( None , None ) ) <EOL> self . assertEqual ( status [ <NUM_LIT:1> ] , ( None , None ) ) <EOL> def test_valid_working_command_with_workers ( self ) : <EOL> workers_count = <NUM_LIT:4> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> responses = self . supervisor . command ( WORKER_STATUS , <EOL> max_retries = <NUM_LIT:1> , <EOL> timeout = <NUM_LIT:100> ) <EOL> self . assertIsInstance ( responses , list ) <EOL> self . assertGreaterEqual ( len ( responses ) , <NUM_LIT:4> ) <EOL> def test_valid_working_command_without_workers ( self ) : <EOL> responses = self . supervisor . command ( WORKER_STATUS , <EOL> max_retries = <NUM_LIT:1> , <EOL> timeout = <NUM_LIT:100> ) <EOL> self . assertIsInstance ( responses , list ) <EOL> self . assertEqual ( responses , [ ] ) <EOL> def test_invalid_command_with_workers ( self ) : <EOL> workers_count = <NUM_LIT:2> <EOL> self . supervisor . init_workers ( workers_count ) <EOL> responses = self . supervisor . command ( "<STR_LIT>" , <EOL> max_retries = <NUM_LIT:1> , <EOL> timeout = <NUM_LIT:100> ) <EOL> self . assertIsInstance ( responses , list ) <EOL> self . assertEqual ( responses , [ ] ) </s>
