<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have searched the <a href="https://github.com/apache/dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have checked the <a href="https://github.com/apache/dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: latest</li>
<li>Operating System version: linux</li>
<li>Java version: 9+</li>
</ul>
<h3 dir="auto">Requirement</h3>
<p dir="auto">After Java 9 ，the default JVM garbage collector is G1 , So please add G1 configuration in</p>
<p dir="auto"><code class="notranslate">dubbo/dubbo-container/dubbo-container-api/src/main/resources/META-INF/assembly/bin/start.sh</code></p>
<p dir="auto">when your jdk is higher then Java 8</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://github.com/apache/incubator-dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/apache/incubator-dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Steps to reproduce this issue</h3>
<p dir="auto">Run <code class="notranslate">AbstractMonitorFactoryTest.testMonitorFactoryGroupCache()</code> or <code class="notranslate">DubboMonitorTest.testCount()</code> many times(may be less than 10 is enough), you will get junit test failed.</p>
<h3 dir="auto">Cause Analysis</h3>
<ul dir="auto">
<li><strong>AbstractMonitorFactoryTest.testMonitorFactoryGroupCache()</strong></li>
</ul>
<p dir="auto">In AbstractMonitorFactory.getMonitor(), if we call <code class="notranslate">thenRunAsync()</code> first, then call <code class="notranslate">FUTURES.put()</code>,<br>
in MonitorListener.run, FUTRUE.get() may get null, then the junit test failed.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54415689-faeb2a80-4737-11e9-80dd-aaf347905b4d.png"><img src="https://user-images.githubusercontent.com/11908486/54415689-faeb2a80-4737-11e9-80dd-aaf347905b4d.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54415734-240bbb00-4738-11e9-8713-410fe0f51b4f.png"><img src="https://user-images.githubusercontent.com/11908486/54415734-240bbb00-4738-11e9-8713-410fe0f51b4f.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54415998-05f28a80-4739-11e9-985e-1b6650e567f1.png"><img src="https://user-images.githubusercontent.com/11908486/54415998-05f28a80-4739-11e9-985e-1b6650e567f1.png" alt="image" style="max-width: 100%;"></a></p>
<hr>
<ul dir="auto">
<li><strong>DubboMonitorTest.testCount()</strong></li>
</ul>
<ol dir="auto">
<li>In DubboMonitor constructor, we starter a schedule that call <code class="notranslate">send()</code> every 20 milliseconds!!</li>
<li>In <code class="notranslate">testCount()</code>, we <code class="notranslate">collect()</code> first, then <code class="notranslate">send()</code>, and <code class="notranslate">send()</code> every 20 milliseconds backend, but we will reset data in <code class="notranslate">send()</code> everytime, so let's consider this sequence: we call <code class="notranslate">collect()</code>, and then call <code class="notranslate">send()</code>, and  before Assertions.assertEquals... , call <code class="notranslate">send()</code> backend by the schedule to reset the statisticsMap.</li>
</ol>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54414686-e9ecea00-4734-11e9-9bf5-520b306ea352.png"><img src="https://user-images.githubusercontent.com/11908486/54414686-e9ecea00-4734-11e9-9bf5-520b306ea352.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54414693-f5d8ac00-4734-11e9-9dc1-394c9870d963.png"><img src="https://user-images.githubusercontent.com/11908486/54414693-f5d8ac00-4734-11e9-9dc1-394c9870d963.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54414706-00934100-4735-11e9-8574-3cfcd3b108fc.png"><img src="https://user-images.githubusercontent.com/11908486/54414706-00934100-4735-11e9-8574-3cfcd3b108fc.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11908486/54414902-9cbd4800-4735-11e9-824a-86be05f0e50e.png"><img src="https://user-images.githubusercontent.com/11908486/54414902-9cbd4800-4735-11e9-824a-86be05f0e50e.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">I describe it in Chinese again:<br>
这个问题是偶现的, 但是可以通过多次执行<code class="notranslate">testCount()</code>来复现(执行最多10次一般就能复现出来), 原因是, 在构造Du bboMonitor 的时候, 会启动一个定时器, 每个20毫秒来执行<code class="notranslate">send()</code>方法, 且在<code class="notranslate">send()</code>设置值以后, 就会reset 全局的statisticsMap, <code class="notranslate">testCount()</code>中考虑这么一个执行顺序: 先执行<code class="notranslate">collect()</code>, 在执行<code class="notranslate">send()</code>, 然后在执行诸多的Assertions.assertEquals之前, 可能会执行每20毫秒执行一次的<code class="notranslate">send()</code>, 在这个<code class="notranslate">send()</code>方法执行完成后, statisticsMap就会被重置, 导致junit test失败</p>