apilloud commented on a change in pull request #13448:
URL: https://github.com/apache/beam/pull/13448#discussion_r533633896



##########
File path: 
runners/java-fn-execution/src/test/java/org/apache/beam/runners/fnexecution/control/DefaultJobBundleFactoryTest.java
##########
@@ -459,27 +461,28 @@ public void loadBalancesBundles() throws Exception {
       verify(envFactory, Mockito.times(2)).createEnvironment(eq(environment), 
any());
 
       long tms = System.currentTimeMillis();
-      AtomicBoolean closed = new AtomicBoolean();
-      // close to free up environment for another bundle
-      TimerTask closeBundleTask =
-          new TimerTask() {
-            @Override
-            public void run() {
-              try {
-                b2.close();
-                closed.set(true);
-              } catch (Exception e) {
-                throw new RuntimeException(e);
-              }
-            }
-          };
-      new Timer().schedule(closeBundleTask, 100);
-
+      ScheduledExecutorService executor = 
Executors.newSingleThreadScheduledExecutor();
+      ScheduledFuture<Optional<Exception>> closingFuture =
+          executor.schedule(
+              () -> {
+                try {
+                  b2.close();
+                  return Optional.empty();
+                } catch (Exception e) {
+                  return Optional.of(e);
+                }
+              },
+              100,
+              TimeUnit.MILLISECONDS);
+
+      // This call should block until closingFuture has finished closing b2
       RemoteBundle b3 = sbf.getBundle(orf, srh, 
BundleProgressHandler.ignored());
 
-      // ensure we waited for close
+      // ensure the previous call waited for close
       Assert.assertThat(System.currentTimeMillis() - tms, 
greaterThanOrEqualTo(100L));
-      Assert.assertThat(closed.get(), is(true));
+      // This assertion includes a small delay to give the forked thread a 
chance to finish if it's
+      // been blocked
+      Assert.assertThat(closingFuture.get(1, TimeUnit.MILLISECONDS), 
equalTo(Optional.empty()));

Review comment:
       That does seem like a good reason to want a timeout.
   
   Here are some possible ways to tighten things down further:
   1. Set `tms` in the thread just before calling `close()`, then check 
`getBundle` returns after that time. (This guards against the system is running 
slow and the thread scheduling after significantly more than 100ms.)
   2. Make the delay of 100 ms random. (This with 1 guards against a 
`Thread.sleep(100L)` implementation of `getBundle`.)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to