hiredscorelabs / cornell

Cornell - record & replay mock server
https://hiredscorelabs.github.io/cornell/
MIT License
141 stars 10 forks source link

Index.yaml file is empty when cornell is stopped by SIGTERM #10

Closed FabienArcellier closed 3 years ago

FabienArcellier commented 3 years ago

I run cornell as a background process in automatic testing to record the context of some test. In that case, the file index.yaml is not dumped properly at the end of the process and is empty. It makes replay not usable without removing the index files. (I am not sure of its real usage)

@contextmanager
def record_cassette(api: str, cassette_path: str, port=9000):
    cornell = local['cornell']
    log = os.path.join(cassette_path, 'cassette.log')
    if os.path.isfile(log):
        os.remove(log)

    with io.open(log, 'a') as fp:
        run = (cornell['-ff', api, '--record', '-fp', '-cd', cassette_path, '-p', port] & BG(stdout=fp, stderr=fp))
        try:
            time.sleep(2)
            yield f'http://localhost:{port}'

        finally:
            run.proc.terminate()

I will open a PR to fix this issue

app.logger.info("Starting Cornell", app_name=app.name, port=port, record=record, record_once=record_once,
                    fixed_path=fixed_path, forward_uri=forward_uri, cassettes_dir=str(cassettes_dir))
atexit.register(on_cornell_exit, app=app)
signal.signal(signal.SIGTERM, lambda: on_cornell_exit(app))
app.run(port=port, threaded=False)
yaelmi3 commented 3 years ago

Thanks @FabienArcellier . Interesting use case, we didn't take into account :)

AvnerCohen commented 3 years ago

Merged.