kezhuw commented on code in PR #510:
URL: https://github.com/apache/curator/pull/510#discussion_r1881890541


##########
curator-framework/src/test/java/org/apache/curator/framework/imps/TestCreate.java:
##########
@@ -540,6 +564,65 @@ public void testIdempotentCreateConnectionLoss() throws 
Exception {
         }
     }
 
+    /**
+     * Tests all cases of create().orSetData()
+     */
+    @Test
+    public void testOrSetData() throws Exception {
+        CuratorFramework client = createClient(new DefaultACLProvider());
+        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 5, 
TimeUnit.SECONDS, new LinkedBlockingQueue<>());
+        try {
+            client.start();
+
+            Stat stat = new Stat();
+
+            String path = "/idpcreate";
+            String pathWithParents1 = "/idpcreate/1/a/b/c/d";
+            String pathWithParents2 = "/idpcreate/2/a/b/c/d";
+            byte[] data1 = new byte[] {1, 2, 3};
+            byte[] data2 = new byte[] {4, 5, 6};
+
+            // first and second create should succeed with the same path and 
different data
+            check(client, client.create().orSetData(), path, data1, 0, 
KeeperException.Code.OK);
+            check(client, client.create().orSetData(), path, data2, 1, 
KeeperException.Code.OK);
+            check(client, client.create(), path, data2, false);
+
+            // without creatingParentsIfNeeded, it should fail
+            check(client, client.create().orSetData(), pathWithParents1, 
data1, 0, KeeperException.Code.NONODE);
+
+            // with creatingParentsIfNeeded, it should succeed and succeed a 
second time as well
+            check(
+                    client,
+                    client.create().orSetData().creatingParentsIfNeeded(),
+                    pathWithParents1,
+                    data1,
+                    0,
+                    KeeperException.Code.OK);
+            check(client, client.create().orSetData(), pathWithParents1, 
data2, 1, KeeperException.Code.OK);
+
+            // Check that calling the same create().orSetData() in parallel is 
ok
+            Callable<Exception> setData = () -> {
+                try {
+                    
client.create().orSetData().creatingParentsIfNeeded().forPath(pathWithParents2, 
data2);
+                } catch (Exception e) {
+                    return e;
+                }
+                return null;
+            };
+            Future<Exception> f1 = executor.submit(setData);

Review Comment:
   > we may be very lucky and not reproduce the problem (if the machine is 
"slow" and the two operations don't run concurrently)
   
   For concurrent sensitive test, I usually run with help from IDE "until 
failure" or "run x times". I, personally, think "reproduce" means that it will 
reproduce the bug given enough runs(tens to hundreds depends on context).
   
   > Mockito?
   
   I prefer to run multiple times over mock if the number of runs is small. 
Mock forces us to think different and complicate things.
   
   I tested this after reverting the fix, it failed in the first runs without 
resorting to "run x times" on my 2015 macbook pro.



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to