how to write a testable state machine - accu€¦ · •write a few transition tests: look for some...
TRANSCRIPT
![Page 1: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/1.jpg)
How To Write ATestable State Machine
Matthew Jones
ACCU Conference, 26th April 2012
![Page 2: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/2.jpg)
State Machines
• Describe your ‘nightmare’ state machine
• 1000 line file
• Mother of all switch() statements
• 10s of line per case
• Nested switch()es
• Freely calling other code to implement the state
• What is the cyclometric complexity?
• Often an entire thread’s code in one place
• Classic testing problem
Writing a Testable State Machine 2
![Page 3: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/3.jpg)
Example
Writing a Testable State Machine 3
• http://en.wikipedia.org/wiki/Event_driven_finite_state_machine
• Trivially simple
• Still has dependency on “application”
![Page 4: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/4.jpg)
The TCP State Machine
Writing a Testable State Machine 4
![Page 5: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/5.jpg)
Linux TCP State Machine
• tcp_states.h
• State logic scattered over dozens of large files in /net
• State changes often ‘incidental’ in other code.
• Completely untestable
Writing a Testable State Machine 5
![Page 6: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/6.jpg)
The Problem
• Not separating concerns: include code in SM that implements the state.
• Seemingly trivial, but introduces dependency(s) on the application
• Mixes state logic with application logic.
• Testing• Manual? Run application, stimulate it, observe outcome.
Infer state machine operation
• printf()
• Stubbing & mocking to get a test suite to build tedious
• Too hard pressure not to.
Writing a Testable State Machine 6
![Page 7: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/7.jpg)
So ...
How should we go about writing a state machine from scratch so
that we can test it easily?
Writing a Testable State Machine 7
![Page 8: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/8.jpg)
What is a State Machine?
• “Code that manages the state of something, responding to external events, and translating them into actions to be implemented by the system“ ©me.
• Transitions from one state to another in response to events.
• Transitions normally expected to cause actions, but aren't a requirement.
• Details of actions are NOT part of the state machine.
Writing a Testable State Machine 8
![Page 9: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/9.jpg)
The Premise• SM is simply a way of turning events into actions.
• Any further details should be in the application.
Writing a Testable State Machine 9
• Maps nicely to an OO approach:• An events interface describing the
events the SM will respond to.• An actions interface describing
the actions the SM willoutput to the system.
• SM implements events.• Application implements actions.
![Page 10: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/10.jpg)
Making A Start
• Start from state diagram.
• Identify inputs and outputs:
• Inputs events
• Outputs actions
Writing a Testable State Machine 10
![Page 11: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/11.jpg)
Events and Actions
Writing a Testable State Machine 11
![Page 12: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/12.jpg)
Writing Some Tests
• Given the events we know the FSM can accept, we can write our first test to fire off an event and expect the action.
• Then another.• And another.• Soon we should have a test
for every transition on the diagram.
• See worked example later
Writing a Testable State Machine 12
![Page 13: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/13.jpg)
Writing Some Tests
• To add real world relevance to the work, add use case tests.
• May have implemented the diagram perfectly, but unless we put it through its paces it might not be apparent that the diagram is flawed.
• Mistake in example :-)
Writing a Testable State Machine 13
![Page 14: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/14.jpg)
Approaches• Language, application, company, project
specific.
• Derive test actions from actions interface and inject (i.e. Test doubles)
• Derive test implementation of SM to allow test code to sense transitions and actions
• Mock the actions interface.
• If non-00, stub action functions
Writing a Testable State Machine 14
![Page 15: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/15.jpg)
Method: Transition Tests• Bootstrap the SM, and test harness, into
existence:
• Write a few transition tests: look for some expected actions and resulting states
• Get code & test framework into place and settled
• Once the state machine is starting to grow, move to test vectors to simplify the tests, and move faster:
• [starting state, event(s), end state, expected action(s)]
Writing a Testable State Machine 15
![Page 16: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/16.jpg)
Example Test Vector
Writing a Testable State Machine 16
![Page 17: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/17.jpg)
Method: Use Case Tests• Add use case tests: inject >1 events, driving the
SM round multiple transitions.• Use cases should be good & bad, realistic &
unlikely.• Use cases apply the SM to the real world
application: they are acceptance tests (GOOS).• Each failing acceptance test lead us to TDD the
requisite transitions:• Add the necessary single transition test vectors, and
code the missing transitions / actions.
• Rinse and repeat
Writing a Testable State Machine 17
![Page 18: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/18.jpg)
Example Test Vectors
Writing a Testable State Machine 18
![Page 19: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/19.jpg)
Code Examples
Writing a Testable State Machine 19
![Page 20: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/20.jpg)
Events and Actions
Writing a Testable State Machine 20
![Page 21: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/21.jpg)
One State & Transition
Writing a Testable State Machine 21
![Page 22: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/22.jpg)
Two States & Transitions
Writing a Testable State Machine 22
![Page 23: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/23.jpg)
Add Third Transition & State
Writing a Testable State Machine 23
![Page 24: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/24.jpg)
Introduce Test Vectors
Writing a Testable State Machine 24
![Page 25: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/25.jpg)
Add Actions To Test Vector
Writing a Testable State Machine 25
![Page 26: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/26.jpg)
Merge Existing Tests Into One
Writing a Testable State Machine 26
![Page 27: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/27.jpg)
(Reminder)
Writing a Testable State Machine 27
![Page 28: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/28.jpg)
Add More Transitions And States
Writing a Testable State Machine 28
![Page 29: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/29.jpg)
Add Self Transitions For Closed
Writing a Testable State Machine 29
![Page 30: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/30.jpg)
Add Some Use Case Tests
Writing a Testable State Machine 30
No changes to SM code
![Page 31: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/31.jpg)
Add Use Case for ESTABLISHED CLOSED
Writing a Testable State Machine 31
![Page 32: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/32.jpg)
Finished Code
Writing a Testable State Machine 32
![Page 33: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/33.jpg)
Conclusions
• If we are growing the code in response to the tests, what does it look like?• Who cares? It works.
• TDD: it should be pretty simple
• GoF OO state machine pattern is unlikely to occur spontaneously.
• The same goes for hierarchical states, even if shown on the state diagram.
• But once tests are in place, we are free to refactor to this if we want.
Writing a Testable State Machine 33
![Page 34: How To Write A Testable State Machine - ACCU€¦ · •Write a few transition tests: look for some expected actions and resulting states •Get code & test framework into place and](https://reader034.vdocuments.site/reader034/viewer/2022042621/5f5414b14c18374a7775e77e/html5/thumbnails/34.jpg)
Conclusions• Maintain separation of concerns (app/SM)
• Test for every event in every state to prevent surprises & prove completeness
• Jump between acceptance tests (use case) and unit tests (transitions, actions)
• Use case tests can drive design in absence of state diagram
• Given a state transition diagram we cantest the SM into existence and prove it completely
Writing a Testable State Machine 34