41
41
import com .google .api .gax .rpc .testing .FakeCallContext ;
42
42
import com .google .api .gax .rpc .testing .MockStreamingApi .MockServerStreamingCall ;
43
43
import com .google .api .gax .rpc .testing .MockStreamingApi .MockServerStreamingCallable ;
44
+ import com .google .api .gax .tracing .NoopApiTracer ;
44
45
import com .google .common .collect .Queues ;
45
46
import com .google .common .truth .Truth ;
46
47
import java .util .concurrent .BlockingDeque ;
51
52
import org .junit .Test ;
52
53
import org .junit .runner .RunWith ;
53
54
import org .junit .runners .JUnit4 ;
55
+ import org .mockito .Mockito ;
54
56
import org .threeten .bp .Duration ;
55
57
56
58
@ RunWith (JUnit4 .class )
@@ -59,29 +61,107 @@ public class ServerStreamingAttemptCallableTest {
59
61
private AccumulatingObserver observer ;
60
62
private FakeRetryingFuture fakeRetryingFuture ;
61
63
private StreamResumptionStrategy <String , String > resumptionStrategy ;
64
+ private static Duration totalTimeout = Duration .ofHours (1 );
65
+ private FakeCallContext mockedCallContext ;
62
66
63
67
@ Before
64
68
public void setUp () {
65
69
innerCallable = new MockServerStreamingCallable <>();
66
70
observer = new AccumulatingObserver (true );
67
71
resumptionStrategy = new MyStreamResumptionStrategy ();
72
+ mockedCallContext = Mockito .mock (FakeCallContext .class );
68
73
}
69
74
70
75
private ServerStreamingAttemptCallable <String , String > createCallable () {
76
+ return createCallable (FakeCallContext .createDefault ());
77
+ }
78
+
79
+ private ServerStreamingAttemptCallable <String , String > createCallable (ApiCallContext context ) {
71
80
ServerStreamingAttemptCallable <String , String > callable =
72
81
new ServerStreamingAttemptCallable <>(
73
- innerCallable ,
74
- resumptionStrategy ,
75
- "request" ,
76
- FakeCallContext .createDefault (),
77
- observer );
82
+ innerCallable , resumptionStrategy , "request" , context , observer );
78
83
79
84
fakeRetryingFuture = new FakeRetryingFuture (callable );
80
85
callable .setExternalFuture (fakeRetryingFuture );
81
86
82
87
return callable ;
83
88
}
84
89
90
+ @ Test
91
+ public void testUserProvidedContextTimeout () {
92
+ // Mock up the ApiCallContext as if the user provided a timeout and streamWaitTimeout.
93
+ Mockito .doReturn (NoopApiTracer .getInstance ()).when (mockedCallContext ).getTracer ();
94
+ Mockito .doReturn (Duration .ofHours (5 )).when (mockedCallContext ).getTimeout ();
95
+ Mockito .doReturn (Duration .ofHours (5 )).when (mockedCallContext ).getStreamWaitTimeout ();
96
+
97
+ ServerStreamingAttemptCallable <String , String > callable = createCallable (mockedCallContext );
98
+ callable .start ();
99
+
100
+ // Ensure that the callable did not overwrite the user provided timeouts
101
+ Mockito .verify (mockedCallContext , Mockito .times (1 )).getTimeout ();
102
+ Mockito .verify (mockedCallContext , Mockito .never ()).withTimeout (totalTimeout );
103
+ Mockito .verify (mockedCallContext , Mockito .times (1 )).getStreamWaitTimeout ();
104
+ Mockito .verify (mockedCallContext , Mockito .never ())
105
+ .withStreamWaitTimeout (Mockito .any (Duration .class ));
106
+
107
+ // Should notify outer observer
108
+ Truth .assertThat (observer .controller ).isNotNull ();
109
+
110
+ // Should configure the inner controller correctly.
111
+ MockServerStreamingCall <String , String > call = innerCallable .popLastCall ();
112
+ Truth .assertThat (call .getController ().isAutoFlowControlEnabled ()).isTrue ();
113
+ Truth .assertThat (call .getRequest ()).isEqualTo ("request" );
114
+
115
+ // Send a response in auto flow mode.
116
+ call .getController ().getObserver ().onResponse ("response1" );
117
+ call .getController ().getObserver ().onResponse ("response2" );
118
+ call .getController ().getObserver ().onComplete ();
119
+
120
+ // Make sure the responses are received
121
+ Truth .assertThat (observer .responses ).containsExactly ("response1" , "response2" ).inOrder ();
122
+ fakeRetryingFuture .assertSuccess ();
123
+ }
124
+
125
+ @ Test
126
+ public void testNoUserProvidedContextTimeout () {
127
+ // Mock up the ApiCallContext as if the user did not provide custom timeouts.
128
+ Mockito .doReturn (NoopApiTracer .getInstance ()).when (mockedCallContext ).getTracer ();
129
+ Mockito .doReturn (null ).when (mockedCallContext ).getTimeout ();
130
+ Mockito .doReturn (null ).when (mockedCallContext ).getStreamWaitTimeout ();
131
+ Mockito .doReturn (mockedCallContext ).when (mockedCallContext ).withTimeout (totalTimeout );
132
+ Mockito .doReturn (mockedCallContext )
133
+ .when (mockedCallContext )
134
+ .withStreamWaitTimeout (Mockito .any (Duration .class ));
135
+
136
+ ServerStreamingAttemptCallable <String , String > callable = createCallable (mockedCallContext );
137
+ callable .start ();
138
+
139
+ // Ensure that the callable configured the timeouts via the Settings in the
140
+ // absence of user-defined timeouts.
141
+ Mockito .verify (mockedCallContext , Mockito .times (1 )).getTimeout ();
142
+ Mockito .verify (mockedCallContext , Mockito .times (1 )).withTimeout (totalTimeout );
143
+ Mockito .verify (mockedCallContext , Mockito .times (1 )).getStreamWaitTimeout ();
144
+ Mockito .verify (mockedCallContext , Mockito .times (1 ))
145
+ .withStreamWaitTimeout (Mockito .any (Duration .class ));
146
+
147
+ // Should notify outer observer
148
+ Truth .assertThat (observer .controller ).isNotNull ();
149
+
150
+ // Should configure the inner controller correctly.
151
+ MockServerStreamingCall <String , String > call = innerCallable .popLastCall ();
152
+ Truth .assertThat (call .getController ().isAutoFlowControlEnabled ()).isTrue ();
153
+ Truth .assertThat (call .getRequest ()).isEqualTo ("request" );
154
+
155
+ // Send a response in auto flow mode.
156
+ call .getController ().getObserver ().onResponse ("response1" );
157
+ call .getController ().getObserver ().onResponse ("response2" );
158
+ call .getController ().getObserver ().onComplete ();
159
+
160
+ // Make sure the responses are received
161
+ Truth .assertThat (observer .responses ).containsExactly ("response1" , "response2" ).inOrder ();
162
+ fakeRetryingFuture .assertSuccess ();
163
+ }
164
+
85
165
@ Test
86
166
public void testNoErrorsAutoFlow () {
87
167
ServerStreamingAttemptCallable <String , String > callable = createCallable ();
@@ -396,8 +476,7 @@ private static class FakeRetryingFuture extends AbstractApiFuture<Void>
396
476
this .attemptCallable = attemptCallable ;
397
477
attemptSettings =
398
478
TimedAttemptSettings .newBuilder ()
399
- .setGlobalSettings (
400
- RetrySettings .newBuilder ().setTotalTimeout (Duration .ofHours (1 )).build ())
479
+ .setGlobalSettings (RetrySettings .newBuilder ().setTotalTimeout (totalTimeout ).build ())
401
480
.setFirstAttemptStartTimeNanos (0 )
402
481
.setAttemptCount (0 )
403
482
.setOverallAttemptCount (0 )
0 commit comments