Created
June 26, 2020 19:58
-
-
Save bbpennel/c5684c114586c257e57e54bbfcb0ee0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// snippet from FedoraTransactionsIT | |
@Test | |
public void testConcurrentPutsAndCommits() throws Exception { | |
final String baseId = getRandomUniqueId(); | |
final HttpPut putBase = new HttpPut(serverAddress + "/" + baseId); | |
try (final CloseableHttpResponse response = execute(putBase)) { | |
assertEquals(CREATED.getStatusCode(), getStatus(response)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
List<String> txIds = new ArrayList<>(); | |
List<Thread> threads = new ArrayList<>(); | |
for (int i = 0; i < 10; i++) { | |
final String txLocation; | |
final HttpPost createTx = new HttpPost(serverAddress + "fcr:tx"); | |
try (final CloseableHttpResponse response = execute(createTx)) { | |
assertEquals(CREATED.getStatusCode(), getStatus(response)); | |
txLocation = getLocation(response); | |
txIds.add(txLocation); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
final String objId = baseId + "/path/to/obj" + i; | |
final HttpPut putNew = new HttpPut(txLocation + "/" + objId); | |
try (CloseableHttpResponse resp = execute(putNew)) { | |
assertEquals(CREATED.getStatusCode(), getStatus(resp)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
} | |
for (final String txLocation : txIds) { | |
Runnable putAndCommitThread = new Runnable() { | |
@Override | |
public void run() { | |
try { | |
Thread.sleep((long) (new Random().nextFloat() * 10)); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
assertEquals(NO_CONTENT.getStatusCode(), getStatus(new HttpPost(txLocation + "/fcr:tx/fcr:commit"))); | |
} | |
}; | |
Thread thread = new Thread(putAndCommitThread); | |
threads.add(thread); | |
} | |
for (Thread thread : threads) { | |
thread.start(); | |
} | |
for (Thread thread : threads) { | |
thread.join(); | |
} | |
System.out.println("All done"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment