java: Make testDatabaseWatchWithContextCancel more deterministic

It seems that sometimes the cancel is executed too late. This change
makes sure we call the cancel before we attempt to read from the
watch.

Change-Id: I7793c0c0017f8f85835a4121e4b1faa96d03f69a
diff --git a/lib/src/test/java/io/v/v23/syncbase/SyncbaseTest.java b/lib/src/test/java/io/v/v23/syncbase/SyncbaseTest.java
index 13246e7..696f9b3 100644
--- a/lib/src/test/java/io/v/v23/syncbase/SyncbaseTest.java
+++ b/lib/src/test/java/io/v/v23/syncbase/SyncbaseTest.java
@@ -11,6 +11,7 @@
 import com.google.common.io.Files;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
 import io.v.impl.google.naming.NamingUtil;
 import io.v.impl.google.services.syncbase.SyncbaseServer;
 import io.v.v23.InputChannel;
@@ -54,6 +55,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Random;
+import java.util.concurrent.TimeUnit;
 
 import static com.google.common.truth.Truth.assertThat;
 import static io.v.v23.VFutures.sync;
@@ -398,12 +400,15 @@
         InputChannel<WatchChange> channel = db.watch(
                 ctxC, sync(db.getResumeMarker(ctx)),
                 ImmutableList.of(Util.rowPrefixPattern(COLLECTION_ID, "b")));
+        final SettableFuture<Void> wait = SettableFuture.create();
         new Thread(new Runnable() {
             @Override
             public void run() {
                 ctxC.cancel();
+                wait.set(null);
             }
         }).start();
+        wait.get(1, TimeUnit.SECONDS);
         try {
             sync(InputChannels.asList(channel));
         } catch (CanceledException e) {