Example #1
0
    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
Example #2
0
    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
Example #3
0
    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
Example #4
0
    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
Example #5
0
    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()
Example #6
0
    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
Example #7
0
def fork():
    return Fork(0)
Example #8
0
def philosopher():
    left_fork = Fork(0)
    right_fork = Fork(1)

    return Philosopher(0, [left_fork, right_fork])
Example #9
0
 def _create_forks(number_of_philosophers: int) -> List[Fork]:
     return [Fork(i) for i in range(number_of_philosophers)]
Example #10
0
    def test_create_fork_should_initiate_with_the_dirty_state(self):
        fork = Fork(0)

        assert fork.state == ForkState.DIRTY