香港六合彩 » SlideShare

43
Simplify Testing using Fault Injection Rob Grzywinski [email protected]

description

白天有白天的感觉,白天有白天的味道。因为油麻地的雨多,油麻地香港六合彩家的床,白天都常常闲不着,摇晃着,吱吱呀呀地叫唤。这是雨中的乐章。油麻地的女香港六合彩似乎特别能生孩子,而这些孩子十有八九是在雨天怀上的。雨使油麻地的男香港六合彩一个个都形销骨立,雨也使油麻地香港六合彩丁兴旺。范烟户觉得在这样的天气里尤其寂寞,就坐在门口唱起来:晨(又鸟)初叫,昏鸦争噪,哪个不去红尘闹。路遥遥,水迢迢。功名尽在长安道,今日少年明日老。山,依旧好;香港六合彩,憔悴了……范烟户的曲儿,飘进了一条又一条巷子……朱荻洼去了一趟杜元潮家,只片刻工夫,又走进雨地里……[第70节]鬼雨/梨花雨8(1)随后,雨中,杜元潮、邱子东都在很诡秘地走动着,去了一趟朱小楼家,去了一趟小七子家……最后,杜元潮去了一趟废弃的仓房。仓房里住了一对卖唱的父女。香港六合彩是路过油麻地,没想到雨将香港六合彩困在了这儿。油麻地的香港六合彩自然想听唱,但,都想白听。父女俩岂肯白唱,就住到仓房里,蒙头睡觉去了。杜元潮掏出五元钱,请父女俩晚饭后到村子中央的祠堂里唱几个曲子。那父女俩自然很高兴,对杜元潮说:香港六合彩一定用力唱。杜元潮让小七子站在巷口,大喊了几声:今晚上,到祠堂听唱!这个消息很令香港六合彩振奋,一个个奔走相告。吃了晚饭,雨小了些,香港六合彩就三三两两往祠堂走,不时地听见香港六合彩说:走,听唱去!与以往的雨天不一样,今晚上的油麻地香港六合彩不是一吃了饭就熄了灯往床上爬,而是纷纷去了祠堂。当杜元潮看到满满一祠堂香港六合彩时,心里很高兴。今晚上不能让油麻地香港六合彩睡觉。油麻地香港六合彩睡觉死沉,性子又木,夜里房子倒了都不一定肯起床。今晚上,必须有成千上百醒着的油麻地香港六合彩。油麻地的历史需要香港六合彩今晚醒着。但也有不少香港六合彩未到祠堂里听唱,比如朱小楼、小七子等。这父女俩唱得很不错,又很卖力。女孩儿声音尖而亮,亮而纯,纯而甜。拉胡琴的父亲声音厚而沙,沙而沉,沉而有力。唱得木讷的油麻地香港六合彩一个个很兴奋,两眼发亮,眼珠儿也变得灵活起来,黑暗里,像无数的猫聚在一起。杜元潮与邱子东站在最后面的黑暗里。这谭月月家住在镇子西头,与镇里香港六合彩家相隔了一段路,显得很僻静。当祠堂里父女俩已唱了两三曲,一个个已渐入佳境时,李长望的身影在通往谭月月家的小路口犹疑不定地出现了。香港六合彩在路口站了站,沿着菜园中间的小路大步走向谭月月家的门口。这女香港六合彩似乎早在门口屏声听着外面的脚步走,当李长望刚刚走到门口时,门便慢慢地开了一条缝。李长望再次向四周观望了一下,闪进门里。门关上后不久,灯便灭了。一直埋伏在草丛里的朱小楼拍了拍与香港六合彩一般潮湿的小七子,急急地往祠堂去了。这女香港六合彩似乎等得很焦渴了,一熄了灯,就带着一股浓烈的雪花膏味,一头扑在李长望宽阔的怀里,并用小小的拳头不住地击打香港六合彩的胸膛,然后,就用双手揪住李长望的衣襟,一个劲地摇晃着香港六合彩,就像拴在树上的一头急了的牛摇晃着大树。嘴里不住地说:你个杀千刀的,死哪里去啦?死哪里去啦?莫不是又勾搭上另外的女香港六合彩了?你这到处吃腥的馋猫!你倒说呀?你倒说呀?你是在往死里折磨香港六合彩呢!

Transcript of 香港六合彩 » SlideShare

Page 1: 香港六合彩 » SlideShare

Simplify Testing using Fault Injection

Rob [email protected]

Page 2: 香港六合彩 » SlideShare

Motivation

• Writing and testing an IO-intensive application• Robust, asynchronous event (message) collector /

router• NIO (early 1.4 days)

• How to best simulate and test various failure modes?• NIO buggy as all get-out• Networks are inherently flaky and problematic

• Didn’t want to have to obfuscate or drastically change code in order to test it

Page 3: 香港六合彩 » SlideShare

Agenda

• Quick Introduction to Fault Injection• Why Use Fault Injection• Fault Injection Examples• Beyond Fault Injection• Quick Introduction to AOP• Fault Injection in Java

• AspectJ• Javassist

• Wrap Up

Page 4: 香港六合彩 » SlideShare

Fault Injection 101

• A fault is an abnormal condition or defect which may lead to a failure [10]

• Fault injection involves the deliberate insertion of faults or errors into a computer system in order to determine its response [9]

• The goal isn’t to recreate the conditions that produced the fault

Page 5: 香港六合彩 » SlideShare

Fault Injection 101 (cont)

There are two primary steps to Fault Injection:

• To identify the set of faults that can occur within an application, module, class, method, etc.• e.g. if the application does not use the network then

there’s no point in injecting network faults• In practice this isn’t as easy as it sounds

e.g. 3rd party libs

• To exercise those faults to evaluate how the application responds• Does the application detect the fault, is it isolated

and does the application recover from it? [8]

Page 6: 香港六合彩 » SlideShare

Example

byte[] readFile() throws IOException { ... final InputStream is = new FileInputStream(…); ... while((offset < bytes.length) && (numRead = is.read(bytes, offset, (bytes.length - offset))) >= 0) offset += numRead; ... is.close(); return bytes;}(from http://javaalmanac.com/egs/java.io/File2ByteArray.html)

What could go wrong with this code?

• new FileInputStream() can throw FileNotFoundException• InputStream.read() can throw IOException and

IndexOutOfBoundsException and can return -1 for end of file

• is.close() can throw IOException

How do we test how the application responds to one of these situations?

Specifically, how do we inject these faults so that we can test how the application responds?

Page 7: 香港六合彩 » SlideShare

Possible Techniques

• Force the situations at the OS level• Quite hairy to reproduce reliably when they’re

needed

• Refactor the snot out of it• Replace the call to InputStream.read() to some

local instrumented method• Create our own instrumented InputStream subclass

possibly using mock objects• Inject the subclass via IoC (requires some framework

such as PicoContainer or Spring)

• Just comment out the code and replace with throw new IOException()• Egad!

We need a way to inject a fault without changing the code!

Page 8: 香港六合彩 » SlideShare

Why Use Fault Injection

• Fault occurred in a production application and it is determined that that is a valid fault (i.e. it is expected to occur)

• Inject the fault in a testing environment without having to reproduce the conditions that actually caused the fault

• Any fault that is difficult or inconvenient to reproduce in testing:

• Out of memory• Disk full• Database crash• 3rd party library / Java bug• Deadlock• Resource contention• Dumb users• Invalid / stale sessions• Corner cases

Page 9: 香港六合彩 » SlideShare

Why Use Fault Injection (cont)

• You publish an API and you want to be tolerant of user input or response

• It is inconvenient or undesirable to refactor code to facilitate testing• e.g. 3rd party library

• You believe that testing code coverage is a good metric to measure for code quality [11]

• Injecting faults forces exception paths to be followed

More to come …

Page 10: 香港六合彩 » SlideShare

Reminder

You don’t have to recreate the conditions that caused the fault …

… you only have to recreate the fault

e.g. if an OhNoException crashed your application because some little old lady drove her 1966 Buick Skylark into your datacenter -- you don’t need the Skylark or the little old lady to fix the application

Page 11: 香港六合彩 » SlideShare

Quick Aside

Traditionally, Fault Injection is centers around faults derived from hardware:

• Disk faults such as file missing and corrupt file• Network faults such as host not found, host

inaccessible, and high latency• Memory faults such as corrupt memory• Operating system faults such as unexpected hardware

interrupts

Traditionally used in mission-critical fault tolerant environments

Page 12: 香港六合彩 » SlideShare

Quick Aside (cont)

Fault Injection comes in two favors:

• Hardware-based• Typically requires specialized hardware

• Software-based• Traditionally attempts to mimic hardware-based

fault injection and typically involves direct interaction with the operating system

• More recently expanded to include any fault that can occur in software such as:

• Mutated bytecode• Exceptions• Invalid or mutated input / output values• Deadlock / resource contention• Application Security• etc.

Page 13: 香港六合彩 » SlideShare

Example

Creating a directory and temporary file

final File directory = new File(…);final boolean success = directory.mkdirs();if(success) { final File file = File.createTempFile(…, …, directory); ...}

Faults:

• Making the parent directory(s) fails (returns false)

• Temp file creation failure (throws IOException)

• Permissions• Parent directory(s) deleted

• Injection of a specific temp file

Page 14: 香港六合彩 » SlideShare

Example (cont)

Great uses of Fault Injection that I can’t easily show in examples:

• Deeply nested exceptions• Effects of unchecked exceptions• Consequences of using catch(Exception e)

i.e. NullPointerExceptions that go unnoticed

• Complex / real-world cases

Page 15: 香港六合彩 » SlideShare

Intelligent Fault Injection

Fault injection doesn’t have to be all on or all off

Logic can be coded around injected faults

e.g. InputStream.read()• Throw IOException after n bytes are read• Return -1 (EOF) one byte before the actual EOF• Sporadically mutate the read bytes

Page 16: 香港六合彩 » SlideShare

Beyond Fault Injection

The techniques that I’m going to demonstrate aren’t specific to injecting faults (i.e. conditions that may lead to a failure)

• “What if” analysis• Test difficult corner cases

e.g. some optimization routine runs when certain conditions are met

• Initialize application state to some desired value• etc.

The techniques are similar to mock objects or IoC but much more granular

Page 17: 香港六合彩 » SlideShare

Summary

Intercept operations and inject code to:

• Perform a custom function• Return a custom value• Modify a input parameter• Throw an exception• Set the state of an object to a specific value

Page 18: 香港六合彩 » SlideShare

Checkpoint

Done:• Quick Introduction to Fault Injection• Why Use Fault Injection• Fault Injection Examples• Beyond Fault Injection

Next:• Quick Introduction to AOP• Fault Injection in Java

• AspectJ• Javassist

• Wrap Up

Page 19: 香港六合彩 » SlideShare

Checkpoint (cont)

Questions?

Page 20: 香港六合彩 » SlideShare

Aspect Oriented Programming 101

Join Point

a point in the flow of a running program [14]

It is the “where” in AOP

• Not all AO environments support all join pointse.g. Spring AOP only supports method-based join

points

Page 21: 香港六合彩 » SlideShare

Aspect Oriented Programming 101 (cont)

Join point examples

• reading or writing a field• calling or executing a method or constructor• catching or throwing an exception

A “join point shadow” is the location of a join point in the source code or bytecode of the program [5]

We will use “join point” and “join point shadow” interchangeably

Page 22: 香港六合彩 » SlideShare

Aspect Oriented Programming 101 (cont)

Pointcut

a set of join points [6]

Effectively a query where the join points are the data that is being queried – the “which”

Page 23: 香港六合彩 » SlideShare

Aspect Oriented Programming 101 (cont)

Advice

code that executes at each join point picked out by a pointcut [7]

Advice is the code that you’re injecting -- the “what”

Page 24: 香港六合彩 » SlideShare

Aspect Oriented Programming 101 (cont)

Aspect

a logical collection of advice

An aspect is to AO what an object is to OO

Page 25: 香港六合彩 » SlideShare

Aspect Oriented Programming 101 (cont)

Introduction (aka Mixin or inter-type declaration)

advice that provides additional functionality to a class

e.g. methods or member variables can be added via an Introduction to the “interface” of a class

Page 26: 香港六合彩 » SlideShare

Aspect Oriented Programming 101 (cont)

Weaving

the process by which specific join points are identified by pointcuts and advice is injected into bytecode

Page 27: 香港六合彩 » SlideShare

How AOP Works

• Compile-timea specialized compiler is used to weave the source code and advice together to produce bytecode

• Post-compile (aka binary weaving)

weaves advice into existing class files modifying them as necessary

• Load-timea classloader performs binary weaving as the class files are loaded into the JVM or a Java agent is used via JVMTI (JVM Tool Interface)

• Run-timeclasses that are already loaded by the JVM are woven with aspects via JPDA (Java Platform Debugger Architecture) to replace method bodies. Dynamic proxies can also be used.

Page 28: 香港六合彩 » SlideShare

Load-Time Weaving

We’re going to focus on load-time weaving (LTW)

• Advantange (over compile-time or post-compile): does not modify classes on disk

• Disadvantage: increases application startup time

Page 29: 香港六合彩 » SlideShare

Java AOP• AspectJ (http://www.eclipse.org/aspectj/): Excellent all-around solution.• dynaop (https://dynaop.dev.java.net/) Bob Lee’s stripped down AOP:

Requires proxy creation in code (i.e. not 100% transparent).• CGLIB (http://cglib.sourceforge.net/): a roll-your-own solution using

proxies. Requires proxy creation in code (i.e. not 100% transparent). For load-time weaving a custom class loader would be needed.

• BCEL (http://jakarta.apache.org/bcel/): roll-your-own solution requiring knowledge of Java byte code to write advice. For load-time weaving a custom class loader would be needed.

• Javassist (http://www.csg.is.titech.ac.jp/~chiba/javassist/): A large step above CGLIB / BCEL in that it includes a stripped down compiler and modifies the advised class on the fly. Load-time weaving is supported.

• Spring AOP (http://www.springframework.org/docs/reference/aop.html): Limited to method-based joinpoints. If using the @AspectJ annotations then it effectively becomes AspectJ otherwise standard Spring-style XML configuration. JDK dynamic proxies or CGLIB for implementation. If you’re already using Spring and only need to advise coarse-grained objects (i.e. the objects that you’re IoC’ing) and method-based jointpoints are suitable then this is a good solution.

• JMangler (http://roots.iai.uni-bonn.de/research/jmangler/): A step above BCEL / CGLIB but a step below Javassist. Requires knowledge of Java byte code (it uses the BCEL API for advice). Load-time weaving is supported.

• JBoss-AOP (http://labs.jboss.com/portal/jbossaop/): Effectively on par with AspectJ. If you’re using JBoss then this would be the best solution. Even has support in their IDE.

Page 30: 香港六合彩 » SlideShare

Questions?

Page 31: 香港六合彩 » SlideShare

AspectJ

• AspectJhttp://www.eclipse.org/aspectj/

• Documentationhttp://www.eclipse.org/aspectj/doc/released/

• AspectJ FAQ http://www.eclipse.org/aspectj/doc/next/faq.html

• AspectJ Development Environment Guide http://

www.eclipse.org/aspectj/doc/next/devguide/index.html

• AspectJ Programming Guidehttp://www.eclipse.org/aspectj/doc/released/progguide/

index.html

• AspectJ Quick Referencehttp://www.eclipse.org/aspectj/doc/released/quick5.pdf

Page 32: 香港六合彩 » SlideShare

Load-Time Weaving AspectJ

Load-time Weaving can be accomplished via [13]:

• -javaagent:pathto/aspectjweaver.jar• aj.bat• Custom classloader (WeavingURLClassLoader)

Both require a META-INF/aop.xml to be located in the classpath

Page 33: 香港六合彩 » SlideShare

AspectJ Examples

There are three primary classes in the example:

• Main• Creates and calls the other classes

• ReadFile• Accepts a File on construction• displayExistence() displays if the file exists• byte[] getBytesFromFile() returns the contents

of the file as a byte array• TemporaryFile

• readWriteTemporaryFile() • Creates a temp file• Writes data into it• Reads contents line-by-line• Reads contents byte-by-byte• Deletes the temp file

Page 34: 香港六合彩 » SlideShare

AspectJ Pros

• Quite mature

• Integration with IDEs (e.g. AJDT)

• Responsive forums

• Does 95% of what you need

Page 35: 香港六合彩 » SlideShare

AspectJ Cons

• Expect to burn a lot of time figuring out the difference between call and execute, within and withincode, etc.

• Can add quite a bit of time to incremental compiles (Also see bugs #146071 and #145438)

• It’s very easy to confuse the IDE• No access to local variables• You have to be careful about other errors in your

application. A compile error in some completely unrelated file may cause errors in aspects.

• Brittle with respect to refactoring especially in the case of load-time weaving. If using AJDT with compile-time weaving then refactorings apply across aspects.

Page 36: 香港六合彩 » SlideShare

Javassist

• Javassisthttp://www.csg.is.titech.ac.jp/~chiba/javassist/

• Javadochttp://www.csg.is.titech.ac.jp/~chiba/javassist/html/

index.html

• Tutorialhttp://www.csg.is.titech.ac.jp/~chiba/javassist/tutorial/

tutorial.html

Page 37: 香港六合彩 » SlideShare

Javassist Example

Page 38: 香港六合彩 » SlideShare

Javassist Pros

• Provides a relatively simple interface to do fault injection at the level of the bytecode

• Provides a limited compiler to take away the need to work directly in bytecode (e.g. BCEL)

Page 39: 香港六合彩 » SlideShare

Javassist Cons

• Limited compiler

• There’s much that you need to roll in order to use it

• Confusing code fragment language

Page 40: 香港六合彩 » SlideShare

Future Work

• Simplify fault injection + JUnite.g. run a set of test cases with different faults

• Library of faults for java.* and javax.*

• Profiler for java.* and javax.* faults

• Encourage fault libraries and profilers for common 3rd party libs

Page 41: 香港六合彩 » SlideShare

Questions?

Page 42: 香港六合彩 » SlideShare

References[1] Harbulot, B. & Gurd, J. (2005). A join point for loops in AspectJ.

http://www.cs.iastate.edu/~leavens/FOAL/papers-2005/harbulot-gurd.pdf

[2] http://www.eclipse.org/aspectj/doc/released/faq.html#q:joinpoints[3] http://www.aspectprogrammer.org/blogs/adrian/2004/06/go_from_stateme.html[4] Ossher, H. & Tarr, P. Operation-Level Composition: A Case in (Join)

Point http://www.ccs.neu.edu/research/demeter/SOP/Tarr.pdf

[5] Hilsdale, E. & Hugunin, J. Advice Weaving in AspectJ http://hugunin.net/papers/aosd-2004-cameraReady.pdf

[6] http://en.wikipedia.org/wiki/Pointcut[7] http://www.eclipse.org/aspectj/doc/released/faq.html#q:advice[8] Hsueh, M, Tasi, T, & Iyer, R. Fault InjectionTechniques and Tools.

IEEE CS Press, 1997, pp. 75-82[9] Clark, J. & Pradhan, D. Fault Injection. IEEE CS Press, 1995, pp. 47-56[10] http://en.wikipedia.org/wiki/Fault_%28technology%29[11] http://blogs.msdn.com/bwill/archive/2003/09/23/51169.aspx[12] Espak, M. Improving Efficiency by Weaving at Run-time

http://www.old.netobjectdays.org/pdf/03/papers/ws-yrw/415.pdf[13] http://www.eclipse.org/aspectj/doc/released/devguide/ltw-

configuration.html[14] http://en.wikipedia.org/wiki/Join_point

Page 43: 香港六合彩 » SlideShare

Thank You

Rob Grzywinskihttp://www.realityinteractive.com/rgrzywinski/[email protected]