forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Fixes #635: Changing creation of service classes away from ex…
…plicit guice usage to avoid duplicate instantiation of DefaultPrometheusMetrics class. (#644)" This reverts commit 9401c6e.
- Loading branch information
waschndolos
committed
Mar 28, 2024
1 parent
0c3c655
commit 0943cc1
Showing
8 changed files
with
117 additions
and
47 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/org/jenkinsci/plugins/prometheus/context/Context.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.jenkinsci.plugins.prometheus.context; | ||
|
||
import com.google.inject.AbstractModule; | ||
import hudson.Extension; | ||
import org.jenkinsci.plugins.prometheus.service.DefaultPrometheusMetrics; | ||
import org.jenkinsci.plugins.prometheus.service.PrometheusMetrics; | ||
|
||
@Extension | ||
public class Context extends AbstractModule { | ||
|
||
@Override | ||
public void configure() { | ||
bind(PrometheusMetrics.class).to(DefaultPrometheusMetrics.class).in(com.google.inject.Singleton.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
src/main/java/org/jenkinsci/plugins/prometheus/service/PrometheusMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/org/jenkinsci/plugins/prometheus/context/ContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.jenkinsci.plugins.prometheus.context; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import jenkins.metrics.api.Metrics; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration; | ||
import org.jenkinsci.plugins.prometheus.service.DefaultPrometheusMetrics; | ||
import org.jenkinsci.plugins.prometheus.service.PrometheusMetrics; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class ContextTest { | ||
|
||
@Test | ||
public void testCanInjectContext() { | ||
try (MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class); | ||
MockedStatic<PrometheusConfiguration> prometheusConfigurationStatic = mockStatic(PrometheusConfiguration.class); | ||
MockedStatic<Metrics> metricsStatic = mockStatic(Metrics.class)) { | ||
Jenkins mockedJenkins = mock(Jenkins.class); | ||
jenkinsStatic.when(Jenkins::get).thenReturn(mockedJenkins); | ||
|
||
MetricRegistry mockedMetrics = mock(MetricRegistry.class); | ||
metricsStatic.when(Metrics::metricRegistry).thenReturn(mockedMetrics); | ||
|
||
PrometheusConfiguration mockedPrometheusConfiguration = mock(PrometheusConfiguration.class); | ||
when(mockedPrometheusConfiguration.getLabeledBuildParameterNamesAsArray()).thenReturn(new String[]{}); | ||
when(mockedPrometheusConfiguration.getDefaultNamespace()).thenReturn("default"); | ||
prometheusConfigurationStatic.when(PrometheusConfiguration::get).thenReturn(mockedPrometheusConfiguration); | ||
|
||
Injector injector = Guice.createInjector(new Context()); | ||
PrometheusMetrics prometheusMetrics = injector.getInstance(PrometheusMetrics.class); | ||
|
||
Assertions.assertNotNull(prometheusMetrics); | ||
Assertions.assertEquals(DefaultPrometheusMetrics.class, prometheusMetrics.getClass()); | ||
|
||
PrometheusMetrics prometheusMetrics2 = injector.getInstance(PrometheusMetrics.class); | ||
|
||
Assertions.assertEquals(prometheusMetrics, prometheusMetrics2, "Should be the same as it's a singleton!"); | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters