1919import static com .google .common .truth .Truth .assertThat ;
2020
2121import com .google .common .collect .Lists ;
22+ import io .grpc .Context ;
23+ import io .opencensus .common .Scope ;
2224import io .opencensus .tags .Tag ;
2325import io .opencensus .tags .Tag .TagBoolean ;
2426import io .opencensus .tags .Tag .TagLong ;
2527import io .opencensus .tags .Tag .TagString ;
2628import io .opencensus .tags .TagContext ;
29+ import io .opencensus .tags .TagContextBuilder ;
2730import io .opencensus .tags .TagContexts ;
2831import io .opencensus .tags .TagKey .TagKeyBoolean ;
2932import io .opencensus .tags .TagKey .TagKeyLong ;
3235import io .opencensus .tags .TagValue .TagValueLong ;
3336import io .opencensus .tags .TagValue .TagValueString ;
3437import io .opencensus .tags .UnreleasedApiAccessor ;
38+ import io .opencensus .tags .unsafe .ContextUtils ;
3539import java .util .Collections ;
3640import java .util .Iterator ;
3741import java .util .List ;
38- import org .junit .Rule ;
3942import org .junit .Test ;
40- import org .junit .rules .ExpectedException ;
4143import org .junit .runner .RunWith ;
4244import org .junit .runners .JUnit4 ;
4345
@@ -55,44 +57,118 @@ public class TagContextsImplTest {
5557 private static final TagValueLong VL = TagValueLong .create (10L );
5658 private static final TagValueBoolean VB = TagValueBoolean .create (false );
5759
58- @ Rule public final ExpectedException thrown = ExpectedException .none ();
60+ private static final Tag TAG1 = TagString .create (KS , VS1 );
61+ private static final Tag TAG2 = TagLong .create (KL , VL );
62+ private static final Tag TAG3 = TagBoolean .create (KB , VB );
5963
6064 @ Test
6165 public void empty () {
6266 assertThat (asList (tagContexts .empty ())).isEmpty ();
67+ assertThat (tagContexts .empty ()).isInstanceOf (TagContextImpl .class );
6368 }
6469
6570 @ Test
6671 public void emptyBuilder () {
67- assertThat (asList (tagContexts .emptyBuilder ().build ())).isEmpty ();
72+ TagContextBuilder builder = tagContexts .emptyBuilder ();
73+ assertThat (builder ).isInstanceOf (TagContextBuilderImpl .class );
74+ assertThat (asList (builder .build ())).isEmpty ();
6875 }
6976
7077 @ Test
7178 public void toBuilder_ConvertUnknownTagContextToTagContextImpl () {
72- Tag tag1 = TagString .create (KS , VS1 );
73- Tag tag2 = TagLong .create (KL , VL );
74- Tag tag3 = TagBoolean .create (KB , VB );
75- TagContext unknownTagContext = new SimpleTagContext (tag1 , tag2 , tag3 );
79+ TagContext unknownTagContext = new SimpleTagContext (TAG1 , TAG2 , TAG3 );
7680 TagContext newTagContext = tagContexts .toBuilder (unknownTagContext ).build ();
77- assertThat (asList (newTagContext )).containsExactly (tag1 , tag2 , tag3 );
81+ assertThat (asList (newTagContext )).containsExactly (TAG1 , TAG2 , TAG3 );
7882 assertThat (newTagContext ).isInstanceOf (TagContextImpl .class );
7983 }
8084
8185 @ Test
8286 public void toBuilder_RemoveDuplicatesFromUnknownTagContext () {
8387 Tag tag1 = TagString .create (KS , VS1 );
8488 Tag tag2 = TagString .create (KS , VS2 );
85- TagContext unknownTagContext = new SimpleTagContext (tag1 , tag2 );
86- TagContext newTagContext = tagContexts .toBuilder (unknownTagContext ).build ();
89+ TagContext tagContextWithDuplicateTags = new SimpleTagContext (tag1 , tag2 );
90+ TagContext newTagContext = tagContexts .toBuilder (tagContextWithDuplicateTags ).build ();
8791 assertThat (asList (newTagContext )).containsExactly (tag2 );
8892 }
8993
9094 @ Test
91- public void toBuilder_HandleNullTag () {
92- TagContext unknownTagContext =
93- new SimpleTagContext (TagString .create (KS , VS2 ), null , TagLong .create (KL , VL ));
94- thrown .expect (NullPointerException .class );
95- tagContexts .toBuilder (unknownTagContext ).build ();
95+ public void toBuilder_SkipNullTag () {
96+ TagContext tagContextWithNullTag = new SimpleTagContext (TAG1 , null , TAG2 );
97+ TagContext newTagContext = tagContexts .toBuilder (tagContextWithNullTag ).build ();
98+ assertThat (asList (newTagContext )).containsExactly (TAG1 , TAG2 );
99+ }
100+
101+ @ Test
102+ public void getCurrentTagContext_DefaultIsEmptyTagContextImpl () {
103+ TagContext currentTagContext = tagContexts .getCurrentTagContext ();
104+ assertThat (asList (currentTagContext )).isEmpty ();
105+ assertThat (currentTagContext ).isInstanceOf (TagContextImpl .class );
106+ }
107+
108+ @ Test
109+ public void getCurrentTagContext_ConvertUnknownTagContextToTagContextImpl () {
110+ TagContext unknownTagContext = new SimpleTagContext (TAG1 , TAG2 , TAG3 );
111+ TagContext result = getResultOfGetCurrentTagContext (unknownTagContext );
112+ assertThat (result ).isInstanceOf (TagContextImpl .class );
113+ assertThat (asList (result )).containsExactly (TAG1 , TAG2 , TAG3 );
114+ }
115+
116+ @ Test
117+ public void getCurrentTagContext_RemoveDuplicatesFromUnknownTagContext () {
118+ Tag tag1 = TagString .create (KS , VS1 );
119+ Tag tag2 = TagString .create (KS , VS2 );
120+ TagContext tagContextWithDuplicateTags = new SimpleTagContext (tag1 , tag2 );
121+ TagContext result = getResultOfGetCurrentTagContext (tagContextWithDuplicateTags );
122+ assertThat (asList (result )).containsExactly (tag2 );
123+ }
124+
125+ @ Test
126+ public void getCurrentTagContext_SkipNullTag () {
127+ TagContext tagContextWithNullTag = new SimpleTagContext (TAG1 , null , TAG2 );
128+ TagContext result = getResultOfGetCurrentTagContext (tagContextWithNullTag );
129+ assertThat (asList (result )).containsExactly (TAG1 , TAG2 );
130+ }
131+
132+ private TagContext getResultOfGetCurrentTagContext (TagContext tagsToSet ) {
133+ Context orig = Context .current ().withValue (ContextUtils .TAG_CONTEXT_KEY , tagsToSet ).attach ();
134+ try {
135+ return tagContexts .getCurrentTagContext ();
136+ } finally {
137+ Context .current ().detach (orig );
138+ }
139+ }
140+
141+ @ Test
142+ public void withTagContext_ConvertUnknownTagContextToTagContextImpl () {
143+ TagContext unknownTagContext = new SimpleTagContext (TAG1 , TAG2 , TAG3 );
144+ TagContext result = getResultOfWithTagContext (unknownTagContext );
145+ assertThat (result ).isInstanceOf (TagContextImpl .class );
146+ assertThat (asList (result )).containsExactly (TAG1 , TAG2 , TAG3 );
147+ }
148+
149+ @ Test
150+ public void withTagContext_RemoveDuplicatesFromUnknownTagContext () {
151+ Tag tag1 = TagString .create (KS , VS1 );
152+ Tag tag2 = TagString .create (KS , VS2 );
153+ TagContext tagContextWithDuplicateTags = new SimpleTagContext (tag1 , tag2 );
154+ TagContext result = getResultOfWithTagContext (tagContextWithDuplicateTags );
155+ assertThat (asList (result )).containsExactly (tag2 );
156+ }
157+
158+ @ Test
159+ public void withTagContext_SkipNullTag () {
160+ TagContext tagContextWithNullTag = new SimpleTagContext (TAG1 , null , TAG2 );
161+ TagContext result = getResultOfWithTagContext (tagContextWithNullTag );
162+ assertThat (asList (result )).containsExactly (TAG1 , TAG2 );
163+ }
164+
165+ private TagContext getResultOfWithTagContext (TagContext tagsToSet ) {
166+ Scope scope = tagContexts .withTagContext (tagsToSet );
167+ try {
168+ return ContextUtils .TAG_CONTEXT_KEY .get ();
169+ } finally {
170+ scope .close ();
171+ }
96172 }
97173
98174 private static List <Tag > asList (TagContext tags ) {
0 commit comments