Skip to content

vladiibine/pyvaldi

Repository files navigation

pyvaldi

Documentation Status Travis-CI Build Status AppVeyor Build Status Coverage Status Code Quality Status Scrutinizer Status
PyPI Package latest release PyPI Package monthly downloads PyPI Wheel Supported versions Supported imlementations

Test helper library, for orchestrating parallel processes in order to make assertions about their state

  • Free software: MIT license

Introduction

This library will help you to test systems that do not have the ideal architecture.

If you've got a DB server, and multiple other nodes that connect to it, changing state concurrently, you know how annoying it can be to reason about such situations.

While your architecture should of course be improved (if possible), this library makes it easier to make assertions about the state of such a system.

I use 3 concepts for this: Starters, Checkpoints and Runners.

Starters are similar to Threads. They will start a certain chain of actions. In other words, they'll get a callable object and call it with some arguments. This callable represents your process under test.

A checkpoint represents a snapshot in the life of one of the tested processes. A checkpoint is considered reached if all actions up to that logical point have been carried out (useful examples: all transactions have been commited OR only started - to test system consistency, all HTTP requests have been sent and responses have been received, or more generally, all methods have been called UP TO that point)

A runner will run your processes (represented by the starters) in parallel. It must also know the order that the checkpoints should be hit in the life of those processes. It will pause the processes, if necessary, to ensure the checkpoints are hit in exactly the order they should. When runners pause, you can make assertions about the state of the system (check the DB or any other relevant stateful system). You can then manually resume the runner to either run to the next checkpoint, skip to a specific checkpoint, or to run until all processes have finished.

Of course, I still have to implement this, but this is pretty much a stable form.

Example

Copy-paste from the tests module. This illustrates how 2 processes, running TwoPhaseMachine()() will do so in an orderly fashion. This simulates the scenario when those processes actually run in this order, and we can interrogate their state.

class TwoPhaseMachine(object):
    def __init__(self):
        self.steps = []

    def first_phase(self):
        self.steps.append(1)

    def second_phase(self):
        self.steps.append(2)

    def __call__(self, *args, **kwargs):
        self.first_phase()
        self.second_phase()


class TwoThreadsTestCase(unittest.TestCase):
    def test_first_thread_finishes_then_second_starts(self):
        first_machine = TwoPhaseMachine()
        second_machine = TwoPhaseMachine()

        first_starter = ProcessStarter(first_machine)
        cp1 = first_starter.add_checkpoint_after(first_machine.second_phase)

        second_starter = ProcessStarter(second_machine)
        cp2 = second_starter.add_checkpoint_before(second_machine.second_phase)
        cp3 = second_starter.add_checkpoint_after(second_machine.second_phase)

        runner = Runner([first_starter, second_starter], [cp1, cp2, cp3])

        self.assertIs(next(runner), cp1)
        self.assertEqual(first_machine.steps, [1, 2])
        self.assertEqual(second_machine.steps, [])
        self.assertIs(next(runner), cp2)
        self.assertEqual(second_machine.steps, [1])
        self.assertIs(next(runner), cp3)
        self.assertEqual(second_machine.steps, [1, 2])

Installation

pip install pyvaldi

Documentation

https://pyvaldi.readthedocs.org/

Development

To run the all tests run:

tox

About

Test helper library, for orchestrating parallel processes in order to make assertions about their state

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published