]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15919 fixed starting of Data Center edition with server push feature (#5459)
authorlukasz-jarocki-sonarsource <77498856+lukasz-jarocki-sonarsource@users.noreply.github.com>
Tue, 22 Feb 2022 13:02:49 +0000 (14:02 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 22 Feb 2022 20:02:46 +0000 (20:02 +0000)
server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistry.java
server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintClientsRegistryTest.java

index 6bdc3b7b58099f6e04472c652e73b58b4979b6e3..5b73339991d4477ac5a784da4013a7956516fb12 100644 (file)
@@ -49,20 +49,35 @@ public class SonarLintClientsRegistry implements RuleActivationListener {
 
   private final SonarLintClientPermissionsValidator sonarLintClientPermissionsValidator;
   private final List<SonarLintClient> clients = new CopyOnWriteArrayList<>();
+  private final RuleActivatorEventsDistributor eventsDistributor;
+
+  private boolean registeredToEvents = false;
 
   public SonarLintClientsRegistry(RuleActivatorEventsDistributor ruleActivatorEventsDistributor, SonarLintClientPermissionsValidator permissionsValidator) {
     this.sonarLintClientPermissionsValidator = permissionsValidator;
-
-    ruleActivatorEventsDistributor.subscribe(this);
+    this.eventsDistributor = ruleActivatorEventsDistributor;
   }
 
   public void registerClient(SonarLintClient sonarLintClient) {
+    ensureListeningToEvents();
     clients.add(sonarLintClient);
     sonarLintClient.scheduleHeartbeat();
     sonarLintClient.addListener(new SonarLintClientEventsListener(sonarLintClient));
     LOG.debug("Registering new SonarLint client");
   }
 
+  private synchronized void ensureListeningToEvents() {
+    if (registeredToEvents) {
+      return;
+    }
+    try {
+      eventsDistributor.subscribe(this);
+      registeredToEvents = true;
+    } catch (RuntimeException e) {
+      LOG.warn("Can not listen to rule activation events for server push. Web Server might not have started fully yet.", e);
+    }
+  }
+
   public void unregisterClient(SonarLintClient client) {
     client.close();
     clients.remove(client);
index 208fa45715f2a10256ad6cb61f1c5f287ebaaaa6..0e222a9aeb042feed694b351090ef9f3b001beef 100644 (file)
@@ -35,8 +35,10 @@ import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.pushapi.qualityprofile.StandaloneRuleActivatorEventsDistributor;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anySet;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.clearInvocations;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
@@ -56,12 +58,13 @@ public class SonarLintClientsRegistryTest {
   private final ServletOutputStream outputStream = mock(ServletOutputStream.class);
 
   private final SonarLintClientPermissionsValidator permissionsValidator = mock(SonarLintClientPermissionsValidator.class);
+  private final StandaloneRuleActivatorEventsDistributor eventsDistributor = mock(StandaloneRuleActivatorEventsDistributor.class);
 
   private SonarLintClientsRegistry underTest;
 
   @Before
   public void before() {
-    underTest = new SonarLintClientsRegistry(mock(StandaloneRuleActivatorEventsDistributor.class), permissionsValidator);
+    underTest = new SonarLintClientsRegistry(eventsDistributor, permissionsValidator);
   }
 
   @Test
@@ -191,6 +194,32 @@ public class SonarLintClientsRegistryTest {
     verify(sonarLintClient, times(2)).close();
   }
 
+  @Test
+  public void registerClient_whenCalledFirstTime_registerAlsoToListenToEvents() {
+    underTest.registerClient(createSampleSLClient());
+
+    verify(eventsDistributor).subscribe(underTest);
+  }
+
+  @Test
+  public void registerClient_whenCalledSecondTime_doNotRegisterToEvents() {
+    underTest.registerClient(createSampleSLClient());
+    clearInvocations(eventsDistributor);
+
+    underTest.registerClient(createSampleSLClient());
+    verifyNoInteractions(eventsDistributor);
+  }
+
+  @Test
+  public void registerClient_whenExceptionAndCalledSecondTime_registerToEvents() {
+    doThrow(new RuntimeException()).when(eventsDistributor).subscribe(any());
+    underTest.registerClient(createSampleSLClient());
+    clearInvocations(eventsDistributor);
+
+    underTest.registerClient(createSampleSLClient());
+    verify(eventsDistributor).subscribe(underTest);
+  }
+
   private SonarLintClient createSampleSLClient() {
     SonarLintClient mock = mock(SonarLintClient.class);
     when(mock.getLanguages()).thenReturn(Set.of("java"));