#!/bin/bash

set -e

function cleanup {
    kill $(cat $AUTOPKGTEST_TMP/memcached.pid)
}

trap cleanup EXIT

memcached -u nobody -d -p 22122 -P $AUTOPKGTEST_TMP/memcached.pid

for p in $(py3versions -s); do
    echo "Testing with $p..."

    $p debian/tests/testapp.py &
    FLASK_PID=$!
    sleep 5

    # The /ping endpoint is not rate limited
    curl -v http://127.0.0.1:5000/ping 2>&1 | grep '200 OK'
    curl -v http://127.0.0.1:5000/ping 2>&1 | grep '200 OK'
    curl -v http://127.0.0.1:5000/ping 2>&1 | grep '200 OK'

    # The / endpoint is
    curl -v http://127.0.0.1:5000/ 2>&1 | grep '200 OK'
    curl -v http://127.0.0.1:5000/ 2>&1 | grep '429 TOO MANY REQUESTS'
    sleep 2
    curl -v http://127.0.0.1:5000/ 2>&1 | grep '200 OK'

    kill $FLASK_PID
done
