예제 #1
0
def test_simulation_no_duplicate_measurement_timestamps(
        gridappsd_client: GridAPPSD):
    num_measurements = 0
    timestamps = set()

    def measurement(sim, timestamp, measurement):
        nonlocal num_measurements
        num_measurements += 1
        assert timestamp not in timestamps
        timestamps.add(timestamp)

    gapps = gridappsd_client
    sim = Simulation(gapps, base_config())
    sim.add_onmeasurement_callback(measurement)
    sim.start_simulation()
    sim.run_loop()

    # did we get a measurement?
    assert num_measurements > 0

    # if empty then we know the simulation did not work.
    assert timestamps
    # from pprint import pprint
    # pprint(config['gridappsd'])
    with run_containers(config, stop_after=True) as containers:
        # Watches the log on the container for the MYSQL data.
        containers.wait_for_log_pattern("gridappsd", "MYSQL")

        gappsd = GridAPPSD()
        gappsd.connect()
        assert gappsd.connected

        time.sleep(10)
        sim = Simulation(gappsd, run_config=run_config)

        sim.add_onstart_callback(onstart)
        sim.add_oncomplete_callback(onfinish)
        sim.add_ontimestep_callback(ontimestep)
        sim.add_onmesurement_callback(onmeasurement)

        sim.start_simulation()
        sim.pause()
        gappsd.subscribe(t.simulation_output_topic(sim.simulation_id), onsimulationoutput)
        gappsd.subscribe(t.application_output_topic(GRIDAPPSD_SERVICE_ID, sim.simulation_id), onsensoroutput)
        time.sleep(10)
        sim.resume()
        sim.run_loop()
        print("Shutting down")

    fh_sim_measurement.close()
    fh_sensor_measurement.close()
    fncs_measurements.close()