You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/07/09 12:41:42 UTC

[GitHub] [dubbo] happytimor commented on pull request #10164: Interrupt current thread when InterruptException occur

happytimor commented on PR #10164:
URL: https://github.com/apache/dubbo/pull/10164#issuecomment-1179537089

   > 
   
   
   
   > would you please add some test cases to verify this
   
   it is a bit difficult for me to setup the ut environment with mockito, i can only provide basic code
   
   ## consumer:
   ``` java
   ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
   String name = "world" + System.currentTimeMillis();
   int initialFlag = 0, runningFlag = 1, normalReturn = 2, interruptReturn = 3;
   AtomicInteger flag = new AtomicInteger(initialFlag);
   Future<?> future = threadPoolExecutor.submit(() -> {
       try {
           flag.set(runningFlag);
           service.sayHello(name);
           flag.set(normalReturn);
       } catch (Exception e) {
           flag.set(interruptReturn);
       }
   });
   Thread.sleep(100);
   future.cancel(true);
   while (flag.get() == initialFlag || flag.get() == runningFlag) {
       Thread.yield();
   }
   Assertions.assertEquals(flag.get(), interruptReturn);
   ```
   
   ## provider:
   ``` java
   String lastName = "";
   
   @Override
   public String sayHello(String name) {
       if (!lastName.equals(name)) {
           lastName = name;
           try {
               //will be interrupted at first time
               Thread.sleep(200);
           } catch (InterruptedException ignore) {
   
           }
       }
       return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
   }
   
   ```
   
   ## important parameter:
   retries=3&cluster=failover
   
   ------
   flag will return normalReturn if we don`t add `Thread.currentThread().interrupt()`
   


-- 
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: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org