Skip to content

Latest commit

 

History

History
14 lines (8 loc) · 4.14 KB

2021-07-31-sam-surprisingly-nice-refactoring.md

File metadata and controls

14 lines (8 loc) · 4.14 KB

A surprisingly nice weekend of refactoring, cleaning and testing

2021-07-31 / exchange,server,python / Sam

Quick update on development so far this weekend. Tom and I had a brief discussion about user stories and decided to try and cook up a few documents to keep track of our terminology (which way around are bid and ask again?), our assumptions (there is only one exchange, all prices are in the same currency and other such simplifications we have decided upon) and the interchange formats our competing applications will need to speak in order to be swappable. We'll put these on Github somewhere, eventually.

We decided that the user-related functionality that I'd developed in the Python version of the exchange should probably live outside the Exchange itself and in another system. We've decided to call this new service the Broker. In the STEX ecosystem, we're going to assume there is just one broker (for now), but we certainly envisage running multiple competing broker services with their own backstory and perks for clients to play with when the Exchange is actually working.

My goals for the weekend are to tidy up my services a little, and write some tests. I spent around an hour today abstracting out the user functionality from my services/exchange.py to a new services/broker.py module. I've designed it to be quite lightweight as we aren't actively developing the broker systems while we get the exchange itself working, but it works just enough that the functionality I built previously (to track user account balances and holdings) still works. This refactoring was surprisingly straightforward, because the storage concerns have been abstracted to the Repositories and can only be accessed through a Unit of Work (UoW). All I needed to do was move the functions from the exchange to the new broker module, and ensure the new Broker class had a reference to the right UoW. The application is ignorant to persistence concerns, which means we can quite easily move the responsibility for data structures around! It's been very nice to see the "hard" work of writing more code actually paying off.

For the past couple of hours I've been writing tests. I've always found writing tests hard to start, but once I get into a groove I almost enjoy it, until I come to more complex cases that require significant fuckery to build the right state, and run assert on deep structures. However, I've had none of such nonsense today, the code (so far) has been straightforward to test. My service functions are smaller than what I would usually write (as I've thought harder about responsibilities as a result of abstracting storage), making it easier to unit test their specific responsibilities. The Uow and Repository make inserting mock data more straightforward as we can instantiate the in-memory Repository and check that data is returned through the service functions in the format expected. My services/orderbook.py is now at 99% coverage, covered save for a one line edge case that I'm too lazy to return to for now.

Despite the coverage, there is a bit more testing to do. It's one thing showing that the services work as expected, but I need to write some integration tests to demonstrate the different UoW flavours (in-memory and SQLAlchemy) are going to do their job correctly. This is important for the case of the orderbook, as its up to the Order repository to return the books in the right sorted order for the matching algorithm to do its job correctly. Still, rather than writing a whole host of matcher tests under the different UoW configurations, I already have a set of unit tests to show the matcher works, so I think I just need to show that the Order repository returns orders in the right order. If our matcher works under the invariant condition that orders are sorted appropriately, we can just test that the SQLAlchemy UoW will not violate this assumption.

Most importantly for progress, my test suite proves my implementation of the order matching algorithm is working (to our specification of the rules at least) under a variety of different conditions, so the Sam and Tom Stock Exchange (2) Server has taken a big step forward towards launch... Or the Python version at least.