how to build rock solid apps & keep 100m+ users happy

49

Upload: iordanis-jordan-giannakakis

Post on 15-Jul-2015

984 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: How to build rock solid apps & keep 100m+ users happy
Page 2: How to build rock solid apps & keep 100m+ users happy

How Shazam works

Page 3: How to build rock solid apps & keep 100m+ users happy

100+ million users

Page 4: How to build rock solid apps & keep 100m+ users happy

Happy users

Even developers?

Page 5: How to build rock solid apps & keep 100m+ users happy

Android testing

Page 6: How to build rock solid apps & keep 100m+ users happy

Faster release cycles

Page 7: How to build rock solid apps & keep 100m+ users happy

Better code

Page 8: How to build rock solid apps & keep 100m+ users happy

Cheaper

http://testdroid.com/testdroid/5851

Page 9: How to build rock solid apps & keep 100m+ users happy

Easy

Page 10: How to build rock solid apps & keep 100m+ users happy

A user walks into a bar

Page 11: How to build rock solid apps & keep 100m+ users happy

Doing it the test driven way

   

   

Write  UI  test  

Implement  

Refactor  

Write  unit  test  BDD  

TDD  

Page 12: How to build rock solid apps & keep 100m+ users happy

Test first

•  Generally easier

•  Eliminates external dependencies

•  Easily repeatable

Page 13: How to build rock solid apps & keep 100m+ users happy

The Acceptance Tests cycle

   

   

Write  UI  test  

Implement  

Refactor  

Write  unit  test  BDD  

TDD  

Page 14: How to build rock solid apps & keep 100m+ users happy

Acceptance criteria

•  Given: arrange

•  When: act

•  Then: assert

Page 15: How to build rock solid apps & keep 100m+ users happy

Example

Given a user is near a music venue And the server always returns a known result When the user Shazams Then the user can check-in their discovery

Page 16: How to build rock solid apps & keep 100m+ users happy

gwen

 given(user).isNear(lexington());    given(server).returns(lustForLife());        when(user).shazams();      then(user).canCheckIn(lustForLife(),  lexington());  

http://github.com/shazam/gwen

Page 17: How to build rock solid apps & keep 100m+ users happy

Other libraries for Acceptance Tests

•  Instrumentation •  JUnit 3 •  Espresso & Robotium •  Fork •  HamMock Server

Page 18: How to build rock solid apps & keep 100m+ users happy

The Unit tests cycle

   

   

Write  UI  test  

Implement  

Refactor  

Write  unit  test  

BDD  

TDD  

Page 19: How to build rock solid apps & keep 100m+ users happy

What we don’t test

Activities & Fragments

http://github.com/xxv/android-lifecycle

Page 20: How to build rock solid apps & keep 100m+ users happy

What we don’t test

Page 21: How to build rock solid apps & keep 100m+ users happy

What we do test

•  Presentation logic

•  Business logic

•  That’s it

Page 22: How to build rock solid apps & keep 100m+ users happy

The MVP pattern

Presenter

Model View

Page 23: How to build rock solid apps & keep 100m+ users happy

The MVP pattern

Presenter

Model View

Android

Page 24: How to build rock solid apps & keep 100m+ users happy

The MVP pattern

•  Makes presentation logic testable

•  No need to test “dummy” view

•  Avoid Android dependencies

Page 25: How to build rock solid apps & keep 100m+ users happy

Dependency Injection, Yourself

•  Breaks hardcoded dependencies

•  Behaviour vs Implementation

•  Implementations for test & runtime

Page 26: How to build rock solid apps & keep 100m+ users happy

Feature X

Hardcoded dependencies

Client

Feature X

Page 27: How to build rock solid apps & keep 100m+ users happy

Dependency Injection

Client

Feature X Interface

Injector  

Page 28: How to build rock solid apps & keep 100m+ users happy

Model

 public  interface  VenueRetriever  {          void  findClosestVenue(VenueFoundCallback  callback);    }      public  class  NetworkVenueRetriever  implements  VenueRetriever  {          public  void  findClosestVenue(VenueFoundCallback  callback)  {                //  Some  slow  networking          }    }      public  class  LocalVenueRetriever  implements  VenueRetriever  {          public  void  findClosestVenue(VenueFoundCallback  callback)  {                //  DB  look-­‐up  /  caching  layer,  perhaps?          }    }  

Page 29: How to build rock solid apps & keep 100m+ users happy

Activity public  class  ResultActivity  extends  Activity  implements  ResultView  {      private  final  VenueRetriever  venueRetriever;      private  ResultPresenter  resultPresenter;        public  ResultActivity()  {          venueRetriever  =  venueRetriever();      }        public  void  onCreate(Bundle  savedInstanceState)  {          //  TODO:  Setup  layouts  &  views            Result  result  =  resultToDisplay();          resultPresenter=new  ResultPresenter(this,  venueRetriever,  result);      }        public  void  onStart()  {          resultPresenter.startPresenting();      }  }  

Page 30: How to build rock solid apps & keep 100m+ users happy

Presenter  public  class  ResultPresenter  {            public  ResultPresenter(ResultView  resultView,  VenueRetriever                        venueRetriever,  Result  result)  {                this.resultView  =  resultView;                this.venueRetriever  =  venueRetriever;                this.result  =  result;          }            public  void  startPresenting()  {                resultView.showResult(result);                venueRetriever.findClosestVenue(new  VenueFoundCallback()  {                      public  void  venueFound(Venue  venue)  {                            resultView.showCheckInPrompt(venue);                      }                });          }    }  

Page 31: How to build rock solid apps & keep 100m+ users happy

View

 public  interface  ResultView  {            void  showResult(Result  track);            void  showCheckInPrompt(Venue  venue);    }  

Page 32: How to build rock solid apps & keep 100m+ users happy

Activity

 public  class  ResultActivity  extends  Activity  implements  ResultView  {            public  void  showResult(Result  result)  {                //TODO  show  the  result  screen  &  bind  result  data          }            public  void  showCheckInPrompt(Venue  venue)  {                //TODO  bind  the  venue  with  check-­‐in  prompt  view          }    }  

Page 33: How to build rock solid apps & keep 100m+ users happy

Other Unit Test technologies

•  JUnit 4 •  Robolectric java.lang.RuntimeException: Stub! •  Hamcrest •  JMock

Page 34: How to build rock solid apps & keep 100m+ users happy

Test execution

Page 35: How to build rock solid apps & keep 100m+ users happy

Continuous Integration

Page 36: How to build rock solid apps & keep 100m+ users happy

Speed (Hint: slow)

Page 37: How to build rock solid apps & keep 100m+ users happy

Usual execution

Test  Suite  

Page 38: How to build rock solid apps & keep 100m+ users happy

Spoon

http://github.com/square/spoon

Page 39: How to build rock solid apps & keep 100m+ users happy

Fork

http://github.com/shazam/fork

Test  Suite  

Page 40: How to build rock solid apps & keep 100m+ users happy

Example

Page 41: How to build rock solid apps & keep 100m+ users happy

Fork

http://github.com/shazam/fork

Page 42: How to build rock solid apps & keep 100m+ users happy

Fork

http://github.com/shazam/fork

•  Pooled execution

•  Infinitely scalable

•  Current setup 1 test / 2 seconds

Page 43: How to build rock solid apps & keep 100m+ users happy

Flakiness monitor

Page 44: How to build rock solid apps & keep 100m+ users happy

ADB Remote

http://github.com/sleekweasel/CgiAdbRemote

Page 45: How to build rock solid apps & keep 100m+ users happy

If all else fails

Page 46: How to build rock solid apps & keep 100m+ users happy

If all else fails

Page 47: How to build rock solid apps & keep 100m+ users happy

If all else fails

Page 48: How to build rock solid apps & keep 100m+ users happy

Summary

•  Testing is easier than you may think •  Practice, practice, practice •  Toolset is limited but getting better •  Ship it!

Page 49: How to build rock solid apps & keep 100m+ users happy

Questions?