1717package org .springframework .kafka .support .micrometer ;
1818
1919import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
2021import static org .mockito .ArgumentMatchers .any ;
2122import static org .mockito .ArgumentMatchers .anyBoolean ;
2223import static org .mockito .BDDMockito .given ;
3132import org .junit .jupiter .api .Test ;
3233
3334import org .springframework .context .ApplicationContext ;
35+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
36+ import org .springframework .context .annotation .Bean ;
37+ import org .springframework .context .annotation .Primary ;
3438import org .springframework .test .util .ReflectionTestUtils ;
3539
3640import io .micrometer .core .instrument .MeterRegistry ;
@@ -44,11 +48,12 @@ public class MicrometerHolderTests {
4448
4549 @ SuppressWarnings ("unchecked" )
4650 @ Test
47- public void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy () {
51+ void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy () {
4852 MeterRegistry meterRegistry = new SimpleMeterRegistry ();
4953 ApplicationContext ctx = mock (ApplicationContext .class );
5054 Timer .Sample sample = mock (Timer .Sample .class );
51- given (ctx .getBeansOfType (any (), anyBoolean (), anyBoolean ())).willReturn (Collections .singletonMap ("registry" , meterRegistry ));
55+ given (ctx .getBeansOfType (any (), anyBoolean (), anyBoolean ()))
56+ .willReturn (Collections .singletonMap ("registry" , meterRegistry ));
5257
5358 MicrometerHolder micrometerHolder = new MicrometerHolder (ctx , "holderName" ,
5459 "timerName" , "timerDesc" , Collections .emptyMap ());
@@ -68,4 +73,70 @@ public void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy() {
6873 verifyNoMoreInteractions (ctx , sample );
6974 }
7075
76+ @ Test
77+ void multiReg () {
78+ assertThatIllegalStateException ().isThrownBy (() -> new MicrometerHolder (
79+ new AnnotationConfigApplicationContext (Config1 .class ), "" , "" , "" , Collections .emptyMap ()));
80+ }
81+
82+ @ Test
83+ void twoPrimaries () {
84+ assertThatIllegalStateException ().isThrownBy (() -> new MicrometerHolder (
85+ new AnnotationConfigApplicationContext (Config2 .class ), "" , "" , "" , Collections .emptyMap ()));
86+ }
87+
88+ @ Test
89+ void primary () {
90+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (Config3 .class );
91+ MicrometerHolder micrometerHolder = new MicrometerHolder (ctx , "holderName" ,
92+ "timerName" , "timerDesc" , Collections .emptyMap ());
93+ Map <String , Timer > meters = (Map <String , Timer >) ReflectionTestUtils .getField (micrometerHolder , "meters" );
94+ assertThat (meters ).hasSize (1 );
95+ }
96+
97+ static class Config1 {
98+
99+ @ Bean
100+ MeterRegistry reg1 () {
101+ return new SimpleMeterRegistry ();
102+ }
103+
104+ @ Bean
105+ MeterRegistry reg2 () {
106+ return new SimpleMeterRegistry ();
107+ }
108+
109+ }
110+
111+ static class Config2 {
112+
113+ @ Bean
114+ @ Primary
115+ MeterRegistry reg1 () {
116+ return new SimpleMeterRegistry ();
117+ }
118+
119+ @ Bean
120+ @ Primary
121+ MeterRegistry reg2 () {
122+ return new SimpleMeterRegistry ();
123+ }
124+
125+ }
126+
127+ static class Config3 {
128+
129+ @ Bean
130+ @ Primary
131+ MeterRegistry reg1 () {
132+ return new SimpleMeterRegistry ();
133+ }
134+
135+ @ Bean
136+ MeterRegistry reg2 () {
137+ return new SimpleMeterRegistry ();
138+ }
139+
140+ }
141+
71142}
0 commit comments