]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9802 fix NPE when app node is starting
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 24 Sep 2017 21:22:59 +0000 (23:22 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 26 Sep 2017 21:49:38 +0000 (23:49 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProvider.java
server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProviderTest.java

index a2f106b5a4f7c7abd94a3b4f8914b96694f9cc06..6cba07a7be243a300861c69cb725be6fde9a5788 100644 (file)
@@ -55,7 +55,9 @@ public class ProcessInfoProvider implements Startable {
 
   public static ProtobufSystemInfo.SystemInfo provide() {
     ProtobufSystemInfo.SystemInfo.Builder protobuf = ProtobufSystemInfo.SystemInfo.newBuilder();
-    instance.sections.forEach(section -> protobuf.addSections(section.toProtobuf()));
+    if (instance != null) {
+      instance.sections.forEach(section -> protobuf.addSections(section.toProtobuf()));
+    }
     return protobuf.build();
   }
 }
index afc2ebcc4cbdb685e8fa8bebd6364261016a2250..d418ad254d695687e1929bf407b78e700cd41624 100644 (file)
@@ -33,19 +33,6 @@ public class ProcessInfoProviderTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  @Test
-  public void set_self_as_statically_shared_instance() {
-    ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[0]);
-
-    underTest.start();
-    assertThat(ProcessInfoProvider.provide()).isNotNull();
-
-    underTest.stop();
-
-    expectedException.expect(NullPointerException.class);
-    ProcessInfoProvider.provide();
-  }
-
   @Test
   public void remove_global_sections_from_results() {
     ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{
@@ -72,4 +59,19 @@ public class ProcessInfoProviderTest {
 
     underTest.stop();
   }
+
+  @Test
+  public void empty_result_is_returned_if_not_started_yet() {
+    ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{
+      new TestSystemInfoSection("foo"),
+      new TestSystemInfoSection("bar")});
+
+    assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(0);
+
+    underTest.start();
+    assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(2);
+
+    underTest.stop();
+    assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(0);
+  }
 }