IMUnit: Improved Multithreaded Unit Testing Vilas Jagannath, Milos Gligoric, Dongyun Jin, Qingzhou Luo Grigore Rosu, Darko Marinov January 31, 2012 CREST Open Workshop (COW 17) London, UK
Project Team Vilas Jagannath Milos Gligoric Dongyun Jin Qingzhou Luo Grigore Rosu Darko Marinov 2
Multicore World Shared Memory Multithreaded Parallel Performance! 3
Correct Difficult to Develop Multithreaded Code Shared Memory Multithreaded • Non-deterministic scheduling • Data races • Deadlocks • Atomicity violations • … Parallel 4
Difficult to Test Multithreaded Code Multithreaded Code • Failures triggered by specific schedules • Most research focuses on exploring schedules for given manually written tests on one given code version 5
Challenges in Unit Testing MT Code 1. How to write multithreaded unit tests? – Developers often want to test specific schedules – How to express schedules in unit tests? 2. How to explore multithreaded unit tests? – Current techniques focus on one code version – Code evolves, need efficient regression testing 3. How to generate multithreaded unit tests? – How to automatically generate test code ? – How to automatically generate schedules ? 6
Our Work on All Three Topics 1. Writing multithreaded unit tests (this talk) – IMUnit: Illinois/improved multithreaded unit testing [ESEC/FSE’11] • Read “immunity”: ¡isolate ¡code ¡from ¡ bugs 2. Regression testing – Prioritizing exploration of change-impacted schedules [ISSTA’11] – Selecting ¡schedules ¡under ¡changes ¡[ICST’10, ¡STVR’12?] 3. Generating tests – Generating ¡schedules ¡[ICSE’08] – Generating ¡code ¡[ICSE’12 ] 7
Example: ConcurrentHashMultiSet • Thread-safe Multiset aka Bag implementation • Provided by Guava (Google Collections) • Consider testing these three methods package com.google.common.collect; public class ConcurrentHashMultiSet<E> { boolean add(E element ) ¡… boolean remove(Object element ) ¡… int count(Object element ) ¡… … } 8
Testing Adds and Remove multiset add(42) remove(42) add(42) count(42) is schedule dependent 9
Testing Remove Before Adds multiset remove(42) add(42) add(42) count(42) == 2 10
Sleep-Based Test: Remove Before Adds @Test public void testRemoveBeforeAdds() … { … multiset = ConcurrentHashMultiset.create(); Thread addThread = new Thread(new Runnable() { public void run() { Thread. sleep(60); Sleep used to express multiset.add(42); multiset.add(42); and enforce schedule } }); addThread.start(); multiset.remove(42); addThread.join(); assertEquals(2, multiset.count(42)); } 11
Testing Remove Between Adds multiset add(42) remove(42) add(42) count(42) == 1 12
Sleep-Based Test: Remove Between Adds @Test public void testRemoveBetweenAdds() … { … multiset = ConcurrentHashMultiset.create(); Thread addThread = new Thread(new Runnable() { public void run() { multiset.add(42); Thread. sleep(80); Sleeps used to express multiset.add(42); } and enforce schedule }); addThread.start(); Thread. sleep(40); multiset.remove(42); addThread.join(); assertEquals(1, multiset.count(42)); } 13
Sleep-Based Tests : Issues @Test public void testRemoveBetweenAdds() … { … multiset = ConcurrentHashMultiset.create(); Thread addThread = new Thread(new Runnable() { public void run() { — Fragile multiset.add(42); Thread. sleep(80); — Inefficient Not buggy Buggy multiset.add(42); Pass True Negative False Negative } — Non modular }); Fail False Positive True Positive — Implicit schedule addThread.start(); Thread. sleep(40); multiset.remove(42); addThread.join(); assertEquals(1, multiset.count(42)); } 14
Others ¡have ¡also ¡recognized ¡issues… • Previous solutions: – ConAn: Long, Hoffman and Strooper – ConcJUnit: Ricken and Cartwright – ThreadControl: Dantas, Brasileiro and Cirne • Latest solution: – MultithreadedTC: Pugh and Ayewah – Tick-based tests + Robust, Efficient — But Non modular, Implicit schedule — Different from traditional tests • IMUnit: Event-based tests 15
IMUnit Test: Remove Before Adds @Test @Schedule(" finishRemove->startingAdd1 ") public void testRemoveBeforeAdds () ¡… ¡{ … multiset = ConcurrentHashMultiset. create(); Thread addThread = new Thread(new Runnable() { public void run() { @Event ("startingAdd1"); multiset.add(42); Event orderings used to multiset.add(42); specify schedules } }); addThread.start(); multiset.remove(42); @Event ("finishRemove"); addThread.join(); assertEquals(2, multiset.count(42)); } 16
IMUnit Test: Remove Before Adds @Test @Schedule(" finishRemove->startingAdd1 ") public void testRemoveBeforeAdds () ¡… ¡{ … multiset = ConcurrentHashMultiset. create(); Thread addThread = new Thread(new Runnable() { public void run() { @Event ("startingAdd1"); multiset.add(42); @Event: interesting point multiset.add(42); in execution of a thread } }); addThread.start(); multiset.remove(42); @Event ("finishRemove"); addThread.join(); assertEquals(2, multiset.count(42)); } 17
IMUnit Test: Remove Before Adds @Test @Schedule(" finishRemove->startingAdd1 ") public void testRemoveBeforeAdds () ¡… ¡{ … multiset = ConcurrentHashMultiset. create(); Thread addThread = new Thread(new Runnable() { public void run() { @Schedule: set of event @Event ("startingAdd1"); multiset.add(42); orderings multiset.add(42); • } e - > ¡e’ ¡≡ ¡e ¡before ¡e’ }); • Partial order addThread.start(); multiset.remove(42); @Event ("finishRemove"); addThread.join(); assertEquals(2, multiset.count(42)); } 18
IMUnit Test: Remove Between Adds @Test @Schedule(" finishAdd1->startingRemove, finishRemove->startingAdd2 ") public void testRemoveBetweenAdds () ¡… ¡{ … multiset = ConcurrentHashMultiset. create(); Thread addThread = new Thread(new Runnable() { public void run() { multiset.add(42); @Event ("finishAdd1"); @Event ("startingAdd2"); multiset.add(42); @Events } }); addThread.start(); @Event ("startingRemove"); multiset.remove(42); @Event ("finishRemove"); addThread.join(); assertEquals(1, multiset.count(42)); } 19
IMUnit Test: Both Schedules @Test @Schedule(" finishRemove->startingAdd1 ") @Schedule(" finishAdd1->startingRemove, finishRemove->startingAdd2 ") public void testAddRemove() … ¡{ … multiset = ConcurrentHashMultiset. create(); Thread addThread = new Thread(new Runnable() { public void run() { @Event ("startingAdd1"); multiset.add(42); @Event ("finishAdd1"); @Event ("startingAdd2"); multiset.add(42); } }); addThread.start(); @Event ("startingRemove"); multiset.remove(42); @Event ("finishRemove"); addThread.join(); … 20 }
Schedule Language <Schedule> ::= { <Ordering> "," } <Ordering> <Ordering> ::= <Condition> "->" <Basic Event> <Condition> ::= <Basic Event> | <Block Event> | <Condition> "||" <Condition> | <Condition> "&&" <Condition> | "(" <Condition> ")" <Block Event> ::= "[" <Basic Event> "]" <Basic Event> ::= <Event Name> ["@" <Thread Name>] | "start" "@" <Thread Name> | "end" "@" <Thread Name> <Event Name> ::= { <Id> "." } <Id> <Thread Name> ::= <Id> • Events • Two types: non-blocking-event and [blocking-event] • Can be parameterized by thread-name: event@threadName • Can ¡also ¡be ¡combined ¡into ¡conditions ¡using ¡“||” ¡and ¡“&&” ¡ • Ordering specifies order between a condition and event • “ - >” ¡is ¡the ¡ordering ¡operator • before-condition -> after-event • Schedule is a comma-separated list of orderings 21
Schedule Logic • Fragment of PTLTL Logic Syntax: – Over finite well formed multithreaded unit test execution traces Logic Semantics: – Two temporal operators • Block • Ordering • Guided by practical requirements – Over 200 existing multithreaded unit tests • Details in paper 22
Schedule Enforcement • Two implementations: original and light • Original implemented using JavaMOP • Schedule logic implemented as JavaMOP logic plugin • Takes as input a schedule and outputs a monitor • Monitor aspects are weaved into test code • Different monitor for each test, schedule pair • Monitor can work in two modes: – Active mode enforces schedules – Passive mode prints error if execution deviates from schedule 23
IMUnit Light • Original implementation: – Preprocessing for @Event – Instrumentation to weave in monitor – Dependency on AspectJ etc • IMUnit light – Just need imunit.jar on classpath – fireEvent (“ eventName ”) ¡instead ¡of ¡@Event – Centralized monitor provided by library – Even more efficient 24
Recommend
More recommend