Couldn't connect to local service while testing

Hi everyone!

I’m having some problems while trying to test my python service at CircleCI. Here’s the issue:

Once I launch the server (localhost), I wait some seconds for it to get ready (that’s a bit botched, I know :rofl: ). However, when my tests run, they fail as trying to connect to localhost:5000 where my service is supposed to be running. I tried the same on travis and got a successful result. Here are my codes:

Service code:

from flask import Flask, render_template, redirect, url_for, make_response
from flask_socketio import SocketIO
from data import Data
import json

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.debug = True
socketio = SocketIO(app)

recv = Data()


@socketio.on('New Metadata')
def handle_new_metadata_reception(data):
    recv.received(json.dumps(data))
    recv.show()


@app.route("/")
def home():
    return redirect(url_for('lastReceived'))


@app.route("/status")
def status():
    status = {"status": "OK"}
    headers = {"Content-Type": "json"}
    return make_response(status, 200, headers)


@app.route("/last_received")
def lastReceived():
    lastRec = recv.get_data()
    headers = {"Content-Type": "json"}
    return make_response(lastRec, 200, headers)


if __name__ == '__main__':
    socketio.run(app)

config file

version: 2		#Versión (2, 2.0 o 2.1)
jobs:         #Comienza la definición del 'trabajo' a ejecutar
  build:      #Es necesario indicar en caso de no usar workflws o flujos
    working_directory: ~/IoT_AsyncService
    docker:   #Pasamos a configurar nuestro contenedor
      - image: circleci/python:3.6.8    #Imagen de python
    steps:             #Conjunto de pasos a realizar
      - checkout       #Pasar el código al directorio de trabajo
      - restore_cache:    #Rescatamos paquetes cacheados
          key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
      - run:
          command: |     #Comando a ejecutar vía shell
            pip3 install --user -r requirements.txt
      - run:
          command: |     #Comando a ejecutar vía shell
            python3 src/AsyncService.py &
      - run:
          command: |     #Comando a ejecutar vía shell
            sleep 5s
      - save_cache:    #Cacheamos los paquetes para no reinstalarlos
          key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
          paths:
            - ".circleci/cache"
      - run:
          command: |     #Comando a ejecutar vía shell
            python3 -m pytest tests/test_data.py
      - store_test_results:     #Guardar resultado tras la ejecución
          path: "test-results"

service running

#!/bin/bash -eo pipefail
python3 src/AsyncService.py
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
 * Serving Flask app "AsyncService" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
 * Debugger is active!
 * Debugger PIN: 191-450-146

Tests failing while trying to connect to localhost:5000

I’ve been looking for similar issues, but got nothing. I’ve already read the documentation where it says by default services are running on localhost, which misleads me.

Many thanks in advance!

This should work, you may have a race condition or something.

I think the easiest way to debug this would be to run these steps manually over SSH and confirm that everything looks ok. https://circleci.com/docs/2.0/ssh-access-jobs/#debugging-permission-denied-publickey