20
20
import static org .mockito .Mockito .*;
21
21
22
22
import java .time .Instant ;
23
+ import java .util .Arrays ;
23
24
import java .util .List ;
24
25
import java .util .Set ;
25
26
import java .util .concurrent .CompletableFuture ;
@@ -93,8 +94,8 @@ void whenDefaultDimension_DimensionValue_Empty() throws DimensionSetExceededExce
93
94
assertEquals (1 , sink .getContext ().getDimensions ().size ());
94
95
Set <String > dimensionKeys = sink .getContext ().getDimensions ().get (0 ).getDimensionKeys ();
95
96
assertEquals (2 , dimensionKeys .size ());
96
- dimensionKeys .contains ("ServiceName" );
97
- dimensionKeys .contains ("ServiceType" );
97
+ assertTrue ( dimensionKeys .contains ("ServiceName" ) );
98
+ assertTrue ( dimensionKeys .contains ("ServiceType" ) );
98
99
}
99
100
100
101
@ Test
@@ -105,8 +106,8 @@ void whenDefaultDimension_DimensionValue_Blank() throws DimensionSetExceededExce
105
106
assertEquals (1 , sink .getContext ().getDimensions ().size ());
106
107
Set <String > dimensionKeys = sink .getContext ().getDimensions ().get (0 ).getDimensionKeys ();
107
108
assertEquals (2 , dimensionKeys .size ());
108
- dimensionKeys .contains ("ServiceName" );
109
- dimensionKeys .contains ("ServiceType" );
109
+ assertTrue ( dimensionKeys .contains ("ServiceName" ) );
110
+ assertTrue ( dimensionKeys .contains ("ServiceType" ) );
110
111
}
111
112
112
113
@ ParameterizedTest
@@ -126,8 +127,11 @@ void whenSetDimension_withInvalidValue_thenThrowInvalidDimensionException(
126
127
127
128
@ Test
128
129
void whenSetDimension_withNameTooLong_thenThrowDimensionException () {
129
- String dimensionName = "a" .repeat (Constants .MAX_DIMENSION_NAME_LENGTH + 1 );
130
+ char [] longName = new char [Constants .MAX_DIMENSION_NAME_LENGTH + 1 ];
131
+ Arrays .fill (longName , 'a' );
132
+ String dimensionName = String .valueOf (longName );
130
133
String dimensionValue = "dimValue" ;
134
+
131
135
assertThrows (
132
136
InvalidDimensionException .class ,
133
137
() -> DimensionSet .of (dimensionName , dimensionValue ));
@@ -136,7 +140,10 @@ void whenSetDimension_withNameTooLong_thenThrowDimensionException() {
136
140
@ Test
137
141
void whenSetDimension_withValueTooLong_thenThrowDimensionException () {
138
142
String dimensionName = "dim" ;
139
- String dimensionValue = "a" .repeat (Constants .MAX_DIMENSION_VALUE_LENGTH + 1 );
143
+ char [] longName = new char [Constants .MAX_DIMENSION_VALUE_LENGTH + 1 ];
144
+ Arrays .fill (longName , 'a' );
145
+ String dimensionValue = String .valueOf (longName );
146
+
140
147
assertThrows (
141
148
InvalidDimensionException .class ,
142
149
() -> DimensionSet .of (dimensionName , dimensionValue ));
@@ -275,6 +282,19 @@ void flush_doesNotPreserveDimensions()
275
282
expectDimension ("Name" , null );
276
283
}
277
284
285
+ @ Test
286
+ void whenMultipleFlush_withNoDimensionsChanges_keepDefaultDimensions ()
287
+ throws InvalidMetricException , DimensionSetExceededException {
288
+ logger .putMetric ("Count" , 1 );
289
+
290
+ logger .flush ();
291
+ logger .flush ();
292
+
293
+ expectDimension ("LogGroup" , "test-log-group" );
294
+ expectDimension ("ServiceName" , "test-env-name" );
295
+ expectDimension ("ServiceType" , "test-env-type" );
296
+ }
297
+
278
298
@ Test
279
299
void setDimensions_clearsAllDimensions () throws DimensionSetExceededException {
280
300
MetricsLogger logger = new MetricsLogger (envProvider );
@@ -302,7 +322,10 @@ void whenSetDimensions_withMultipleFlush_thenClearsDimensions()
302
322
303
323
@ Test
304
324
void whenPutMetric_withTooLongName_thenThrowInvalidMetricException () {
305
- String name1 = "a" .repeat (Constants .MAX_METRIC_NAME_LENGTH + 1 );
325
+ char [] longName = new char [Constants .MAX_METRIC_NAME_LENGTH + 1 ];
326
+ Arrays .fill (longName , 'a' );
327
+ String name1 = new String (longName );
328
+
306
329
assertThrows (InvalidMetricException .class , () -> logger .putMetric (name1 , 1 ));
307
330
}
308
331
@@ -349,7 +372,10 @@ void whenSetNamespace_withInvalidValue_thenThrowInvalidNamespaceException(String
349
372
350
373
@ Test
351
374
void whenSetNamespace_withNameTooLong_thenThrowInvalidNamespaceException () {
352
- String namespace = "a" .repeat (Constants .MAX_NAMESPACE_LENGTH + 1 );
375
+ char [] longName = new char [Constants .MAX_NAMESPACE_LENGTH + 1 ];
376
+ Arrays .fill (longName , 'a' );
377
+ String namespace = new String (longName );
378
+
353
379
assertThrows (InvalidNamespaceException .class , () -> logger .setNamespace (namespace ));
354
380
}
355
381
0 commit comments