Repository: flink
Updated Branches:
  refs/heads/master 2929eda7e -> ad8ef6d01


[FLINK-8040] [tests] Fix test instability in ResourceGuardTest

This closes #5004.


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/ad8ef6d0
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/ad8ef6d0
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/ad8ef6d0

Branch: refs/heads/master
Commit: ad8ef6d01b23c74fcfb1f39a151dd00bfbd78ca3
Parents: 2929eda
Author: Stefan Richter <[email protected]>
Authored: Mon Nov 13 11:50:07 2017 +0100
Committer: Stefan Richter <[email protected]>
Committed: Mon Nov 13 13:43:15 2017 +0100

----------------------------------------------------------------------
 .../apache/flink/util/ResourceGuardTest.java    | 53 +++++++++++++-------
 1 file changed, 34 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/ad8ef6d0/flink-core/src/test/java/org/apache/flink/util/ResourceGuardTest.java
----------------------------------------------------------------------
diff --git 
a/flink-core/src/test/java/org/apache/flink/util/ResourceGuardTest.java 
b/flink-core/src/test/java/org/apache/flink/util/ResourceGuardTest.java
index 98aae4d..ade8d0f 100644
--- a/flink-core/src/test/java/org/apache/flink/util/ResourceGuardTest.java
+++ b/flink-core/src/test/java/org/apache/flink/util/ResourceGuardTest.java
@@ -53,30 +53,37 @@ public class ResourceGuardTest extends TestLogger {
        @Test
        public void testCloseBlockIfAcquired() throws Exception {
                ResourceGuard resourceGuard = new ResourceGuard();
-               ResourceGuard.Lease lease_1 = resourceGuard.acquireResource();
+               ResourceGuard.Lease lease = resourceGuard.acquireResource();
                AtomicBoolean checker = new AtomicBoolean(true);
 
                Thread closerThread = new Thread() {
                        @Override
                        public void run() {
-                               try {
-                                       // this line should block until all 
acquires are matched by releases.
-                                       resourceGuard.close();
-                                       checker.set(false);
-                               } catch (Exception ignore) {
-                                       checker.set(false);
-                               }
+                               // this line should block until all acquires 
are matched by releases.
+                               resourceGuard.close();
+                               checker.set(false);
                        }
                };
 
                closerThread.start();
 
-               ResourceGuard.Lease lease_2 = resourceGuard.acquireResource();
-               lease_2.close();
+               // we wait until the close()-call in the other thread happened.
+               while (!resourceGuard.isClosed()) {
+                       Thread.yield();
+               }
+
+               // validate that the close()-call is still blocked.
                Assert.assertTrue(checker.get());
 
-               // this matches the first acquire and will unblock the close.
-               lease_1.close();
+               // validate that the closed-status is already effective.
+               try {
+                       resourceGuard.acquireResource();
+                       Assert.fail("Resource guard is expected to be already 
closed.");
+               } catch (IOException ignore) {
+               }
+
+               // this matches the first acquire and will unblock the 
close()-call in the other thread.
+               lease.close();
                closerThread.join(60_000);
                Assert.assertFalse(checker.get());
        }
@@ -90,21 +97,29 @@ public class ResourceGuardTest extends TestLogger {
                Thread closerThread = new Thread() {
                        @Override
                        public void run() {
-                               try {
-                                       // this line should block until all 
acquires are matched by releases.
-                                       resourceGuard.close();
-                                       checker.set(false);
-                               } catch (Exception ignore) {
-                                       checker.set(false);
-                               }
+                               // this line should block until all acquires 
are matched by releases.
+                               resourceGuard.close();
+                               checker.set(false);
                        }
                };
 
                closerThread.start();
+
+               // we wait until the close()-call in the other thread happened.
+               while (!resourceGuard.isClosed()) {
+                       Thread.yield();
+               }
+
+               // attempt to unblock the resource guard via interrupt.
                closerThread.interrupt();
 
+               // wait some time.
+               closerThread.join(100);
+
+               // check that unblock through interrupting failed.
                Assert.assertTrue(checker.get());
 
+               // proper unblocking by closing the lease.
                lease.close();
                closerThread.join(60_000);
                Assert.assertFalse(checker.get());

Reply via email to