1919import static com .google .common .truth .Truth .assertThat ;
2020
2121import io .opencensus .common .Timestamp ;
22+ import io .opencensus .implcore .internal .CurrentState ;
23+ import io .opencensus .implcore .tags .TagMapImpl ;
24+ import io .opencensus .metrics .data .AttachmentValue ;
25+ import io .opencensus .stats .Aggregation ;
26+ import io .opencensus .stats .Aggregation .Count ;
27+ import io .opencensus .stats .Aggregation .Distribution ;
28+ import io .opencensus .stats .BucketBoundaries ;
29+ import io .opencensus .stats .Measure .MeasureDouble ;
30+ import io .opencensus .stats .View ;
31+ import io .opencensus .stats .ViewData ;
32+ import io .opencensus .tags .TagKey ;
33+ import java .util .Arrays ;
34+ import java .util .Collections ;
2235import org .junit .Test ;
2336import org .junit .runner .RunWith ;
2437import org .junit .runners .JUnit4 ;
@@ -31,4 +44,53 @@ public class MutableViewDataTest {
3144 public void testConstants () {
3245 assertThat (MutableViewData .ZERO_TIMESTAMP ).isEqualTo (Timestamp .create (0 , 0 ));
3346 }
47+
48+ @ Test
49+ public void testTimeRewindsOnCountViewNoThrow () {
50+ // First we set up some buckets THEN we rewind time for giggles.
51+ View tester =
52+ View .create (
53+ View .Name .create ("view" ),
54+ "Description" ,
55+ MeasureDouble .create ("name" , "desc" , "us" ),
56+ Count .create (),
57+ Collections .singletonList (TagKey .create ("KEY" )));
58+ Timestamp start = Timestamp .create (10000000 , 0 );
59+ Timestamp validPointTime = Timestamp .create (10000010 , 0 );
60+ CurrentState .State state = CurrentState .State .ENABLED ;
61+ MutableViewData viewData = MutableViewData .create (tester , start );
62+ // Create a data points to get thrown away.
63+ viewData .record (
64+ TagMapImpl .EMPTY , 1.0 , validPointTime , Collections .<String , AttachmentValue >emptyMap ());
65+ // Rewind time and look for explosions.
66+ Timestamp thePast = Timestamp .create (0 , 0 );
67+ ViewData result = viewData .toViewData (thePast , state );
68+ assertThat (result .getAggregationMap ()).isEmpty ();
69+ }
70+
71+ @ Test
72+ public void testTimeRewindsOnDistributionViewNoThrow () {
73+ // First we set up some buckets THEN we rewind time for giggles.
74+ Aggregation latencyDistribution =
75+ Distribution .create (
76+ BucketBoundaries .create (Arrays .asList (0.0 , 25.0 , 100.0 , 200.0 , 400.0 , 800.0 , 10000.0 )));
77+ View tester =
78+ View .create (
79+ View .Name .create ("view" ),
80+ "Description" ,
81+ MeasureDouble .create ("name" , "desc" , "us" ),
82+ latencyDistribution ,
83+ Collections .singletonList (TagKey .create ("KEY" )));
84+ Timestamp start = Timestamp .create (10000000 , 0 );
85+ Timestamp validPointTime = Timestamp .create (10000010 , 0 );
86+ CurrentState .State state = CurrentState .State .ENABLED ;
87+ MutableViewData viewData = MutableViewData .create (tester , start );
88+ // Create a data points to get thrown away.
89+ viewData .record (
90+ TagMapImpl .EMPTY , 1.0 , validPointTime , Collections .<String , AttachmentValue >emptyMap ());
91+ // Rewind time and look for explosions.
92+ Timestamp thePast = Timestamp .create (0 , 0 );
93+ ViewData result = viewData .toViewData (thePast , state );
94+ assertThat (result .getAggregationMap ()).isEmpty ();
95+ }
3496}
0 commit comments