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

    View Slide

  2. @miciek
    Reinventing the Test Pyramid
    ?
    ?
    ?

    View Slide

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

    View Slide

  4. @miciek
    Unit Tests

    View Slide

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

    View Slide

  6. @miciek
    Mocking and Stubbing...
    Mocking interfaces
    with 3, 4, 5+ functions...
    Mocking interfaces
    with 0 or 1 functions...

    View Slide

  7. @miciek
    Part I
    ?
    ?
    Fast, Stable, Real

    View Slide

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

    View Slide

  9. @miciek
    Pac-Man Engine Domain

    View Slide

  10. @miciek
    Pac-Man Engine Domain

    View Slide

  11. @miciek
    Pac-Man Engine Domain

    View Slide

  12. @miciek
    Pac-Man Engine Domain

    View Slide

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

    View Slide

  14. @miciek
    TDDing Pac-Man Engine

    View Slide

  15. @miciek
    TDDing Pac-Man Engine

    View Slide

  16. @miciek
    TDDing
    Pac-Man Engine
    Live coding

    View Slide

  17. @miciek
    TDDing Pac-Man Engine

    View Slide

  18. @miciek
    TDDing Pac-Man Engine

    View Slide

  19. @miciek
    TDDing Pac-Man Engine

    View Slide

  20. @miciek
    TDDing Pac-Man Engine

    View Slide

  21. @miciek
    Pure & Immutable

    View Slide

  22. @miciek
    TDDing Pac-Man Engine

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  26. @miciek
    What we always want...
    Fast!
    Stable!
    Quick to read/write!
    Documentation

    View Slide

  27. @miciek
    Testing
    HTTP layer
    Live coding

    View Slide

  28. @miciek
    Fast, Stable, Real, Documenting

    View Slide

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

    View Slide

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

    View Slide

  31. @miciek
    Requirements
    State
    holds multiple games at once
    updates (ticks) all the games
    every 200ms

    View Slide

  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

    View Slide

  33. @miciek
    Holding multiple games at once

    View Slide

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

    View Slide

  35. @miciek
    Quick integration
    tests
    Live coding

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  39. @miciek
    Communicating Services
    Backend service
    Frontend service
    Service Discovery
    Circuit Breaking
    Security
    Scalability & Routing
    Telemetry

    View Slide

  40. @miciek
    Testing on Production
    Backend service PROD
    Frontend service PROD
    Service Discovery
    Circuit Breaking
    Security
    Scalability & Routing
    Telemetry
    App Container App Container

    View Slide

  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

    View Slide

  42. @miciek
    Service Mesh
    Backend service PROD
    Frontend service PROD
    Sidecar Sidecar
    Another Service PROD
    Sidecar
    Service Mesh
    Control Plane

    View Slide

  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

    View Slide

  44. @miciek
    Isolated tests on
    Production
    Live coding

    View Slide

  45. @miciek
    New Testing Pyramid
    Real
    Fast,
    Stable,
    Real
    No HTTP Servers
    Function parameters
    Abstracting over data
    Routing test traffic
    Isolated tests on PROD

    View Slide

  46. @miciek
    Summary
    functional programming
    helps with
    separation of concerns
    service meshes
    may help with
    isolated testing
    ...but they are
    new & unproven

    View Slide

  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

    View Slide

  48. @miciek
    @miciek
    www.michalplachta.com

    View Slide

  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

    View Slide