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


##########
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)
   
   maybe we can make this a little bit nasty and schedule many operations in 
parallel.
   It would be great to have a way to intercept the code paths and verify that 
we have exercised all the possible branches. (Mockito?)
   



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