def test_ma_toll_horizon_termination(vis=False):
    """
    Instead of driving out of road, it may collide with other cars as well! so assertion may fail.
    """
    # test horizon
    env = MultiAgentTollgateEnv(
        {
            "horizon": 100,
            "num_agents": 8,
            "debug_static_world": True,
            "debug_physics_world": True,
            "use_render": vis,
            "debug": True,
            "crash_done": False,
            "log_level": 50,
        }
    )
    try:
        for _ in range(1):  # This function is really easy to break, repeat multiple times!
            _check_spaces_before_reset(env)
            obs, _ = env.reset()
            # env.engine.toggleDebug()
            _check_spaces_after_reset(env, obs)
            assert env.observation_space.contains(obs)
            should_respawn = set()
            special_agents = set(["agent0", "agent7"])
            for step in range(1, 10000):
                act = {k: [0, 0] for k in env.agents.keys()}
                for v_id in act.keys():
                    if v_id in special_agents:
                        act[v_id] = [1, 1]  # Add some randomness
                    else:
                        if v_id in env.agents:
                            env.agents[v_id].set_static(True)
                obs, r, tm, tc, i = _act(env, act)

                # if vis:
                #     env.render(mode="topdown")

                if step == 0 or step == 1:
                    assert not any(tm.values())
                    assert not any(tc.values())

                if should_respawn:
                    for kkk in should_respawn:
                        assert kkk not in obs, "It seems the max_step agents is not respawn!"
                        assert kkk not in r
                        assert kkk not in tm
                        assert kkk not in tc
                        assert kkk not in i
                    should_respawn.clear()

                for kkk, ddd in tm.items():
                    if ddd and kkk in special_agents:
                        assert i[kkk]["out_of_road"] or i[kkk]["crash_vehicle"]

                    if ddd and kkk == "__all__":
                        # print("Current: ", step)
                        continue
                    if ddd and kkk not in special_agents:
                        assert i[kkk]["max_step"]
                        assert not i[kkk]["out_of_road"]
                        assert not i[kkk]["crash"]
                        assert not i[kkk]["crash_vehicle"]
                        should_respawn.add(kkk)

                if tm["__all__"]:
                    obs, _ = env.reset()
                    should_respawn.clear()
                    break
    finally:
        env.close()

    Instead of driving out of road, it may collide with other cars as well! so assertion may fail.
    
metadrive/tests/test_env/test_ma_tollgate.py