ConcJUnit: Unit Testing for Concurrent Programs COMP 600 Mathias Ricken Rice University August 24, 2009.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
/ PSWLAB Concurrent Bug Patterns and How to Test Them by Eitan Farchi, Yarden Nir, Shmuel Ur published in the proceedings of IPDPS’03 (PADTAD2003)
ConcJUnit: Unit Testing for Concurrent Programs PPPJ 2009 Mathias Ricken and Robert Cartwright Rice University August 28, 2009.
Multithreading The objectives of this chapter are:
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Overview Review – what constitutes a thread Creating threads – general Creating threads – Java What happens if synchronization is not used? Assignment.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Testing Concurrent Programs COMP Production Programming Mathias Ricken Rice University Spring 2009.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Unit Testing in Java with an Emphasis on Concurrency Corky Cartwright Rice and Halmstad Universities Summer 2013.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Internet Software Development More stuff on Threads Paul Krause.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
Test-First Java Concurrency for the Classroom SIGCSE 2010 Mathias Ricken and Robert Cartwright Rice University March 12, 2009.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Testing Concurrent Programs, A 7-Minute Jargon-Free Introduction Thesis Writing Seminar Mathias Ricken Rice University February 25, 2010.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
1 A Framework for Testing Concurrent Programs PhD Thesis Defense Mathias Ricken Rice University January 10, 2011.
A Framework for Testing Concurrent Programs COMP 600 Mathias Ricken Rice University August 27, 2007.
A Framework for Testing Concurrent Programs MS Thesis Defense Mathias Ricken Rice University June 14, 2007.
Threads.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
SilkTest 2008 R2 SP1: Silk4J Introduction. ConfidentialCopyright © 2008 Borland Software Corporation. 2 What is Silk4J? Silk4J enables you to create functional.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Java 3: Odds & Ends Advanced Programming Techniques.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Multi-Threading in Java
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Polytechnic University of Tirana Faculty of Information Technology Computer Engineering Department A MULTITHREADED SEARCH ENGINE AND TESTING OF MULTITHREADED.
1 A Framework for Testing Concurrent Programs PhD Proposal Mathias Ricken Rice University December 2, 2010.
November 27, 2007 Verification of a Concurrent Priority Queue Bart Verzijlenberg.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
PHPUnit Julia Bondar IAPM23. Agenda What is PHPUnit How to write an automated test Writing and running tests with PHPUnit Advantages and disadvantages.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Java Thread Programming
SCJP 9/10 Threads.
Multithreading The objectives of this chapter are:
Java Programming Language
Multithreaded Programming in Java
More About Threads.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Chapter 4: Threads.
Multithreading.
Multithreading.
21 Threads.
Multithreading The objectives of this chapter are:
Testing Concurrent Programs
Presentation transcript:

ConcJUnit: Unit Testing for Concurrent Programs COMP 600 Mathias Ricken Rice University August 24, 2009

2 Brian Goetz, Java Concurrency in Practice, Addison-Wesley, 2006

3 Concurrency Practiced Badly [1]

4 Unit Tests… Occur early Automate testing Keep the shared repository clean Serve as documentation Prevent bugs from reoccurring Allow safe refactoring

5 Existing Unit Testing Frameworks JUnit, TestNG Don’t detect test failures in child threads Don’t ensure that child threads terminate Tests that should fail may succeed

6 ConcJUnit Replacement for JUnit  Backward compatible  Just replace junit.jar file Detects failures in all threads Warns if child threads outlive main thread Available at

7 Sample JUnit Tests public class Test extends TestCase { public void testException() { throw new RuntimeException("booh!"); } public void testAssertion() { assertEquals(0, 1); } } if (0!=1) throw new AssertionFailedError(); } Both tests fail.

8 JUnit Test with Child Thread public class Test extends TestCase { public void testException() { new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); } new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); throw new RuntimeException("booh!"); Main thread Child thread Main thread Child thread spawns uncaught! end of test success!

9 JUnit Test with Child Thread public class Test extends TestCase { public void testException() { new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); } new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); throw new RuntimeException("booh!"); Uncaught exception, test should fail but does not! By default, no uncaught exception handler installed for child threads

10 Changes to JUnit Thread group with exception handler  JUnit test runs in a separate thread, not main thread  Child threads are created in same thread group  When test ends, check if handler was invoked

11 JUnit Test with Child Thread public class Test extends TestCase { public void testException() { new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); } new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); throw new RuntimeException("booh!"); invokes checks TestGroup’s Uncaught Exception Handler

12 JUnit Test with Child Thread public class Test extends TestCase { public void testException() { new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); } new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); throw new RuntimeException("booh!"); Test thread Child thread uncaught! end of test Main thread spawns and joinsresumes check group’s handler invokes group’s handler failure!

13 Child Thread Outlives Parent public class Test extends TestCase { public void testException() { new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); } new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); throw new RuntimeException("booh!"); Test thread Child thread uncaught! end of test success! invokes group’s handler Main thread check group’s handler Too late!

14 Well-Formed Tests Uncaught exceptions and failed assertions in all threads cause failure If the test is declared a success before all child threads have ended, failures may go unnoticed Therefore, all child threads must terminate before test ends  Check for living child threads after test ends

15 Check for Living Child Threads public class Test extends TestCase { public void testException() { new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); } new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }).start(); throw new RuntimeException("booh!"); Test thread Child thread uncaught! end of test failure! invokes group’s handler Main thread check for living child threads check group’s handler

16 Correctly Written Test public class Test extends TestCase { public void testException() { Thread t = new Thread(new Runnable() { public void run() { /* child thread */ } }); t.start(); t.join(); } Thread t = new Thread(new Runnable() { public void run() { /* child thread */ } }); t.start(); t.join(); // wait until child thread has ended /* child thread */ Test thread Child thread end of test success! Main thread check for living child threads check group’s handler [4]

17 Well-Formed Tests Use Join Uncaught exceptions and failed assertions in all threads cause failure Therefore, all child threads must terminate before test ends Without join() operation, a test may get “lucky”  Require all child threads to be joined

18 Fork/Join Model Parent thread joins with each of its child threads May be too limited for a general-purpose programming language Child thread 1 Child thread 2 Main thread

19 Join with All Offspring Threads Main thread joins with all offspring threads, regardless of what thread spawned them Child thread 1 Child thread 2 Main thread

20 Join with Last Thread of Chain Chain of child threads guaranteed to outlive parent Main thread joins with last thread of chain Child thread 1 Child thread 2 Main thread Child thread 3

21 Generalize to Join Graph Threads as nodes; edges to joined thread Test is well-formed as long as all threads are reachable from main thread Child thread 1 Child thread 2 Main thread Child thread 3 MT CT1 CT2 CT3

22 Child thread 1 Child thread 2 Main thread MT CT1 CT2 Child thread 1 Child thread 2 Main thread MT CT1 CT2 Join Graph Examples

23 Child thread 1 Child thread 2 Main thread MT CT1 CT2 Unreachable Nodes An unreachable node has not been joined  Child thread may outlive the test

24 this current thread MT CT Constructing the Graph In Thread.start() – add node for this End of Thread.join() – add edge from Thread.currentThread() to this [3]

25 Modifying the Java Runtime Changing Thread.start() and join()  Need to modify Java Runtime Library  Utility to process user’s rt.jar file  Put new jar file on boot classpath: -Xbootclasspath/p:newrt.jar

26 Modifying the Java Runtime May not always be possible  ~30 MB hard drive space needed  Access to boot classpath option required ConcJUnit written to work without it  Just no join graph and “lucky” warnings [5]

27 Evaluation JFreeChart  All tests passed; tests are not concurrent DrJava: 900 unit tests  Passed:880  No join: 1  Lucky: 18  Timeout: 1  Runtime overhead: ~1 percent

28 Limitations Only checks chosen schedule  A different schedule may still fail Example: Thread t = new Thread(…); if (nondeterministic()) t.join(); [2]

29 Future Work Randomly insert sleeps or yields  Example:If a notify() is delayed, a wait() may time out. Can detect a number of sample problems Record schedule, replay if test fails  Makes failures reproducible if found [6]

30 Thank you!

31 Extra Slides

32 Notes 1. Probably not caused by concurrency problems; just a metaphor. ←← 2. Also cannot detect uncaught exceptions in a program’s uncaught exception handler (JLS limitation) ←← 3. Only add edge if joined thread is really dead; do not add if join ended spuriously. ←←

33 public class Test extends TestCase { public void testException() { Thread t = new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }); t.start(); while(t.isAlive()) { try { t.join(); } catch(InterruptedException ie) { } } Thread t = new Thread(new Runnable() { public void run() { throw new RuntimeException("booh!"); } }); t.start(); while(t.isAlive()) { try { t.join(); } catch(InterruptedException ie) { } } throw new RuntimeException("booh!"); Loop since join() may end spuriously 4. Spurious Wakeup ←

34 Thread Creation Context In Thread.start() – also record stack trace of Thread.currentThread(  Easy to find source code of a child thread that is not joined  Also available for uncaught exception stack traces 5.

35 Creation Context Example class Main { void foo() { // which one? new Helper().start(); //... } AssertionError: at Helper.m(Helper.java:2) at Helper.run(Helper.java:3) Started at: at Main.foo(Main.java:4) at Main.bar(Main.java:15) at Main.main(Main.java:25) class Helper extends Thread { void m() { Assert.fail(); } public void run() { m(); } } ←

36 Notes 6. Have not studied probabilities or durations for sleeps/yields: One inserted delay may negatively impact a second inserted delay Example: If both notify() and wait() are delayed. ←←

37 Many Thanks To… My advisor  Corky Cartwright My committee members  Walid Taha  David Scott  Bill Scherer NSF and Texas ATP  For providing partial funding

38 Concurrency Problems Not Found During Testing [1]