/
DeletionCollisionTest.java
110 lines (101 loc) · 4.35 KB
/
DeletionCollisionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.repository;
import static org.jboss.as.repository.PathUtil.deleteRecursively;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.jboss.byteman.contrib.bmunit.BMScript;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Test to check the lock when removing content while reading it.
* @author Emmanuel Hugonnet (c) 2016 Red Hat, inc.
*/
@RunWith(BMUnitRunner.class)
public class DeletionCollisionTest {
private ContentRepository repository;
private final File rootDir = new File("target", "repository");
private final File tmpRootDir = new File("target", "tmp");
private final long EXECUTION_TIMEOUT = 30000L;
public DeletionCollisionTest() {
}
@Before
public void createRepository() throws IOException {
if (rootDir.exists()) {
deleteRecursively(rootDir.toPath());
}
rootDir.mkdirs();
if (tmpRootDir.exists()) {
deleteRecursively(tmpRootDir.toPath());
}
tmpRootDir.mkdirs();
repository = ContentRepository.Factory.create(rootDir, tmpRootDir, 0L, 1000L);
repository.readWrite();
}
@After
public void destroyRepository() throws IOException {
deleteRecursively(rootDir.toPath());
deleteRecursively(tmpRootDir.toPath());
repository = null;
}
@BMScript(value="lockDeployment.btm")
@Test
public void testFileLockByReadContent() throws Exception {
try (InputStream stream = getClass().getClassLoader().getResourceAsStream("testfile.xhtml")) {
final byte[] deploymentHash = repository.addContentToExploded(
repository.addContent(null),
Collections.singletonList(new ExplodedContent("testfile.xhtml", stream)),
true);
Assert.assertTrue(repository.hasContent(deploymentHash));
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<InputStream> result = executor.submit(() -> repository.readContent(deploymentHash, "testfile.xhtml"));
Thread.sleep(100L);
Future deletion = executor.submit(() -> {
repository.removeContent(new ContentReference("testfile.xhtml", deploymentHash));
});
deletion.get(EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS);
result.get(EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS);
try (TemporaryFileInputStream in = (TemporaryFileInputStream) result.get(EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
Assert.assertTrue(Files.exists(in.getFile()));
}
}
}
@BMScript(value="lockDeployment.btm")
@Test
public void testFileLockByRemoveContent() throws Exception {
try (InputStream stream = getClass().getClassLoader().getResourceAsStream("testfile.xhtml")) {
final byte[] deploymentHash = repository.addContentToExploded(
repository.addContent(null),
Collections.singletonList(new ExplodedContent("testfile.xhtml", stream)),
true);
Assert.assertTrue(repository.hasContent(deploymentHash));
ExecutorService executor = Executors.newFixedThreadPool(2);
Future deletion = executor.submit(() -> {
repository.removeContent(new ContentReference("testfile.xhtml", deploymentHash));
});
Thread.sleep(100L);
Future<InputStream> result = executor.submit(() -> repository.readContent(deploymentHash, "testfile.xhtml"));
deletion.get(EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS);
try {
result.get(EXECUTION_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.fail("Shouldn't be able to lock the content");
} catch(ExecutionException ex) {
Assert.assertTrue(ex.getCause().getMessage().contains("WFLYDR0019"));
}
}
}
}