commits@javamail.java.net

[javamail~mercurial:713] Fix deadlock in IdleManager - bug 6804

From: <shannon_at_java.net>
Date: Mon, 27 Apr 2015 22:08:12 +0000

Project: javamail
Repository: mercurial
Revision: 713
Author: shannon
Date: 2015-04-27 21:34:17 UTC
Link:

Log Message:
------------
Update version to 1.5.4-SNAPSHOT.
add missing entries for bugs that were fixed
Fix deadlock in IdleManager - bug 6804


Revisions:
----------
711
712
713


Modified Paths:
---------------
client/pom.xml
demo/pom.xml
dsn/pom.xml
gimap/pom.xml
imap/pom.xml
javadoc/pom.xml
logging/pom.xml
mail/pom.xml
mailapi/pom.xml
mailapijar/pom.xml
mailhandler/pom.xml
mbox/native/pom.xml
mbox/pom.xml
oldmail/pom.xml
outlook/pom.xml
parent-distrib/pom.xml
pom.xml
pop3/pom.xml
publish/pom.xml
servlet/pom.xml
smtp/pom.xml
taglib/pom.xml
webapp/pom.xml
doc/release/CHANGES.txt
mail/src/main/java/com/sun/mail/iap/Protocol.java
mail/src/main/java/com/sun/mail/iap/ResponseInputStream.java
mail/src/main/java/com/sun/mail/imap/IMAPFolder.java


Added Paths:
------------
mail/src/test/java/com/sun/mail/imap/IMAPIdleManagerTest.java


Diffs:
------
diff -r e5fca2ba4f16 -r 03b21f61f3a7 client/pom.xml
--- a/client/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/client/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 demo/pom.xml
--- a/demo/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/demo/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 dsn/pom.xml
--- a/dsn/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/dsn/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 gimap/pom.xml
--- a/gimap/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/gimap/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 imap/pom.xml
--- a/imap/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/imap/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>parent-distrib</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         <relativePath>../parent-distrib/pom.xml</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 javadoc/pom.xml
--- a/javadoc/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/javadoc/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,13 +48,13 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>
     <artifactId>javadoc</artifactId>
     <packaging>pom</packaging>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     <name>JavaMail API javadocs</name>
     <description>${project.name}</description>
 

diff -r e5fca2ba4f16 -r 03b21f61f3a7 logging/pom.xml
--- a/logging/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/logging/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 mail/pom.xml
--- a/mail/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/mail/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 mailapi/pom.xml
--- a/mailapi/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/mailapi/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -56,7 +56,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 mailapijar/pom.xml
--- a/mailapijar/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/mailapijar/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -55,7 +55,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>javax.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 mailhandler/pom.xml
--- a/mailhandler/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/mailhandler/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>parent-distrib</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         <relativePath>../parent-distrib/pom.xml</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 mbox/native/pom.xml
--- a/mbox/native/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/mbox/native/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         <relativePath>../../pom.xml</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 mbox/pom.xml
--- a/mbox/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/mbox/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 oldmail/pom.xml
--- a/oldmail/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/oldmail/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -53,7 +53,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>javax.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 outlook/pom.xml
--- a/outlook/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/outlook/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 parent-distrib/pom.xml
--- a/parent-distrib/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/parent-distrib/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 pom.xml
--- a/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -54,7 +54,7 @@
     <groupId>com.sun.mail</groupId>
     <artifactId>all</artifactId>
     <packaging>pom</packaging>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     <name>JavaMail API distribution</name>
     <description>${project.name}</description>
     <url>http://javamail.java.net</url>
@@ -85,9 +85,9 @@
     </organization>
 
     <properties>
- <mail.version>1.5.3</mail.version>
+ <mail.version>1.5.4-SNAPSHOT</mail.version>
         <!-- like mail.version, but with underscores instead of dots -->
- <mail.zipversion>1_5_3</mail.zipversion>
+ <mail.zipversion>1_5_4-SNAPSHOT</mail.zipversion>
         <mail.spec.version>1.5</mail.spec.version>
         <activation-api.version>1.1</activation-api.version>
         <!-- defaults that are overridden in mail module -->

diff -r e5fca2ba4f16 -r 03b21f61f3a7 pop3/pom.xml
--- a/pop3/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/pop3/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>parent-distrib</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         <relativePath>../parent-distrib/pom.xml</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 publish/pom.xml
--- a/publish/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/publish/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,13 +48,13 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>
     <artifactId>publish</artifactId>
     <packaging>pom</packaging>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     <name>JavaMail API publish project</name>
 
     <build>
@@ -89,44 +89,44 @@
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>javax.mail</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
     <!--
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>demo</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>client</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>servlet</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>webapp</artifactId>
             <type>war</type>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>taglib</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>logging</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.sun.mail</groupId>
             <artifactId>outlook</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         </dependency>
     -->
     </dependencies>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 servlet/pom.xml
--- a/servlet/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/servlet/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 smtp/pom.xml
--- a/smtp/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/smtp/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>parent-distrib</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
         <relativePath>../parent-distrib/pom.xml</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 taglib/pom.xml
--- a/taglib/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/taglib/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>

diff -r e5fca2ba4f16 -r 03b21f61f3a7 webapp/pom.xml
--- a/webapp/pom.xml Wed Apr 15 11:35:11 2015 -0700
+++ b/webapp/pom.xml Fri Apr 17 14:09:11 2015 -0700
@@ -48,7 +48,7 @@
     <parent>
         <groupId>com.sun.mail</groupId>
         <artifactId>all</artifactId>
- <version>1.5.3</version>
+ <version>1.5.4-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sun.mail</groupId>


diff -r 03b21f61f3a7 -r 51efb09767c6 doc/release/CHANGES.txt
--- a/doc/release/CHANGES.txt Fri Apr 17 14:09:11 2015 -0700
+++ b/doc/release/CHANGES.txt Fri Apr 17 14:10:23 2015 -0700
@@ -27,6 +27,7 @@
 K 6535 address similar to (x)<y>(z) will throw StringIndexOutOfBoundsException
 K 6551 Update logging demos to use the new 1.5.2 features
 K 6552 Use classloader ergonomics in the MailHandler
+K 6585 ArrayIndexOutOfBoundsException in IMAPFolder.copyUIDMessages
 K 6638 attachment filenames aren't being encoded by default
 K 6644 Include javadoc example formats for logging.
 K 6657 SharedFileInputStream has problems with 2GB+ files
@@ -75,6 +76,7 @@
                   ----------------------------
 The following bugs have been fixed in the 1.5.1 release.
 
+K 5901 Wrong "source" version in build.properties
 K 5924 IMAP provider should support the QRESYNC extension (RFC 5162)
 K 5925 IMAP provider should support the WITHIN search extension (RFC 5032)
 K 5933 JavaMail does not handle write timeouts


diff -r 51efb09767c6 -r 9711ace03942 doc/release/CHANGES.txt
--- a/doc/release/CHANGES.txt Fri Apr 17 14:10:23 2015 -0700
+++ b/doc/release/CHANGES.txt Mon Apr 27 14:34:17 2015 -0700
@@ -14,6 +14,13 @@
         https://kenai.com/bugzilla/
 
 
+ CHANGES IN THE 1.5.4 RELEASE
+ ----------------------------
+The following bugs have been fixed in the 1.5.4 release.
+
+K 6804 IdleManager can deadlock with frequent notifications
+
+
                   CHANGES IN THE 1.5.3 RELEASE
                   ----------------------------
 The following bugs have been fixed in the 1.5.3 release.

diff -r 51efb09767c6 -r 9711ace03942 mail/src/main/java/com/sun/mail/iap/Protocol.java
--- a/mail/src/main/java/com/sun/mail/iap/Protocol.java Fri Apr 17 14:10:23 2015 -0700
+++ b/mail/src/main/java/com/sun/mail/iap/Protocol.java Mon Apr 27 14:34:17 2015 -0700
@@ -253,6 +253,26 @@
     }
 
     /**
+ * Is another response available in our buffer?
+ *
+ * @return true if another response is in the buffer
+ * @since JavaMail 1.5.4
+ */
+ public boolean hasResponse() {
+ /*
+ * XXX - Really should peek ahead in the buffer to see
+ * if there's a *complete* response available, but if there
+ * isn't who's going to read more data into the buffer
+ * until there is?
+ */
+ try {
+ return input.available() > 0;
+ } catch (IOException ex) {
+ }
+ return false;
+ }
+
+ /**
      * Return a buffer to be used to read a response.
      * The default implementation returns null, which causes
      * a new buffer to be allocated for every response.

diff -r 51efb09767c6 -r 9711ace03942 mail/src/main/java/com/sun/mail/iap/ResponseInputStream.java
--- a/mail/src/main/java/com/sun/mail/iap/ResponseInputStream.java Fri Apr 17 14:10:23 2015 -0700
+++ b/mail/src/main/java/com/sun/mail/iap/ResponseInputStream.java Mon Apr 27 14:34:17 2015 -0700
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 1997-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997-2015 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -159,4 +159,15 @@
         ba.setCount(idx);
         return ba;
     }
+
+ /**
+ * How much buffered data do we have?
+ *
+ * @return number of bytes available
+ * @exception IOException if the stream has been closed
+ * @since JavaMail 1.5.4
+ */
+ public int available() throws IOException {
+ return bin.available();
+ }
 }

diff -r 51efb09767c6 -r 9711ace03942 mail/src/main/java/com/sun/mail/imap/IMAPFolder.java
--- a/mail/src/main/java/com/sun/mail/imap/IMAPFolder.java Fri Apr 17 14:10:23 2015 -0700
+++ b/mail/src/main/java/com/sun/mail/imap/IMAPFolder.java Mon Apr 27 14:34:17 2015 -0700
@@ -2965,42 +2965,45 @@
      * @since JavaMail 1.5.2
      */
     boolean handleIdle(boolean once) throws MessagingException {
- Response r = protocol.readIdleResponse();
- try {
- synchronized (messageCacheLock) {
- boolean done = true;
- try {
- if (r == null || protocol == null ||
- !protocol.processIdleResponse(r))
- return false; // done
- done = false;
- } finally {
- if (done) {
- logger.finest("handleIdle: set to RUNNING");
- idleState = RUNNING;
- idleManager = null;
- messageCacheLock.notifyAll();
+ do {
+ Response r = protocol.readIdleResponse();
+ try {
+ synchronized (messageCacheLock) {
+ boolean done = true;
+ try {
+ if (r == null || protocol == null ||
+ !protocol.processIdleResponse(r))
+ return false; // done
+ done = false;
+ } finally {
+ if (done) {
+ logger.finest("handleIdle: set to RUNNING");
+ idleState = RUNNING;
+ idleManager = null;
+ messageCacheLock.notifyAll();
+ }
+ }
+ if (once) {
+ if (idleState == IDLE) {
+ try {
+ protocol.idleAbort();
+ } catch (Exception ex) {
+ // ignore any failures, still have to abort.
+ // connection failures will be detected above
+ // in the call to readIdleResponse.
+ }
+ idleState = ABORTING;
+ }
                     }
                 }
- if (once) {
- if (idleState == IDLE) {
- try {
- protocol.idleAbort();
- } catch (Exception ex) {
- // ignore any failures, still have to abort.
- // connection failures will be detected above
- // in the call to readIdleResponse.
- }
- idleState = ABORTING;
- }
- }
+ } catch (ConnectionException cex) {
+ // Oops, the store or folder died on us.
+ throwClosedException(cex);
+ } catch (ProtocolException pex) {
+ throw new MessagingException(pex.getMessage(), pex);
             }
- } catch (ConnectionException cex) {
- // Oops, the store or folder died on us.
- throwClosedException(cex);
- } catch (ProtocolException pex) {
- throw new MessagingException(pex.getMessage(), pex);
- }
+ // keep processing responses already in our buffer
+ } while (protocol.hasResponse());
         return true;
     }
 

diff -r 51efb09767c6 -r 9711ace03942 mail/src/test/java/com/sun/mail/imap/IMAPIdleManagerTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mail/src/test/java/com/sun/mail/imap/IMAPIdleManagerTest.java Mon Apr 27 14:34:17 2015 -0700
@@ -0,0 +1,185 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright (c) 2009-2015 Oracle and/or its affiliates. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common Development
+ * and Distribution License("CDDL") (collectively, the "License"). You
+ * may not use this file except in compliance with the License. You can
+ * obtain a copy of the License at
+ * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
+ * or packager/legal/LICENSE.txt. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ * When distributing the software, include this License Header Notice in each
+ * file and include the License file at packager/legal/LICENSE.txt.
+ *
+ * GPL Classpath Exception:
+ * Oracle designates this particular file as subject to the "Classpath"
+ * exception as provided by Oracle in the GPL Version 2 section of the License
+ * file that accompanied this code.
+ *
+ * Modifications:
+ * If applicable, add the following below the License Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyright [year] [name of copyright owner]"
+ *
+ * Contributor(s):
+ * If you wish your version of this file to be governed by only the CDDL or
+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
+ * elects to include this software in this distribution under the [CDDL or GPL
+ * Version 2] license." If you don't indicate a single choice of license, a
+ * recipient has the option to distribute your version of this file under
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
+ * its licensees as provided above. However, if you add GPL Version 2 code
+ * and therefore, elected the GPL Version 2 license, then the option applies
+ * only if the new code is made subject to such option by the copyright
+ * holder.
+ */
+
+package com.sun.mail.imap;
+
+import java.io.IOException;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.Folder;
+import javax.mail.FetchProfile;
+
+import com.sun.mail.test.TestServer;
+
+import org.junit.Test;
+import org.junit.Rule;
+import org.junit.rules.Timeout;
+import static org.junit.Assert.fail;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test that IdleManager handles multiple responses in a single
+ * network packet correctly.
+ */
+public final class IMAPIdleManagerTest {
+
+ // timeout the test in case of deadlock
+ @Rule
+ public Timeout deadlockTimeout = new Timeout(5000);
+
+ @Test
+ public void testDone() {
+ test(new IMAPHandlerIdleDone());
+ }
+
+ @Test
+ public void testExists() {
+ test(new IMAPHandlerIdleExists());
+ }
+
+ private void test(IMAPHandlerIdle handler) {
+ TestServer server = null;
+ try {
+ server = new TestServer(handler);
+ server.start();
+
+ final Properties properties = new Properties();
+ properties.setProperty("mail.imap.host", "localhost");
+ properties.setProperty("mail.imap.port", "" + server.getPort());
+ properties.setProperty("mail.imap.usesocketchannels", "true");
+ final Session session = Session.getInstance(properties);
+ //session.setDebug(true);
+
+ ExecutorService executor = Executors.newCachedThreadPool();
+ IdleManager idleManager = new IdleManager(session, executor);
+
+ final IMAPStore store = (IMAPStore)session.getStore("imap");
+ try {
+ store.connect("test", "test");
+ Folder folder = store.getFolder("INBOX");
+ folder.open(Folder.READ_WRITE);
+ idleManager.watch(folder);
+ handler.waitForIdle();
+
+ // now do something that is sure to touch the server
+ FetchProfile fp = new FetchProfile();
+ fp.add(FetchProfile.Item.ENVELOPE);
+ folder.fetch(folder.getMessages(), fp);
+
+ // check that the new message was seen
+ int count = folder.getMessageCount();
+ folder.close(true);
+
+ assertEquals(3, count);
+ } catch (Exception ex) {
+ System.out.println(ex);
+ //ex.printStackTrace();
+ fail(ex.toString());
+ } finally {
+ store.close();
+ }
+ } catch (final Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ } finally {
+ if (server != null) {
+ server.quit();
+ }
+ }
+ }
+
+ /**
+ * Custom handler.
+ */
+ private static class IMAPHandlerIdle extends IMAPHandler {
+ // must be static because handler is cloned for each connection
+ protected static CountDownLatch latch = new CountDownLatch(1);
+
+ @Override
+ public void select() throws IOException {
+ numberOfMessages = 1;
+ super.select();
+ }
+
+ public void idle() throws IOException {
+ cont();
+ latch.countDown();
+ idleWait();
+ ok();
+ }
+
+ public void waitForIdle() throws InterruptedException {
+ latch.await();
+ }
+ }
+
+ /**
+ * Custom handler. Respond to DONE with a single packet containing
+ * EXISTS and OK.
+ */
+ private static final class IMAPHandlerIdleDone extends IMAPHandlerIdle {
+ @Override
+ public void idle() throws IOException {
+ cont();
+ latch.countDown();
+ idleWait();
+ println("* 3 EXISTS\r\n" + tag + " OK");
+ }
+ }
+
+ /**
+ * Custom handler. Send two EXISTS responses in a single packet.
+ */
+ private static final class IMAPHandlerIdleExists extends IMAPHandlerIdle {
+ @Override
+ public void idle() throws IOException {
+ cont();
+ latch.countDown();
+ idleWait();
+ println("* 2 EXISTS\r\n* 3 EXISTS");
+ ok();
+ }
+ }
+}