### Question:
KeyPairsRepositoryService { @Transactional(isolation = Isolation.READ_COMMITTED) public Optional<KeyPairs> getFreshKey() { try { return this.keyPairsRepository .findFirstOptionalByAvailableOrderByIdAsc(new Boolean(true)) .map((k) -> k.setAvailable(new Boolean(false))) .map((k) -> this.keyPairsRepository.save(k)); } catch (Exception e) { LOG.error("Error obtaining a fresh key.", e); return Optional.empty(); } } @Autowired  KeyPairsRepositoryService(KeyPairsRepository keyPairsRepository); @Transactional(isolation = Isolation.READ_COMMITTED) Optional<KeyPairs> getFreshKey(); boolean addKeyPairsIfNotPresent(KeyPairs keyPairs); Optional<KeyPairs> findByPublicEth(String publicEth); Optional<KeyPairs> findByPublicBtc(String publicBtc); Optional<KeyPairs> findById(long id);  }### Answer:
@Test public void testGetFreshKey() throws InterruptedException { List<Optional<KeyPairs>> optionalKeyPairsList = Collections.synchronizedList(new ArrayList<Optional<KeyPairs>>()); ThreadTestUtils.runMultiThread( () -> optionalKeyPairsList.add(keyPairsRepositoryService.getFreshKey()), 10 ); assertEquals(10, optionalKeyPairsList.size()); assertEquals(10, optionalKeyPairsList.stream().filter((k) -> k.isPresent()).count()); List<KeyPairs> keyPairsList = optionalKeyPairsList.stream() .map((k) -> k.get()) .collect(Collectors.toList()); assertTrue(allBTCAreUnique(10, keyPairsList)); assertTrue(allETHAreUnique(10, keyPairsList)); assertTrue(allMarkedAsUnavailable(keyPairsList)); }