view test/test_demo.py @ 6565:2c2dbfc332ba

Try to handle multiple connections better. The session database is a hot spot. When multiple requests (e.g. 20) come in at the same time session database contention can get great. The original code didn't retry session database access when the open failed. This resulted in errors at the client. The second pass delayed 0.01 seconds and retried. It was better but we still had multiple second stalls. I think the first request got in, everybody else backed up and then retried at the same time. Again they stepped on each other. With logging I would see many counters go all the way to low single digits or to -1 indicating falure. This pass uses randomint to generate delays from 0-.125 seconds in 5ms increments. This performs better in testing. I rarely saw a counter less than 13 (2 failed retries). Current logging starts after 6 failures and counts down until success or failure.
author John Rouillard <rouilj@ieee.org>
date Thu, 16 Dec 2021 20:02:00 -0500
parents 5a3a386aa8e7
children 77eb1a41fc06
line wrap: on
line source

import unittest
import os, sys, shutil

from roundup.demo import install_demo, run_demo

import roundup.scripts.roundup_server

# https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
# lightly modified
from contextlib import contextmanager
_py3 = sys.version_info[0] > 2
if _py3:
    from io import StringIO # py3
else:
    from StringIO import StringIO # py2
@contextmanager
def captured_output():
    new_out, new_err = StringIO(), StringIO()
    old_out, old_err = sys.stdout, sys.stderr
    try:
        sys.stdout, sys.stderr = new_out, new_err
        yield sys.stdout, sys.stderr
    finally:
        sys.stdout, sys.stderr = old_out, old_err

class TestDemo(unittest.TestCase):
    def setUp(self):
        self.home = os.path.abspath('_test_demo')

    def tearDown(self):
        try:
            shutil.rmtree(self.home)
        except FileNotFoundError:
            pass
        
    def testDemoClassic(self):
        with captured_output() as (out, err):
            install_demo(self.home, 'anydbm', 'classic')
        output = out.getvalue().strip()
        print(output)

        # dummy up the return of get_server so the serve_forever method
        # raises keyboard interrupt exiting the server so the test exits.
        gs = roundup.scripts.roundup_server.ServerConfig.get_server
        def raise_KeyboardInterrupt():
            raise KeyboardInterrupt

        def test_get_server(self):
            httpd = gs(self)
            httpd.serve_forever = raise_KeyboardInterrupt
            return httpd

        roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server

        # Run under context manager to capture output of startup text.
        with captured_output() as (out, err):
            run_demo(self.home)
        output = out.getvalue().strip()
        print(output)
        # if the server installed and started this will be the
        # last line in the output.
        self.assertIn("Keyboard Interrupt: exiting", output.split('\n'))

    def testDemoMinimal(self):
        with captured_output() as (out, err):
            # use a modified path that resolves when 
            install_demo(self.home, 'sqlite', '../templates/minimal')
        output = out.getvalue().strip()
        print(output)

        # verify that db was set properly by reading config
        with open(self.home + "/config.ini", "r") as f:
            config_lines = f.readlines()

        self.assertIn("backend = sqlite\n", config_lines)

        # dummy up the return of get_server so the serve_forever method
        # raises keyboard interrupt exiting the server so the test exits.
        gs = roundup.scripts.roundup_server.ServerConfig.get_server
        def raise_KeyboardInterrupt():
            raise KeyboardInterrupt

        def test_get_server(self):
            httpd = gs(self)
            httpd.serve_forever = raise_KeyboardInterrupt
            return httpd

        roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server

        # Run under context manager to capture output of startup text.
        with captured_output() as (out, err):
            run_demo(self.home)
        output = out.getvalue().strip()
        print(output)
        # if the server installed and started this will be the
        # last line in the output.
        self.assertIn("Keyboard Interrupt: exiting", output.split('\n'))

    def testDemoJinja(self):
        with captured_output() as (out, err):
            install_demo(self.home, 'anydbm', 'jinja2')
        output = out.getvalue().strip()
        print(output)

        # verify that template was set to jinja2 by reading config
        with open(self.home + "/config.ini", "r") as f:
            config_lines = f.readlines()

        self.assertIn("template_engine = jinja2\n", config_lines)

        # dummy up the return of get_server so the serve_forever method
        # raises keyboard interrupt exiting the server so the test exits.
        gs = roundup.scripts.roundup_server.ServerConfig.get_server
        def raise_KeyboardInterrupt():
            raise KeyboardInterrupt

        def test_get_server(self):
            httpd = gs(self)
            httpd.serve_forever = raise_KeyboardInterrupt
            return httpd

        roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server

        # Run under context manager to capture output of startup text.
        with captured_output() as (out, err):
            run_demo(self.home)
        output = out.getvalue().strip()
        print(output)
        # if the server installed and started this will be the
        # last line in the output.
        self.assertIn("Keyboard Interrupt: exiting", output.split('\n'))

Roundup Issue Tracker: http://roundup-tracker.org/