Path: blob/main/tests/complex/traci/connection/clientCloses/runner.py
169689 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @date 2010-03-211718from __future__ import absolute_import19from __future__ import print_function2021import os22import subprocess23import sys2425if "SUMO_HOME" in os.environ:26sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))27import sumolib # noqa28import traci # noqa2930PORT = sumolib.miscutils.getFreeSocketPort()31DELTA_T = 10003233sumoBinary = sumolib.checkBinary(sys.argv[1])34if sys.argv[1] == "sumo":35addOption = []36else:37addOption = ["-S", "-Q"]383940def runSingle(traciEndTime, sumoEndTime=None):41step = 042if sumoEndTime is None:43opt = addOption44else:45opt = addOption + ["--end", str(sumoEndTime)]46sumoProcess = subprocess.Popen([sumoBinary, "-c", "sumo.sumocfg", "--remote-port", str(PORT)] + opt,47stdout=sys.stdout)48traci.init(PORT)49while not step > traciEndTime:50traci.simulationStep()51step += 152print("Print ended at step", traci.simulation.getTime())53traci.close()54sumoProcess.wait()55sys.stdout.flush()565758print("=========== long route ===========")59fdo = open("input_routes.rou.xml", "w")60print('<routes>', file=fdo)61print(62' <route id="horizontal" edges="2fi 2si 1o 1fi 1si 3o 3fi 3si 4o 4fi 4si"/>', file=fdo)63print(' <vehicle id="horiz" route="horizontal" depart="0"/>', file=fdo)64print('</routes>', file=fdo)65fdo.close()66print("----------- SUMO end time is smaller than TraCI's -----------")67sys.stdout.flush()68runSingle(99, 50)69print("----------- SUMO end time is not given -----------")70sys.stdout.flush()71runSingle(99)727374print("=========== empty routes in SUMO ===========")75fdo = open("input_routes.rou.xml", "w")76print('<routes>', file=fdo)77print('</routes>', file=fdo)78fdo.close()79print("----------- SUMO end time is smaller than TraCI's -----------")80sys.stdout.flush()81runSingle(99, 50)82print("----------- SUMO end time is not given -----------")83sys.stdout.flush()84runSingle(99)858687print("=========== vehicle leaves before TraCI ends ===========")88fdo = open("input_routes.rou.xml", "w")89print('<routes>', file=fdo)90print(' <route id="horizontal" edges="2fi 2si"/>', file=fdo)91print(' <vehicle id="horiz" route="horizontal" depart="0"/>', file=fdo)92print('</routes>', file=fdo)93fdo.close()94print("----------- SUMO end time is smaller than TraCI's -----------")95sys.stdout.flush()96runSingle(99, 50)97print("----------- SUMO end time is not given -----------")98sys.stdout.flush()99runSingle(99)100101102