1
1
/*
2
- * Copyright 2020 the original author or authors.
2
+ * Copyright 2020-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .kafka .support .micrometer ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
20
21
import static org .mockito .ArgumentMatchers .any ;
21
22
import static org .mockito .ArgumentMatchers .anyBoolean ;
22
23
import static org .mockito .BDDMockito .given ;
31
32
import org .junit .jupiter .api .Test ;
32
33
33
34
import 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 ;
34
38
import org .springframework .test .util .ReflectionTestUtils ;
35
39
36
40
import io .micrometer .core .instrument .MeterRegistry ;
@@ -44,11 +48,12 @@ public class MicrometerHolderTests {
44
48
45
49
@ SuppressWarnings ({ "unchecked" , "deprecation" })
46
50
@ Test
47
- public void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy () {
51
+ void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy () {
48
52
MeterRegistry meterRegistry = new SimpleMeterRegistry ();
49
53
ApplicationContext ctx = mock (ApplicationContext .class );
50
54
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 ));
52
57
53
58
MicrometerHolder micrometerHolder = new MicrometerHolder (ctx , "holderName" ,
54
59
"timerName" , "timerDesc" , Collections .emptyMap ());
@@ -68,4 +73,70 @@ public void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy() {
68
73
verifyNoMoreInteractions (ctx , sample );
69
74
}
70
75
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
+
71
142
}
0 commit comments