Skip to content

Commit

Permalink
Merge 19e7cc2 into 9490736
Browse files Browse the repository at this point in the history
  • Loading branch information
etataurov committed Nov 17, 2016
2 parents 9490736 + 19e7cc2 commit 33515e4
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile*
*.pyc
build
asyncio_apns.egg-info
output
32 changes: 22 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
sudo: required

services:
- docker

env:
- DOCKER_PYTHON="3.5"

language: python
python:
- "3.4"
- "3.5"

before_install:
- eval "$(gimme 1.7)"

install:
- "pip install -r requirements-test.txt"
- "pip install python-coveralls"
- "pip install flake8"
- "pip install -e ."
- pushd tests_mocked_server && CGO_ENABLED=0 go build -v go-apns-server.go && popd
- sed "s/{{python_version}}/$DOCKER_PYTHON/" Dockerfile.template > Dockerfile
- docker build -t etataurov/asyncio_apns .
- pip install python-coveralls
- mkdir -p output

script:
- "flake8 asyncio_apns"
- "py.test -v --cov=asyncio_apns"
- docker run --rm etataurov/asyncio_apns flake8 asyncio_apns
- docker run --rm -v $(pwd)/output:/app/output etataurov/asyncio_apns /bin/sh -c "py.test tests tests_mocked_server -v --cov=asyncio_apns && cp .coverage output/"

after_success:
- "coveralls"
- docker run --rm -e "TRAVIS_JOB_ID=$TRAVIS_JOB_ID" -e "TRAVIS_BRANCH=$TRAVIS_BRANCH" -v $(pwd)/output:/app/output etataurov/asyncio_apns /bin/sh -c "coveralls -d output/.coverage"
8 changes: 8 additions & 0 deletions Dockerfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:{{python_version}}-alpine
RUN apk add --no-cache git
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
COPY . app
WORKDIR app
RUN pip install -r requirements-test.txt && pip install python-coveralls && pip install flake8
RUN pip install -e .
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from distutils.core import setup
from setuptools import setup

install_requires = ["h2>=2.2.0"]
if sys.version_info < (3, 5):
Expand Down
19 changes: 19 additions & 0 deletions tests_mocked_server/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIC/zCCAeegAwIBAgIQTytcpl1uBwcQAkJ0KDJnwjANBgkqhkiG9w0BAQsFADAS
MRAwDgYDVQQKEwdBY21lIENvMB4XDTE2MDcxOTEwMjcxMloXDTE3MDcxOTEwMjcx
MlowEjEQMA4GA1UEChMHQWNtZSBDbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
AQoCggEBAPpI2mMcKh0kuc92RELRUZxHCOBHiVNIQqq135bUubFfkV6Wf+rwjgAD
gk7ihKmrwDe1JeHFu4AnlJw6vvoAz25MwLLfn0tf1oE5BCKNRmeiiUjt+SSR512c
JW7XgebbTGxW9pVDz8Un3814Z1mzyegPtRtwY3px4HdKnqWPo1F5/WZKEWlKrnMk
vnYa5yTUh3V39q6wf0ZWVth5iKMQeZHtXw+FiSPIs2qAOvfpNoZDAdBEqtMDyjdl
4DGkPmSO1oiUUKreOgIKl+R/Bqg93VJP+GPn4sZc7FEWctdlZdILVEwNIbMgG/uY
ti1b4vj0gvOo+gHddcji0wVJmnvgDkECAwEAAaNRME8wDgYDVR0PAQH/BAQDAgWg
MBMGA1UdJQQMMAoGCCsGAQUFBwMBMAwGA1UdEwEB/wQCMAAwGgYDVR0RBBMwEYIP
Y29tcGFuZXJvcy5jbHViMA0GCSqGSIb3DQEBCwUAA4IBAQCUwwAtfLRB2G1aLSHZ
t/dr279rRo0KKWH8JTpqpSl0scPyVPyWNuTORsDozM29b32E4o9XxsfkKhhALGv2
0KVZVkxUtJIOLaM7ogQIci1ToQq18ap4OPjMr8iB+kZA4uZp8iYPuanv6AUjRjsl
eRE5qieqvP0kP6ixGCYtZ6hGri/vHwVuDMIlsHFkOr5Gf+Xu3LDJuLL8lfN4Yff3
3+WI+mjrzhNrirU8q5czyjLMLaCkTq7+OUoRjG4w+SC5HrPD9eexVa4L6I+25VBh
HhxYS1sIyB4PtOnfSedBMjsYCH5O+RFQSF+WZIJv3oQD0pjH/1GYUe7Hz8Y9CcBE
zMLi
-----END CERTIFICATE-----
59 changes: 59 additions & 0 deletions tests_mocked_server/go-apns-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2016, CleverTap
// All rights reserved.
// Licensed under the New 3-Clause BSD License
//
// A mock server for the HTTP2 Apple push gateway

package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
)

func main() {
serverPort := flag.Int("port", 8443, "The port for the mock server to listen to")
serverCert := flag.String("cert", "cert.pem", "The path to the certificate")
serverKey := flag.String("key", "key.pem", "The path to the certificate key")

flag.Parse()

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Error:", err)
}
n := rand.Int31n(10)

statusCode := 200

var body string

if n == 7 {
statusCode = 400
body = "{\"reason\": \"BadDeviceToken\"}"
} else if n > 7 {
statusCode = 410
body = "{\"reason\": \"Unregistered\"}"
}

w.WriteHeader(statusCode)

if statusCode != 200 {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, body)
}
})

log.Println("Using certificate", *serverCert, "with key", *serverKey)
log.Println("Starting server on port", *serverPort)
log.Println("Hit ctrl + c to stop...")

port := ":" + strconv.Itoa(*serverPort)
log.Fatal(http.ListenAndServeTLS(port, *serverCert, *serverKey, nil))
}
27 changes: 27 additions & 0 deletions tests_mocked_server/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA+kjaYxwqHSS5z3ZEQtFRnEcI4EeJU0hCqrXfltS5sV+RXpZ/
6vCOAAOCTuKEqavAN7Ul4cW7gCeUnDq++gDPbkzAst+fS1/WgTkE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ozMDwlIQaNbrGlt/EMm6BlxQE+0/3+5rP7bMm1MbsdJ1JQ4FBj47YlMptZ
M3GsGkfIHBlyDoFVCopglTcCgYEAlr4FHj2DEKzMWytFc/8oSOFlBp7Y8x+NrB2t
Pr/92aj50f6fJW5cYvurMB0g3aNguYY6/SRto/Yll3lDGfT8dcPBYbdnr7jCJfcO
b3nYewKCav+sBMTN6+nnYN7HQOSHH4fosTT+crmx11GsIy6BacW8GrRe5pTtMD4U
HIcZhTsCgYEAwbbSOm6hhkxGIV8cz5IdYw4xxj6Fw2kZc178dhVfeWX4bOHcRH6H
yS//fD94lnAgV3punW1BibcK3nAxMBN2sLIWIacor41USJGp2TWe2cREqOD+3GlQ
170PWseGrQiDm/EfkjEfcJTB6PePe9y0QMvarnzafHNTjlQAGCc5zeo=
-----END RSA PRIVATE KEY-----
41 changes: 41 additions & 0 deletions tests_mocked_server/test_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asyncio
import os
import subprocess
import time

import pytest
from asyncio_apns import APNsConnection, APNsError

CWD = os.path.dirname(os.path.realpath(__file__))


@pytest.yield_fixture(scope="module")
def mock_server(request):
server = subprocess.Popen("./go-apns-server", shell=True, cwd=CWD)
time.sleep(0.5)
yield server
server.terminate()


@pytest.mark.skipif(not os.path.exists(os.path.join(CWD, 'go-apns-server')),
reason="No binary for test server")
@pytest.mark.asyncio
def test_batch_send_messages(event_loop, mock_server):
connection = APNsConnection(os.path.join(CWD, "cert.pem"), os.path.join(CWD, "key.pem"),
loop=event_loop, server_addr="127.0.0.1", server_port=8443)
yield from connection.connect()

@asyncio.coroutine
def send_message():
token = "fsd76fh23jhd6s2j2h"
message = "Hello my world"
try:
yield from connection.send_message(message, token=token)
except APNsError as exc:
assert exc.status in ("BadDeviceToken", "Unregistered")

tasks = []
for _ in range(1000):
tasks.append(asyncio.async(send_message(), loop=event_loop))
yield from asyncio.wait(tasks, loop=event_loop)
connection.disconnect()

0 comments on commit 33515e4

Please sign in to comment.