Skip to main content

Simulating poor network connections.

Project description

PyPI PyPI - Python Versions PyPI - Implementation Documentation PyPI - License Pipeline Status Coverage

Poorconn is a Python package that simulates poor network conditions. It is suitable for testing purposes, for both Python and non-Python projects.

It is capable of simulating the following poor network conditions:

  • Throttled network connections. (delay_before_sending, delay_before_sending_upon_acceptance)

  • Servers that cut off connections immediately upon accepting them. (close_upon_accepting)

  • Connections that are initially slow, but become normal subsequently. (delay_before_sending_once, delay_before_sending_upon_acceptance_once)

Quickstart

Install this package via

$ pip install 'poorconn[full]'  # or "pip install poorconn" if you don't need pytest support

The following example starts a local HTTP server at port 8000 that always closes connections upon accepting them:

from http.server import HTTPServer, SimpleHTTPRequestHandler
from poorconn import close_upon_accepting, make_socket_patchable

# Start a local HTTP server that always closes connections upon established
with HTTPServer(("localhost", 8000), SimpleHTTPRequestHandler) as httpd:
    httpd.socket = make_socket_patchable(httpd.socket)
    close_upon_accepting(httpd.socket)
    httpd.serve_forever()

After running the code above, connections from a client would establish but fail to communicate subsequently:

$ wget -t 1 http://127.0.0.1:8000
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... No data received.
Giving up.

If you use pytest, you can also take advantage of poorconn fixtures in poorconn.pytest_plugin. The following example gets you started with testing against a slow HTTP server:

pytest_plugins = ('poorconn',)

from pathlib import Path
import time
import requests
import pytest

@pytest.mark.poorconn_http_server_config(t=2, length=1024)
def test_slow_http_server(poorconn_http_server, tmp_path):
    "Test GETing from a slow local http server that delays 2 seconds to send every 1024 bytes."
    (tmp_path / 'index.txt').write_bytes(b'h' * 1024)
    starting_time = time.time()
    # Replace the following line with the program you want to test
    content = requests.get(f'{poorconn_http_server.url}/index.txt').content
    ending_time = time.time()
    assert ending_time - starting_time > 2

Bug Reports and Feature Requests

Please open a ticket on the GitLab Issue Tracker.

Contributing

Contributions are welcome! To get started, check out CONTRIBUTING.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

poorconn-0.1.tar.gz (38.5 kB view details)

Uploaded Source

Built Distribution

poorconn-0.1-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

Details for the file poorconn-0.1.tar.gz.

File metadata

  • Download URL: poorconn-0.1.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.3

File hashes

Hashes for poorconn-0.1.tar.gz
Algorithm Hash digest
SHA256 5eecd9fdadc29509b5e2185457b9533426625449c9ceadd4fc814b8918c7bb48
MD5 0d310f6c90821b068cdd4c5ddb0fe9a3
BLAKE2b-256 6f63f4817565c95a4bc9b51bbcdf96190ae57e46e6af4e9b901df9da843d95b3

See more details on using hashes here.

File details

Details for the file poorconn-0.1-py3-none-any.whl.

File metadata

  • Download URL: poorconn-0.1-py3-none-any.whl
  • Upload date:
  • Size: 44.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.3

File hashes

Hashes for poorconn-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40443f9da25d201be99b61cd77bee2162873e21d695eb2d631cd6eba7ae6cb7f
MD5 09d4af4d8e473b32e8f410d31bcb86db
BLAKE2b-256 0b8c6459ef59d0365f35de6c1e0578af6388d3efbbc9daf8889cd55952687be7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page