Skip to content

Commit

Permalink
feat: add backend option to Server.serve
Browse files Browse the repository at this point in the history
test: run tests for all three backends in Travis
  • Loading branch information
standy66 committed Feb 3, 2019
1 parent b1df796 commit 5f47f8e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
14 changes: 10 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ stages:
- name: deploy
if: tag IS present AND type != pull_request
env:
- PYTHON_IMAGE="python:3.6"
- PYTHON_IMAGE="python:3.7"
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0"
- PYTHON_IMAGE="python:3.6" PURERPC_BACKEND="asyncio"
- PYTHON_IMAGE="python:3.6" PURERPC_BACKEND="curio"
- PYTHON_IMAGE="python:3.6" PURERPC_BACKEND="trio"
- PYTHON_IMAGE="python:3.7" PURERPC_BACKEND="asyncio"
- PYTHON_IMAGE="python:3.7" PURERPC_BACKEND="curio"
- PYTHON_IMAGE="python:3.7" PURERPC_BACKEND="trio"
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0" PURERPC_BACKEND="asyncio"
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0" PURERPC_BACKEND="curio"
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0" PURERPC_BACKEND="trio"

script:
- ./ci/run_tests_in_docker.sh $PYTHON_IMAGE
- ./ci/run_tests_in_docker.sh $PYTHON_IMAGE $PURERPC_BACKEND

jobs:
include:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ FROM ${BASE_IMAGE}
COPY . /purerpc
WORKDIR /purerpc
RUN pip install .
RUN pip install curio trio # Optional, for tests
4 changes: 3 additions & 1 deletion ci/run_tests_in_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
set -e

BASE_IMAGE="$1"
PURERPC_BACKEND="$2"
BUILD_TAG=${BASE_IMAGE//:/-}
BUILD_TAG=${BUILD_TAG//\//-}

docker build --build-arg BASE_IMAGE=${BASE_IMAGE} -t "standy/purerpc:${BUILD_TAG}" .
docker run -it "standy/purerpc:$BUILD_TAG" bash -c 'python setup.py test'
echo "Runnig tests with $PURERPC_BACKEND backend"
docker run -it -e PURERPC_BACKEND=${PURERPC_BACKEND} "standy/purerpc:$BUILD_TAG" bash -c 'python setup.py test'
10 changes: 5 additions & 5 deletions src/purerpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ async def _run_async_server(self, raw_socket):
async for socket in tcp_server.accept_connections():
await task_group.spawn(ConnectionHandler(self), socket)

def _target_fn(self):
def _target_fn(self, backend):
socket = self._create_socket_and_listen()
anyio.run(self._run_async_server, socket)
anyio.run(self._run_async_server, socket, backend=backend)

def serve(self):
def serve(self, backend="asyncio"):
if self.num_processes == 1:
self._target_fn()
self._target_fn(backend)
else:
# this is simple SO_REUSEPORT load balancing on Linux
processes = []
for i in range(self.num_processes):
process = Process(target=self._target_fn)
process = Process(target=self._target_fn, args=(backend,))
process.start()
processes.append(process)
for process in processes:
Expand Down

0 comments on commit 5f47f8e

Please sign in to comment.