Skip to content

Commit f9e90f3

Browse files
committed
Wrote a bash script to test the example
This is not yet run in CI, but should check the example.
1 parent dd6e92c commit f9e90f3

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

docs/source/quickstart/quickstart.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
Quick start
22
===========
33

4-
You can install `labthings-fastapi` using `pip`. Create a virtual environment for you project, then install labthings with:
4+
You can install `labthings-fastapi` using `pip`. We recommend you create a virtual environment, for example:
55

6-
.. code-block:: bash
76

8-
pip install labthings-fastapi[server]
7+
.. literalinclude:: quickstart_example.sh
8+
:language: bash
9+
:start-after: BEGIN venv
10+
:end-before: END venv
11+
12+
then install labthings with:
13+
14+
.. literalinclude:: quickstart_example.sh
15+
:language: bash
16+
:start-after: BEGIN install
17+
:end-before: END install
918

1019
To define a simple example ``Thing``, paste the following into a python file, ``counter.py``:
1120

@@ -14,9 +23,11 @@ To define a simple example ``Thing``, paste the following into a python file, ``
1423

1524
``counter.py`` defines the ``TestThing`` class, and then runs a LabThings server in its ``__name__ == "__main__"`` block. This means we should be able to run the server with:
1625

17-
.. code-block:: bash
1826

19-
python counter.py
27+
.. literalinclude:: quickstart_example.sh
28+
:language: bash
29+
:start-after: BEGIN serve
30+
:end-before: END serve
2031

2132
Visiting http://localhost:5000/counter/ will show the thing description, and you can interact with the actions and properties using the Swagger UI at http://localhost:5000/docs/.
2233

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
echo "Setting up environemnt"
2+
# BEGIN venv
3+
python -m venv .venv --prompt labthings
4+
source .venv/bin/activate # or .venv/Scripts/activate on Windows
5+
# END venv
6+
echo "Installing labthings-fastapi"
7+
# BEGIN install
8+
pip install labthings-fastapi[server]
9+
# END install
10+
echo "running example"
11+
# BEGIN serve
12+
python counter.py
13+
# END serve
14+
echo $! > example_server.pid
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
set -e
2+
3+
# Run the quickstart code in the background
4+
bash ./quickstart_example.sh 2>&1 &
5+
quickstart_example_pid=$!
6+
echo "Spawned example with PID $quickstart_example_pid"
7+
8+
# Wait for it to respond
9+
# Loop until the command is successful or the maximum number of attempts is reached
10+
ret=7
11+
attempt_num=0
12+
while [[ $ret == 7 ]] && [ $attempt_num -le 50 ]; do
13+
# Execute the command
14+
ret=0
15+
curl -sf -m 10 http://localhost:5000/counter/counter || ret=$?
16+
if [[ $ret == 7 ]]; then
17+
echo "Curl didn't connect on attempt $attempt_num"
18+
19+
# Check the example process hasn't died
20+
ps $quickstart_example_pid > /dev/null
21+
if [[ $? != 0 ]]; then
22+
echo "Child process (the server example) died without responding."
23+
exit -1
24+
fi
25+
26+
attempt_num=$(( attempt_num + 1 ))
27+
sleep 1
28+
fi
29+
done
30+
echo "Final return value $ret on attempt $attempt_num"
31+
32+
# Check the Python client code
33+
echo "Running Python client code"
34+
(. .venv/bin/activate && python counter_client.py)
35+
36+
37+
# Get the spawned server's PID
38+
children=$(ps -o pid= --ppid "$quickstart_example_pid")
39+
kill $children
40+
echo "Killed spawned processes: $children"
41+
42+
wait
43+
44+
if [[ $ret == 0 ]]; then
45+
echo "Success"
46+
exit 0
47+
else
48+
echo "Curl returned $ret, likely something went wrong."
49+
exit -1
50+
fi

0 commit comments

Comments
 (0)