Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building testable APIs using functions & meshes

Building testable APIs using functions & meshes

In this live coding talk you will learn about functional techniques that encourage separation of concerns. I will build a stateful HTTP API from highly isolated components that are easier to test than entangled spaghetti-like codebases. I will show you how tests can help us scaffold the architecture and how should we approach testing in the era of microservices. In the session I will use immutability, type parameters and function parameters as tools to implement a very practical example: Pac-Man game web server. I will code in Scala using Akka HTTP and deploy to Kubernetes with Istio service mesh.

Michał Płachta

May 10, 2018
Tweet

More Decks by Michał Płachta

Other Decks in Programming

Transcript

  1. @miciek @miciek Michał Płachta www.michalplachta.com www.michalplachta.com Building testable APIs using

    functions & meshes @miciek
  2. @miciek Reinventing the Test Pyramid ? ? ?

  3. @miciek New Testing Pyramid: Part I ? ? Unit?

  4. @miciek Unit Tests

  5. @miciek What we really want... Fast! Stable! Quick to read/write!

    Documentation
  6. @miciek Mocking and Stubbing... Mocking interfaces with 3, 4, 5+

    functions... Mocking interfaces with 0 or 1 functions...
  7. @miciek Part I ? ? Fast, Stable, Real

  8. @miciek Pac-Man Game Server ? Engine ?

  9. @miciek Pac-Man Engine Domain

  10. @miciek Pac-Man Engine Domain

  11. @miciek Pac-Man Engine Domain

  12. @miciek Pac-Man Engine Domain

  13. @miciek Immutable Pac-Man Engine GameState movePacMan GameState current new GameState

    changeDir GameState current new
  14. @miciek TDDing Pac-Man Engine

  15. @miciek TDDing Pac-Man Engine

  16. @miciek TDDing Pac-Man Engine Live coding

  17. @miciek TDDing Pac-Man Engine

  18. @miciek TDDing Pac-Man Engine

  19. @miciek TDDing Pac-Man Engine

  20. @miciek TDDing Pac-Man Engine

  21. @miciek Pure & Immutable

  22. @miciek TDDing Pac-Man Engine

  23. @miciek New Testing Pyramid: Part II ? Service? Integration? Fast,

    Stable, Real
  24. @miciek Pac-Man Game Server Engine HTTP ?

  25. @miciek HTTP layer requirements POST /games { “gridName”: “msPacManLevel1” }

    200 OK { “gameId”: 1 } 200 OK { "pacMan": { "position": { "x": 2, "y": 1 }, "direction": "east" } } GET /games/1
  26. @miciek What we always want... Fast! Stable! Quick to read/write!

    Documentation
  27. @miciek Testing HTTP layer Live coding

  28. @miciek Fast, Stable, Real, Documenting

  29. @miciek Separation of Concerns I only know what I NEED

    to know...
  30. @miciek Pac-Man Game Server State Engine HTTP

  31. @miciek Requirements State holds multiple games at once updates (ticks)

    all the games every 200ms
  32. @miciek Holding multiple games at once A concurrent hash-trie or

    TrieMap is a concurrent thread-safe lock-free implementation of a hash array mapped trie. - Scala documentation http://lampwww.epfl.ch/~prokopec/ctries-snapshot.pdf
  33. @miciek Holding multiple games at once

  34. @miciek Ticking all the games every 200ms https://monix.io/docs/2x/execution/scheduler.html#schedule-with-a-delay

  35. @miciek Quick integration tests Live coding

  36. @miciek New Testing Pyramid: Part II ? Service? Integration? Fast,

    Stable, Real
  37. @miciek New Testing Pyramid: Part II ? Fast, Stable, Real

  38. @miciek Only one service so far... State Engine HTTP Backend

    service
  39. @miciek Communicating Services Backend service Frontend service Service Discovery Circuit

    Breaking Security Scalability & Routing Telemetry
  40. @miciek Testing on Production Backend service PROD Frontend service PROD

    Service Discovery Circuit Breaking Security Scalability & Routing Telemetry App Container App Container
  41. @miciek Testing on Production… in Isolation? Backend service PROD Frontend

    service PROD Sidecar Sidecar Service Discovery Circuit Breaking Security Scalability & Routing Telemetry App Container App Container
  42. @miciek Service Mesh Backend service PROD Frontend service PROD Sidecar

    Sidecar Another Service PROD Sidecar Service Mesh Control Plane
  43. @miciek Testing on Production… in Isolation Backend service PROD Frontend

    service PROD Sidecar Sidecar Backend service v2 (test) Sidecar Routing only test traffic to v2
  44. @miciek Isolated tests on Production Live coding

  45. @miciek New Testing Pyramid Real Fast, Stable, Real No HTTP

    Servers Function parameters Abstracting over data Routing test traffic Isolated tests on PROD
  46. @miciek Summary functional programming helps with separation of concerns service

    meshes may help with isolated testing ...but they are new & unproven
  47. @miciek Next steps ➔ Play around with https://www.github.com/miciek/pacman-multiplayer-fp project ➔

    Play with Kubernetes and Istio using Katakoda https://www.katacoda.com/courses/istio/deploy-istio-on-kubernetes
  48. @miciek @miciek www.michalplachta.com

  49. @miciek @miciek Michał Płachta www.michalplachta.com www.michalplachta.com Building testable APIs using

    functions & meshes @miciek github.com/miciek/pacman-multiplayer-fp