5
5
6
6
package com .aws .greengrass .integrationtests .ipc ;
7
7
8
+ import com .aws .greengrass .clientdevices .auth .infra .CDAExecutor ;
8
9
import com .aws .greengrass .dependency .State ;
9
10
import com .aws .greengrass .clientdevices .auth .ClientDevicesAuthService ;
10
11
import com .aws .greengrass .clientdevices .auth .exception .AuthenticationException ;
27
28
import org .junit .jupiter .api .extension .ExtensionContext ;
28
29
import org .junit .jupiter .api .io .TempDir ;
29
30
import org .mockito .Mock ;
31
+ import org .mockito .Mockito ;
30
32
import org .mockito .junit .jupiter .MockitoExtension ;
31
33
import software .amazon .awssdk .aws .greengrass .GetClientDeviceAuthTokenResponseHandler ;
32
34
import software .amazon .awssdk .aws .greengrass .GreengrassCoreIPCClient ;
47
49
import java .util .Optional ;
48
50
import java .util .concurrent .CompletableFuture ;
49
51
import java .util .concurrent .CountDownLatch ;
52
+ import java .util .concurrent .LinkedBlockingQueue ;
53
+ import java .util .concurrent .ThreadPoolExecutor ;
50
54
import java .util .concurrent .TimeUnit ;
51
55
import java .util .function .Consumer ;
52
56
53
- import static com .aws .greengrass .componentmanager .KernelConfigResolver .CONFIGURATION_CONFIG_KEY ;
54
- import static com .aws .greengrass .clientdevices .auth .ClientDevicesAuthService .CLOUD_REQUEST_QUEUE_SIZE_TOPIC ;
55
- import static com .aws .greengrass .clientdevices .auth .ClientDevicesAuthService .PERFORMANCE_TOPIC ;
56
57
import static com .aws .greengrass .testcommons .testutilities .ExceptionLogProtector .ignoreExceptionOfType ;
57
58
import static com .aws .greengrass .testcommons .testutilities .TestUtils .asyncAssertOnConsumer ;
58
59
import static org .hamcrest .MatcherAssert .assertThat ;
59
60
import static org .hamcrest .Matchers .containsString ;
60
61
import static org .hamcrest .Matchers .is ;
61
62
import static org .junit .jupiter .api .Assertions .assertEquals ;
62
63
import static org .junit .jupiter .api .Assertions .assertThrows ;
63
- import static org .mockito .ArgumentMatchers .anyMap ;
64
- import static org .mockito .ArgumentMatchers .anyString ;
64
+ import static org .mockito .ArgumentMatchers .any ;
65
65
import static org .mockito .Mockito .when ;
66
66
67
67
@ ExtendWith ({GGExtension .class , UniqueRootPathExtension .class , MockitoExtension .class })
@@ -159,8 +159,15 @@ void GIVEN_brokerWithValidCredentials_WHEN_GetClientDeviceAuthToken_THEN_returns
159
159
@ Test
160
160
void GIVEN_brokerWithInvalidCredentials_WHEN_GetClientDeviceAuthToken_THEN_throwsInvalidArgumentsError_and_WHEN_queueIsFull_THEN_throwsServiceError ()
161
161
throws Exception {
162
+ // Inject work queue which will reject any work added
163
+ LinkedBlockingQueue <Runnable > mockQueue = Mockito .mock (LinkedBlockingQueue .class );
164
+ when (mockQueue .offer (any ())).thenReturn (false );
165
+ ThreadPoolExecutor executor = new ThreadPoolExecutor (1 , 1 , 1 , TimeUnit .SECONDS , mockQueue );
166
+ kernel .getContext ().put (CDAExecutor .class , new CDAExecutor (executor ));
162
167
kernel .getContext ().put (SessionManager .class , sessionManager );
168
+
163
169
startNucleusWithConfig ("cda.yaml" );
170
+
164
171
try (EventStreamRPCConnection connection = IPCTestUtils .getEventStreamRpcConnection (kernel ,
165
172
"BrokerWithGetClientDeviceAuthTokenPermission" )) {
166
173
GreengrassCoreIPCClient ipcClient = new GreengrassCoreIPCClient (connection );
@@ -174,11 +181,6 @@ void GIVEN_brokerWithInvalidCredentials_WHEN_GetClientDeviceAuthToken_THEN_throw
174
181
assertThat (err .getCause ().getMessage (), containsString ("Invalid client device credentials" ));
175
182
}
176
183
177
- // Update the cloud queue size to 1 so that we'll just reject the second request
178
- kernel .findServiceTopic (ClientDevicesAuthService .CLIENT_DEVICES_AUTH_SERVICE_NAME )
179
- .lookup (CONFIGURATION_CONFIG_KEY , PERFORMANCE_TOPIC , CLOUD_REQUEST_QUEUE_SIZE_TOPIC ).withValue (1 );
180
- kernel .getContext ().waitForPublishQueueToClear ();
181
-
182
184
// Verify that we get a good error that the request couldn't be queued
183
185
try (EventStreamRPCConnection connection = IPCTestUtils .getEventStreamRpcConnection (kernel ,
184
186
"BrokerWithGetClientDeviceAuthTokenPermission" )) {
@@ -187,27 +189,10 @@ void GIVEN_brokerWithInvalidCredentials_WHEN_GetClientDeviceAuthToken_THEN_throw
187
189
new CredentialDocument ().withMqttCredential (
188
190
new MQTTCredential ().withClientId ("some-client-id" ).withCertificatePem ("VALID PEM" )));
189
191
190
- CountDownLatch cdl = new CountDownLatch (1 );
191
- when (sessionManager .createSession (anyString (), anyMap ())).thenAnswer ((a ) -> {
192
- cdl .countDown ();
193
- Thread .sleep (1_000 ); // slow down the first request so that the second will be rejected
194
- return "uuid" ;
195
- });
196
- // Request 1 (immediately runs)
197
- CompletableFuture <GetClientDeviceAuthTokenResponse > fut1 =
198
- ipcClient .getClientDeviceAuthToken (request , Optional .empty ()).getResponse ();
199
- // Ensure the threadpool is actively blocked before we send the next requests to fill the queue and then
200
- // overflow the queue.
201
- cdl .await (2 , TimeUnit .SECONDS );
202
- // Request 2 (queued so that queue size is 1)
203
- CompletableFuture <GetClientDeviceAuthTokenResponse > fut2 =
204
- ipcClient .getClientDeviceAuthToken (request , Optional .empty ()).getResponse ();
205
- // Request 3 (expect rejection)
192
+ // Expect rejection
206
193
Exception err = assertThrows (Exception .class , () -> clientDeviceAuthToken (ipcClient , request , (r ) -> {}));
207
194
assertThat (err .getCause ().getMessage (), containsString ("Unable to queue request" ));
208
195
assertEquals (ServiceError .class , err .getCause ().getClass ());
209
- fut1 .get (2 , TimeUnit .SECONDS );
210
- fut2 .get (2 , TimeUnit .SECONDS );
211
196
}
212
197
}
213
198
0 commit comments