Top Banner
Tests are not specs How to write actual specifications with TLA+ Hillel Wayne @hillelogram [email protected]
44

Your Tests Are Not Your Specs

Mar 22, 2017

Download

Software

Hillel Wayne
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Your Tests Are Not Your Specs

Tests are not specsHow to write actual specifications with TLA+

Hillel Wayne@hillelogram

[email protected]

Page 2: Your Tests Are Not Your Specs

Rules• Interrupt me if I’m speaking too fast• You don’t need to understand the code samples• Ask questions if you have them• I may punt them until later

Page 3: Your Tests Are Not Your Specs

Espark LearningOr core, same thing

Page 4: Your Tests Are Not Your Specs
Page 5: Your Tests Are Not Your Specs
Page 6: Your Tests Are Not Your Specs

MDM

School

Page 7: Your Tests Are Not Your Specs

MDM

School

Page 8: Your Tests Are Not Your Specs

Business logic

Page 9: Your Tests Are Not Your Specs

TDD• Write a failing test• Fix the failing test• Refactor

Page 10: Your Tests Are Not Your Specs
Page 11: Your Tests Are Not Your Specs

Describe EmailSender context “when called” it “should send an email” it “should not send two emails”

Page 12: Your Tests Are Not Your Specs

Describe EmailSender context “when called” context “and the email API is lagging out” it “should send an email” it “should not send two emails”

Page 13: Your Tests Are Not Your Specs

Describe EmailSender context “when called” context “and the email API is lagging out” context “and there are three separate email services” context “and our AP database has a partition” it “should send an email” it “should not send two emails”

Page 14: Your Tests Are Not Your Specs

TESTS ARE NOT SPECS

Page 15: Your Tests Are Not Your Specs
Page 16: Your Tests Are Not Your Specs

Programming Languages

• I/O• Implementations• Concrete• Fast• Designed to run

Specification Languages

• No I/O• Definitions• Abstract• “Comprehensive”• Designed to check

Page 17: Your Tests Are Not Your Specs

Maximum of Setdef maximum(numbers) max = numbers.first numbers.each do |n| if n > max max = n end maxend

Maximum(numbers) == CHOOSE max \in numbers: \A n \in numbers: max >= n

Page 18: Your Tests Are Not Your Specs
Page 19: Your Tests Are Not Your Specs

Alice BobMoney

Page 20: Your Tests Are Not Your Specs

Variables alice_account = 4, bob_account = 0;

Process transfer \in {1}Variables to_transfer \in 1..5Begin Withdraw: alice_account := alice_account – to_transfer; Deposit: bob_account := bob_account + to_transfer;End process;

NoOverDrafts == alice_account >= 0

Page 21: Your Tests Are Not Your Specs
Page 22: Your Tests Are Not Your Specs
Page 23: Your Tests Are Not Your Specs

1 Withdraw Deposit

2 Withdraw Deposit

3 Withdraw Deposit

4 Withdraw Deposit

5 Withdraw Broken

Page 24: Your Tests Are Not Your Specs

Variables alice_account = 4, bob_account = 0;

Process transfer \in {1}Variables to_transfer \in 1..5Begin Start: if to_transfer <= alice_account then Withdraw: alice_account := alice_account – to_transfer; Deposit: bob_account := bob_account + to_transfer; end if;End process;

Page 25: Your Tests Are Not Your Specs

Variables alice_account = 4, bob_account = 0;

Process transfer \in {1, 2}Variables to_transfer \in 1..5Begin Start: if to_transfer <= alice_account then Withdraw: alice_account := alice_account – to_transfer; Deposit: bob_account := bob_account + to_transfer; end if;End process;

Page 26: Your Tests Are Not Your Specs
Page 27: Your Tests Are Not Your Specs

CheckWithdraw

CheckWithdraw

Deposit

WithdrawWithdrawCheck

Page 28: Your Tests Are Not Your Specs

Variables alice_account = 4, bob_account = 0;

Process transfer \in {1}Variables to_transfer \in 1..5Begin Start: if to_transfer <= alice_account then Withdraw: alice_account := alice_account – to_transfer; Deposit: bob_account := bob_account + to_transfer; end if;End process;

<>[](alice_account + bob_account = 4)

Page 29: Your Tests Are Not Your Specs
Page 30: Your Tests Are Not Your Specs

Check Withdraw Crash

Page 31: Your Tests Are Not Your Specs

Use case

• For performance reasons, we should install the app on more than one device per API call.• For stability reasons, we could never show the MDM it was incorrect

about something. So if we tell it to install an app it thinks it installed, the system wigs out.

Page 32: Your Tests Are Not Your Specs

EXTENDS Integers, TLC, SequencesCONSTANTS Devices

(* --algorithm BatchInstallvariables AppScope \in [Devices -> {0, 1}]; Installs \in [Devices -> BOOLEAN]; batch_pool = {}; lock = FALSE;

define PoolNotEmpty == batch_pool # {}end define

procedure ChangeAppScope()variables cache;begin GetLock: await ~lock; lock := TRUE; Cache: cache := batch_pool; Filter: cache := cache \intersect {d \in Devices: AppScope[d] = 0}; Add: AppScope := [d \in Devices |-> IF d \in cache THEN AppScope[d] + 1 ELSE AppScope[d] ]; Clean:

batch_pool := batch_pool \ cache; lock := FALSE; return;end procedure

fair process SyncDevice \in Devicesbegin Sync: if Installs[self] then batch_pool := batch_pool \union {self}; end if; if PoolNotEmpty then either call ChangeAppScope(); or skip; end either; end if;end process;

fair process TimeLoop = 0begin Start: while TRUE do await PoolNotEmpty; call ChangeAppScope(); end while;end processend algorithm;

Page 33: Your Tests Are Not Your Specs

Filter: cache := cache \intersect {d \in Devices: AppScope[d] = 0};Clean: batch_pool := batch_pool \ cache;

Page 34: Your Tests Are Not Your Specs

GetLock: await ~lock; lock := TRUE;

Page 35: Your Tests Are Not Your Specs

TLA+ SUCKS

Page 36: Your Tests Are Not Your Specs

Slow

Page 37: Your Tests Are Not Your Specs

Unfriendly• F[a] vs F(a) vs F a• [A -> B] vs [A |-> B]• = vs == vs := vs /= vs #• Whitespace is sometimes significant• …And More!

Page 38: Your Tests Are Not Your Specs
Page 39: Your Tests Are Not Your Specs
Page 40: Your Tests Are Not Your Specs

SPECS ARE NOT TESTS

Page 41: Your Tests Are Not Your Specs

Conclusions• Tests check implementation of code• Specs check design of system• Formal specification helps find complicated bugs• TLA+ will save your butt

Page 42: Your Tests Are Not Your Specs

Resources• http://lamport.azurewebsites.net/tla/tla.html• http://lamport.azurewebsites.net/tla/book.html• https://learntla.com (me!)

Page 43: Your Tests Are Not Your Specs
Page 44: Your Tests Are Not Your Specs

Questions?

Hillel Wayne

[email protected]

Twitter: @hillelogram

Site: www.learntla.com