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)
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
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()
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()
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()
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
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
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
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
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
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
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
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
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