### Question:
CryptoJobHandler implements Closeable { public void close() { if (enabled.compareAndSet(true, false)) { List<Runnable> pendingTasks = scheduler.shutdownNow(); for (Runnable task : pendingTasks) { if (task instanceof CryptoJob) { ((CryptoJob) task).closeJob(CLOSED_EXCEPTION); } else { logger.error("Unknown type of job seen : {}", task.getClass()); } } try { scheduler.awaitTermination(1000, TimeUnit.MILLISECONDS); } catch (Exception e) { logger.error("Unexpected exception while waiting for crypto jobs to terminate", e); } } } CryptoJobHandler(int threadCount); void close();  }### Answer:
@Test public void testCryptoJobExecutorServiceDiffThreadCount() throws GeneralSecurityException, InterruptedException { int totalDataCount = 10; for (int j = 0; j < 5; j++) { cryptoJobHandler.close(); cryptoJobHandler = new CryptoJobHandler(j + 1); CountDownLatch encryptCallBackCount = new CountDownLatch(totalDataCount); CountDownLatch decryptCallBackCount = new CountDownLatch(totalDataCount); SecretKeySpec perBlobKey = kms.getRandomKey(); for (int i = 0; i < totalDataCount; i++) { testEncryptDecryptFlow(perBlobKey, encryptCallBackCount, decryptCallBackCount, Mode.Both); } awaitCountDownLatch(encryptCallBackCount, ENCRYPT_JOB_TYPE); awaitCountDownLatch(decryptCallBackCount, DECRYPT_JOB_TYPE); } }
@Test public void testEncryptionFailure() throws InterruptedException, GeneralSecurityException { cryptoJobHandler.close(); MockCryptoService mockCryptoService = new MockCryptoService(new CryptoServiceConfig(verifiableProperties)); mockCryptoService.exceptionOnEncryption.set( new GeneralSecurityException("Exception to test", new IllegalStateException())); cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT); SecretKeySpec perBlobSecretKey = kms.getRandomKey(); testFailureOnEncryption(perBlobSecretKey, mockCryptoService, kms); mockCryptoService.clearStates(); cryptoJobHandler.close(); MockKeyManagementService mockKms = new MockKeyManagementService(new KMSConfig(verifiableProperties), defaultKey); mockKms.exceptionToThrow.set(new GeneralSecurityException("Exception to test", new IllegalStateException())); cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT); testFailureOnEncryption(perBlobSecretKey, cryptoService, mockKms); }
@Test public void testDecryptionFailure() throws InterruptedException, GeneralSecurityException { cryptoJobHandler.close(); MockCryptoService mockCryptoService = new MockCryptoService(new CryptoServiceConfig(verifiableProperties)); mockCryptoService.exceptionOnDecryption.set( new GeneralSecurityException("Exception to test", new IllegalStateException())); cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT); SecretKeySpec perBlobSecretKey = kms.getRandomKey(); testFailureOnDecryption(perBlobSecretKey, null, false, mockCryptoService, kms); mockCryptoService.clearStates(); cryptoJobHandler.close(); MockKeyManagementService mockKms = new MockKeyManagementService(new KMSConfig(verifiableProperties), defaultKey); cryptoJobHandler = new CryptoJobHandler(DEFAULT_THREAD_COUNT); testFailureOnDecryption(perBlobSecretKey, mockKms, true, cryptoService, mockKms); }