2929 */
3030package com .google .api .gax .tracing ;
3131
32+ import static org .junit .jupiter .api .Assertions .assertEquals ;
33+ import static org .junit .jupiter .api .Assertions .assertThrows ;
34+ import static org .junit .jupiter .api .Assertions .assertTrue ;
35+ import static org .mockito .Mockito .doThrow ;
36+ import static org .mockito .Mockito .inOrder ;
3237import static org .mockito .Mockito .mock ;
3338import static org .mockito .Mockito .verify ;
3439import static org .mockito .Mockito .when ;
3843import java .util .Map ;
3944import org .junit .jupiter .api .BeforeEach ;
4045import org .junit .jupiter .api .Test ;
46+ import org .mockito .InOrder ;
4147
4248class CompositeTracerTest {
4349
@@ -53,7 +59,7 @@ void setUp() {
5359 }
5460
5561 @ Test
56- void testInScope () {
62+ void testInScope_lifoOrder () {
5763 ApiTracer .Scope scope1 = mock (ApiTracer .Scope .class );
5864 ApiTracer .Scope scope2 = mock (ApiTracer .Scope .class );
5965
@@ -63,10 +69,54 @@ void testInScope() {
6369 ApiTracer .Scope compositeScope = compositeTracer .inScope ();
6470 compositeScope .close ();
6571
72+ verify (child1 ).inScope ();
73+ verify (child2 ).inScope ();
74+
75+ InOrder inOrder = inOrder (scope2 , scope1 );
76+ inOrder .verify (scope2 ).close ();
77+ inOrder .verify (scope1 ).close ();
78+ }
79+
80+ @ Test
81+ void testInScope_childInScopeThrows () {
82+ ApiTracer .Scope scope1 = mock (ApiTracer .Scope .class );
83+ RuntimeException exception = new RuntimeException ("Runtime Error" );
84+
85+ when (child1 .inScope ()).thenReturn (scope1 );
86+ when (child2 .inScope ()).thenThrow (exception );
87+
88+ RuntimeException thrown = assertThrows (RuntimeException .class , () -> compositeTracer .inScope ());
89+
90+ assertEquals (exception , thrown );
6691 verify (child1 ).inScope ();
6792 verify (child2 ).inScope ();
6893 verify (scope1 ).close ();
69- verify (scope2 ).close ();
94+ }
95+
96+ @ Test
97+ void testInScope_childScopeCloseThrows () {
98+ ApiTracer .Scope scope1 = mock (ApiTracer .Scope .class );
99+ ApiTracer .Scope scope2 = mock (ApiTracer .Scope .class );
100+
101+ RuntimeException exception2 = new RuntimeException ("Scope 2 close Error" );
102+ RuntimeException exception1 = new RuntimeException ("Scope 1 close Error" );
103+
104+ when (child1 .inScope ()).thenReturn (scope1 );
105+ when (child2 .inScope ()).thenReturn (scope2 );
106+
107+ doThrow (exception2 ).when (scope2 ).close ();
108+ doThrow (exception1 ).when (scope1 ).close ();
109+
110+ ApiTracer .Scope compositeScope = compositeTracer .inScope ();
111+
112+ RuntimeException thrown = assertThrows (RuntimeException .class , () -> compositeScope .close ());
113+
114+ assertEquals (exception2 , thrown );
115+ assertTrue (Arrays .asList (thrown .getSuppressed ()).contains (exception1 ));
116+
117+ InOrder inOrder = inOrder (scope2 , scope1 );
118+ inOrder .verify (scope2 ).close ();
119+ inOrder .verify (scope1 ).close ();
70120 }
71121
72122 @ Test
0 commit comments