Пример #1
0
def test_events_multi_thread_handler():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5|-1,0,0,0,1'
    events = picraft.events.Events(conn)
    result = []
    lock = threading.Lock()
    @events.on_block_hit(thread=True)
    def handler(event):
        with lock:
            result.append(event)
    events.process()
    # The handler is threaded so there's no guarantee the result has been
    # filled by this point. Hence, we wait up to two seconds for that to
    # happen, with a small wait between acquiring locks to avoid lock
    # starvation in the background threads.
    start = time.time()
    while time.time() - start < 2:
        with lock:
            if len(result) == 2:
                if result[0].pos != Vector(1, 2, 3):
                    result[0], result[1] = result[1], result[0]
                assert result[0].pos == Vector(1, 2, 3)
                assert result[0].face == 'x-'
                assert result[0].player.player_id == 5
                assert result[1].pos == Vector(-1, 0, 0)
                assert result[1].face == 'y-'
                assert result[1].player.player_id == 1
                break
        time.sleep(0.1)
Пример #2
0
def test_events_multi_thread_handler():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5|-1,0,0,0,1'
    events = picraft.events.Events(conn)
    result = []
    lock = threading.Lock()

    @events.on_block_hit(thread=True)
    def handler(event):
        with lock:
            result.append(event)

    events.process()
    # The handler is threaded so there's no guarantee the result has been
    # filled by this point. Hence, we wait up to two seconds for that to
    # happen, with a small wait between acquiring locks to avoid lock
    # starvation in the background threads.
    start = time.time()
    while time.time() - start < 2:
        with lock:
            if len(result) == 2:
                if result[0].pos != Vector(1, 2, 3):
                    result[0], result[1] = result[1], result[0]
                assert result[0].pos == Vector(1, 2, 3)
                assert result[0].face == 'x-'
                assert result[0].player.player_id == 5
                assert result[1].pos == Vector(-1, 0, 0)
                assert result[1].face == 'y-'
                assert result[1].player.player_id == 1
                break
        time.sleep(0.1)
Пример #3
0
def test_events_hit_handler_filter_face_bytes():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5'
    events = picraft.events.Events(conn)
    result = []
    @events.on_block_hit(pos=Vector(1, 2, 3), face=b'y-')
    def handler(event):
        result.append(event)
    events.process()
    assert len(result) == 0
Пример #4
0
def test_events_hit_handler_filter_bad2():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5'
    events = picraft.events.Events(conn)
    result = []
    @events.on_block_hit(pos=Vector(1, 2, 3), face=False)
    def handler(event):
        result.append(event)
    with pytest.raises(TypeError):
        events.process()
Пример #5
0
def test_events_chat_handler_filter_message_bad():
    conn = mock.MagicMock()
    conn.server_version = 'raspberry-juice'
    conn.transact.side_effect = ['', '1,teleport']
    events = picraft.events.Events(conn)
    result = []
    @events.on_chat_post(message=1)
    def handler(event):
        result.append(event)
    with pytest.raises(TypeError):
        events.process()
Пример #6
0
def test_events_pos_handler_filter_bad():
    conn = mock.MagicMock()
    conn.transact.side_effect = ['1.0,1.0,1.0', '1.1,1.0,1.0', '']
    events = picraft.events.Events(conn)
    events.track_players = {1}
    result = []
    @events.on_player_pos(old_pos=1)
    def handler(event):
        result.append(event)
    with pytest.raises(TypeError):
        events.process()
Пример #7
0
def test_events_pos_handler_filter_one():
    conn = mock.MagicMock()
    conn.transact.side_effect = ['1.0,1.0,1.0', '1.1,1.0,1.0', '']
    events = picraft.events.Events(conn)
    events.track_players = {1}
    result = []
    @events.on_player_pos(old_pos=Vector(2, 0, 0))
    def handler(event):
        result.append(event)
    events.process()
    assert len(result) == 0
Пример #8
0
def test_events_idle_decorator():
    conn = mock.MagicMock()
    conn.transact.return_value = ''
    events = picraft.events.Events(conn)
    events.include_idle = True
    result = []
    @events.on_idle()
    def handler(event):
        result.append(1)
    events.process()
    assert result
Пример #9
0
def test_events_hit_handler_filter_bad2():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5'
    events = picraft.events.Events(conn)
    result = []

    @events.on_block_hit(pos=Vector(1, 2, 3), face=False)
    def handler(event):
        result.append(event)

    with pytest.raises(TypeError):
        events.process()
Пример #10
0
def test_events_hit_handler_filter_face_bytes():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5'
    events = picraft.events.Events(conn)
    result = []

    @events.on_block_hit(pos=Vector(1, 2, 3), face=b'y-')
    def handler(event):
        result.append(event)

    events.process()
    assert len(result) == 0
Пример #11
0
def test_events_chat_decorator():
    conn = mock.MagicMock()
    conn.server_version = 'raspberry-juice'
    conn.transact.side_effect = ['', '1,Hello world!']
    events = picraft.events.Events(conn)
    result = []
    @events.on_chat_post()
    def handler(event):
        result.append(event)
    events.process()
    assert len(result) == 1
    assert result[0].message == 'Hello world!'
    assert result[0].player.player_id == 1
Пример #12
0
def test_events_chat_handler_filter_message_bytes():
    conn = mock.MagicMock()
    conn.server_version = 'raspberry-juice'
    conn.transact.side_effect = ['', '1,teleport']
    events = picraft.events.Events(conn)
    result = []
    @events.on_chat_post(message=b'teleport')
    def handler(event):
        result.append(event)
    events.process()
    assert len(result) == 1
    assert result[0].message == 'teleport'
    assert result[0].player.player_id == 1
Пример #13
0
def test_events_hit_handler_filter_many():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5'
    events = picraft.events.Events(conn)
    result = []
    @events.on_block_hit(pos=[Vector(0, 0, 0), Vector(1, 2, 3)], face=['x+', 'x-'])
    def handler(event):
        result.append(event)
    events.process()
    assert len(result) == 1
    assert result[0].pos == Vector(1, 2, 3)
    assert result[0].face == 'x-'
    assert result[0].player.player_id == 5
Пример #14
0
def test_events_chat_handler_filter_message_bad():
    conn = mock.MagicMock()
    conn.server_version = 'raspberry-juice'
    conn.transact.side_effect = ['', '1,teleport']
    events = picraft.events.Events(conn)
    result = []

    @events.on_chat_post(message=1)
    def handler(event):
        result.append(event)

    with pytest.raises(TypeError):
        events.process()
Пример #15
0
def test_events_idle_decorator():
    conn = mock.MagicMock()
    conn.transact.return_value = ''
    events = picraft.events.Events(conn)
    events.include_idle = True
    result = []

    @events.on_idle()
    def handler(event):
        result.append(1)

    events.process()
    assert result
Пример #16
0
def test_events_pos_handler_filter_bad():
    conn = mock.MagicMock()
    conn.transact.side_effect = ['1.0,1.0,1.0', '1.1,1.0,1.0', '']
    events = picraft.events.Events(conn)
    events.track_players = {1}
    result = []

    @events.on_player_pos(old_pos=1)
    def handler(event):
        result.append(event)

    with pytest.raises(TypeError):
        events.process()
Пример #17
0
def test_events_pos_handler_filter_one():
    conn = mock.MagicMock()
    conn.transact.side_effect = ['1.0,1.0,1.0', '1.1,1.0,1.0', '']
    events = picraft.events.Events(conn)
    events.track_players = {1}
    result = []

    @events.on_player_pos(old_pos=Vector(2, 0, 0))
    def handler(event):
        result.append(event)

    events.process()
    assert len(result) == 0
Пример #18
0
def test_events_pos_handler_filter_many():
    conn = mock.MagicMock()
    conn.transact.side_effect = ['1.0,1.0,1.0', '1.1,1.0,1.0', '']
    events = picraft.events.Events(conn)
    events.track_players = {1}
    result = []
    @events.on_player_pos(old_pos=[Vector(0, 0, 0), Vector(1, 0, 0), Vector(1, 1, 1)])
    def handler(event):
        result.append(event)
    events.process()
    assert len(result) == 1
    assert result[0].old_pos == Vector(1.0, 1.0, 1.0)
    assert result[0].new_pos == Vector(1.1, 1.0, 1.0)
    assert result[0].player.player_id == 1
Пример #19
0
def test_events_chat_handler_filter_message_bytes():
    conn = mock.MagicMock()
    conn.server_version = 'raspberry-juice'
    conn.transact.side_effect = ['', '1,teleport']
    events = picraft.events.Events(conn)
    result = []

    @events.on_chat_post(message=b'teleport')
    def handler(event):
        result.append(event)

    events.process()
    assert len(result) == 1
    assert result[0].message == 'teleport'
    assert result[0].player.player_id == 1
Пример #20
0
def test_events_hit_handler_filter_one():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5'
    events = picraft.events.Events(conn)
    result = []

    @events.on_block_hit(pos=Vector(1, 2, 3), face='x-')
    def handler(event):
        result.append(event)

    events.process()
    assert len(result) == 1
    assert result[0].pos == Vector(1, 2, 3)
    assert result[0].face == 'x-'
    assert result[0].player.player_id == 5
Пример #21
0
def test_events_chat_decorator():
    conn = mock.MagicMock()
    conn.server_version = 'raspberry-juice'
    conn.transact.side_effect = ['', '1,Hello world!']
    events = picraft.events.Events(conn)
    result = []

    @events.on_chat_post()
    def handler(event):
        result.append(event)

    events.process()
    assert len(result) == 1
    assert result[0].message == 'Hello world!'
    assert result[0].player.player_id == 1
Пример #22
0
def test_events_multi_thread_single_exec_handler():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5|-1,0,0,0,1'
    events = picraft.events.Events(conn)
    result = []
    @events.on_block_hit(thread=True, multi=False)
    def handler(event):
        result.append(event)
        # This delay should cause the second event to be thrown away
        time.sleep(0.2)
    events.process()
    time.sleep(0.5)
    assert len(result) == 1
    assert result[0].pos == Vector(1, 2, 3)
    assert result[0].face == 'x-'
    assert result[0].player.player_id == 5
Пример #23
0
def test_events_pos_decorator():
    conn = mock.MagicMock()
    conn.transact.side_effect = ['1.0,1.0,1.0', '1.1,1.0,1.0', '']
    events = picraft.events.Events(conn)
    events.track_players = {1}
    result = []

    @events.on_player_pos()
    def handler(event):
        result.append(event)

    events.process()
    assert len(result) == 1
    assert result[0].old_pos == Vector(1.0, 1.0, 1.0)
    assert result[0].new_pos == Vector(1.1, 1.0, 1.0)
    assert result[0].player.player_id == 1
Пример #24
0
def test_events_multi_thread_single_exec_handler():
    conn = mock.MagicMock()
    conn.transact.return_value = '1,2,3,4,5|-1,0,0,0,1'
    events = picraft.events.Events(conn)
    result = []

    @events.on_block_hit(thread=True, multi=False)
    def handler(event):
        result.append(event)
        # This delay should cause the second event to be thrown away
        time.sleep(0.2)

    events.process()
    time.sleep(0.5)
    assert len(result) == 1
    assert result[0].pos == Vector(1, 2, 3)
    assert result[0].face == 'x-'
    assert result[0].player.player_id == 5