sandynz opened a new issue #12476:
URL: https://github.com/apache/shardingsphere/issues/12476


   ## Bug Report
   
   ### Which version of ShardingSphere did you use?
   5.0.0-RC1-SNAPSHOT
   
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   ShardingSphere-Scaling
   
   ### Expected behavior
   Unit test not blocked.
   
   ### Actual behavior
   `assertThat(event.getType(), is(DataChangedEvent.Type.ADDED));` fail 
randomly.
   Unit test blocked.
   
   From GitHub CI log:
   ```
   [Curator-SafeNotifyService-0] o.a.c.f.l.MappingListenerManager - Listener 
(org.apache.curator.framework.recipes.cache.CuratorCacheListenerBuilderImpl$2@24ebbdf)
 threw an exception
   java.lang.AssertionError: 
   Expected: is <ADDED>
        but: was <UPDATED>
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:956)
        at org.junit.Assert.assertThat(Assert.java:923)
        at 
org.apache.shardingsphere.scaling.core.api.impl.GovernanceRepositoryAPIImplTest.lambda$assertWatch$0(GovernanceRepositoryAPIImplTest.java:91)
   ```
   
   ### Reason analyze (If you can)
   In `GovernanceRepositoryAPIImplTest.java`
   ```
       @Test
       public void assertWatch() throws InterruptedException {
           CountDownLatch countDownLatch = new CountDownLatch(1);
           String key = ScalingConstant.SCALING_ROOT + "/1";
           governanceRepositoryAPI.watch(ScalingConstant.SCALING_ROOT, event -> 
{
               if (event.getKey().equals(key)) {
                   assertThat(event.getType(), is(DataChangedEvent.Type.ADDED));
                   countDownLatch.countDown();
               }
           });
           governanceRepositoryAPI.persist(key, "");
           countDownLatch.await();
       }
   ```
   
   1, If `assertThat` throw exception, `countDownLatch.await();` will always be 
blocked.
   
   2, Repository key is the same in unit test methods, and value is persisted. 
See test log:
   ```
   [INFO ] 2021-09-16 12:07:34.817 [main] 
o.a.s.s.c.a.i.GovernanceRepositoryAPIImplTest - assertWatch, key=/scaling/1 
value=assertGetChildrenKeys
   [INFO ] 2021-09-16 12:07:34.919 [Curator-SafeNotifyService-0] 
o.a.s.s.c.a.i.GovernanceRepositoryAPIImplTest - 
event=DataChangedEvent(key=/scaling/1, value=assertWatch, type=ADDED)
   ```
   
   In `CuratorZookeeperRepository.java`
   ```
       private Type getChangedType(final TreeCacheEvent.Type type) {
           switch (type) {
               case NODE_ADDED:
                   return Type.ADDED;
               case NODE_UPDATED:
                   return Type.UPDATED;
               case NODE_REMOVED:
                   return Type.DELETED;
               default:
                   return Type.IGNORED;
           }
       }
   ```
   
   TreeCacheEvent type depends on whether path is locally cached or not, it 
could be `ADDED` or `UPDATED`.
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule 
configuration, when exception occur etc.
   Throw exception in `assertWatch`'s `watch` listener.
   
   ### Example codes for reproduce this issue (such as a github link).
   


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