def test_receive_request_with_same_owner_should_do_nothing( self, fork: Fork, philosopher: Philosopher): fork._owner = philosopher fork.request(philosopher) assert fork._owner == philosopher
def test_done_should_change_state_to_dirty_and_notify( self, fork: Fork, philosopher: Philosopher): with mock.patch('dining_philosophers.forks.Condition.notify_all' ) as mock_notify_all: fork.done() assert fork.state == ForkState.DIRTY assert mock_notify_all.called
def test_create_philosopher(self): ID = 0 left_fork = Fork(0) right_fork = Fork(1) philosopher = Philosopher(ID, (left_fork, right_fork)) assert philosopher.id == ID assert philosopher.state == PhilosopherState.THINKING
def test_receive_request_from_some_neighbor_with_dirty_state_should_clean_it_and_change_owner( # noqa self, fork: Fork, philosopher: Philosopher): another_fork = Fork(1) another_philosopher = Philosopher(1, (fork, another_fork)) fork._owner = another_philosopher fork.request(philosopher) assert fork.state == ForkState.CLEAN assert fork._owner == philosopher
def test_run_philosopher_thread(self): ID = 0 left_fork = Fork(0) right_fork = Fork(1) philosopher = Philosopher(ID, (left_fork, right_fork)) with mock.patch( 'dining_philosophers.philosophers.threading.Thread.start' ) as mock_start_thread: philosopher.start() mock_start_thread.assert_called_once()
def test_receive_request_from_some_neighbor_with_clean_state_should_wait_until_the_owner_is_done( # noqa self, fork: Fork, philosopher: Philosopher): another_fork = Fork(1) another_philosopher = Philosopher(1, (fork, another_fork)) fork.state = ForkState.CLEAN fork._owner = another_philosopher with mock.patch( 'dining_philosophers.forks.Condition.wait') as mock_wait: fork.request(philosopher) assert mock_wait.called
def fork(): return Fork(0)
def philosopher(): left_fork = Fork(0) right_fork = Fork(1) return Philosopher(0, [left_fork, right_fork])
def _create_forks(number_of_philosophers: int) -> List[Fork]: return [Fork(i) for i in range(number_of_philosophers)]
def test_create_fork_should_initiate_with_the_dirty_state(self): fork = Fork(0) assert fork.state == ForkState.DIRTY