|
| 1 | +package org.opengrok.web.api.v1.controller; |
| 2 | + |
| 3 | +import java.nio.file.Paths; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.TreeMap; |
| 9 | +import java.util.concurrent.ExecutionException; |
| 10 | +import java.util.concurrent.ExecutorService; |
| 11 | +import java.util.concurrent.Executors; |
| 12 | +import java.util.concurrent.Future; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import javax.ws.rs.client.Entity; |
| 15 | +import javax.ws.rs.core.Application; |
| 16 | +import javax.ws.rs.core.Response; |
| 17 | +import org.apache.commons.io.FileUtils; |
| 18 | +import org.glassfish.jersey.internal.inject.AbstractBinder; |
| 19 | +import org.glassfish.jersey.server.ResourceConfig; |
| 20 | +import org.glassfish.jersey.test.JerseyTest; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.MockitoAnnotations; |
| 28 | +import org.opengrok.indexer.condition.ConditionalRun; |
| 29 | +import org.opengrok.indexer.condition.ConditionalRunRule; |
| 30 | +import org.opengrok.indexer.condition.RepositoryInstalled; |
| 31 | +import org.opengrok.indexer.configuration.Project; |
| 32 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
| 33 | +import org.opengrok.indexer.history.HistoryGuru; |
| 34 | +import org.opengrok.indexer.history.RepositoryInfo; |
| 35 | +import org.opengrok.indexer.util.TestRepository; |
| 36 | +import org.opengrok.web.api.v1.suggester.provider.service.SuggesterService; |
| 37 | + |
| 38 | +@ConditionalRun(RepositoryInstalled.GitInstalled.class) |
| 39 | +public class ConcurrentConfigurationControllerTest extends JerseyTest { |
| 40 | + |
| 41 | + private static final int PROJECTS_COUNT = 20; |
| 42 | + private static final int THREAD_COUNT = Math.max(30, Runtime.getRuntime().availableProcessors() * 2); |
| 43 | + private static final int TASK_COUNT = 100; |
| 44 | + |
| 45 | + @Rule |
| 46 | + public ConditionalRunRule rule = new ConditionalRunRule(); |
| 47 | + |
| 48 | + @Mock |
| 49 | + private SuggesterService suggesterService; |
| 50 | + |
| 51 | + private RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 52 | + |
| 53 | + TestRepository repository; |
| 54 | + String origSourceRootPath; |
| 55 | + String origDataRootPath; |
| 56 | + Map<String, Project> origProjects; |
| 57 | + List<RepositoryInfo> origRepositories; |
| 58 | + |
| 59 | + @Override |
| 60 | + protected Application configure() { |
| 61 | + MockitoAnnotations.initMocks(this); |
| 62 | + return new ResourceConfig(ConfigurationController.class) |
| 63 | + .register(new AbstractBinder() { |
| 64 | + @Override |
| 65 | + protected void configure() { |
| 66 | + bind(suggesterService).to(SuggesterService.class); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + @Before |
| 72 | + public void setUp() throws Exception { |
| 73 | + super.setUp(); |
| 74 | + origSourceRootPath = env.getSourceRootPath(); |
| 75 | + origDataRootPath = env.getDataRootPath(); |
| 76 | + origProjects = env.getProjects(); |
| 77 | + origRepositories = env.getRepositories(); |
| 78 | + |
| 79 | + // prepare test repository |
| 80 | + repository = new TestRepository(); |
| 81 | + repository.create(HistoryGuru.class.getResourceAsStream("repositories.zip")); |
| 82 | + |
| 83 | + env.setSourceRoot(repository.getSourceRoot()); |
| 84 | + env.setDataRoot(repository.getDataRoot()); |
| 85 | + |
| 86 | + |
| 87 | + List<RepositoryInfo> repositoryInfos = new ArrayList<>(); |
| 88 | + Map<String, Project> projects = new TreeMap<>(); |
| 89 | + |
| 90 | + /* |
| 91 | + * Prepare PROJECTS_COUNT git repositories, named project-{i} in the test repositories directory. |
| 92 | + */ |
| 93 | + for (int i = 0; i < PROJECTS_COUNT; i++) { |
| 94 | + Project project = new Project(); |
| 95 | + project.setName("project-" + i); |
| 96 | + project.setPath("/project-" + i); |
| 97 | + RepositoryInfo repo = new RepositoryInfo(); |
| 98 | + repo.setDirectoryNameRelative("/project-" + i); |
| 99 | + |
| 100 | + projects.put("project-" + i, project); |
| 101 | + repositoryInfos.add(repo); |
| 102 | + |
| 103 | + // create the repository |
| 104 | + FileUtils.copyDirectory( |
| 105 | + Paths.get(repository.getSourceRoot(), "git").toFile(), |
| 106 | + Paths.get(repository.getSourceRoot(), "project-" + i).toFile() |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + env.setRepositories(repositoryInfos); |
| 111 | + env.setProjects(projects); |
| 112 | + } |
| 113 | + |
| 114 | + @After |
| 115 | + public void tearDown() throws Exception { |
| 116 | + super.tearDown(); |
| 117 | + repository.destroy(); |
| 118 | + |
| 119 | + env.setProjects(origProjects); |
| 120 | + env.setRepositories(origRepositories); |
| 121 | + env.setSourceRoot(origSourceRootPath); |
| 122 | + env.setDataRoot(origDataRootPath); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testConcurrentConfigurationReloads() throws InterruptedException, ExecutionException { |
| 127 | + final ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT); |
| 128 | + List<Future<?>> futures = new LinkedList<>(); |
| 129 | + |
| 130 | + /* |
| 131 | + * Now run setting a value in parallel which triggers configuration reload. |
| 132 | + */ |
| 133 | + for (int i = 0; i < TASK_COUNT; i++) { |
| 134 | + futures.add(threadPool.submit(() -> { |
| 135 | + Response put = target("configuration") |
| 136 | + .path("projectsEnabled") |
| 137 | + .request() |
| 138 | + .put(Entity.text("true")); |
| 139 | + |
| 140 | + Assert.assertEquals(204, put.getStatus()); |
| 141 | + |
| 142 | + assertTestedProjects(); |
| 143 | + })); |
| 144 | + } |
| 145 | + |
| 146 | + threadPool.shutdown(); |
| 147 | + threadPool.awaitTermination(1, TimeUnit.MINUTES); |
| 148 | + |
| 149 | + for (Future<?> future : futures) { |
| 150 | + // calling get on a future will rethrow the exceptions in its thread (i. e. assertions in this case) |
| 151 | + future.get(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testConcurrentCInvalidateRepositories() throws InterruptedException, ExecutionException { |
| 157 | + final ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT); |
| 158 | + final List<RepositoryInfo> repositoryInfos = env.getRepositories(); |
| 159 | + List<Future<?>> futures = new LinkedList<>(); |
| 160 | + |
| 161 | + /* |
| 162 | + * Now run apply config in parallel |
| 163 | + */ |
| 164 | + for (int i = 0; i < TASK_COUNT; i++) { |
| 165 | + futures.add(threadPool.submit(() -> { |
| 166 | + env.applyConfig(false, false); |
| 167 | + assertTestedProjects(); |
| 168 | + })); |
| 169 | + } |
| 170 | + |
| 171 | + threadPool.shutdown(); |
| 172 | + threadPool.awaitTermination(1, TimeUnit.MINUTES); |
| 173 | + |
| 174 | + for (Future<?> future : futures) { |
| 175 | + // calling get on a future will rethrow the exceptions in its thread (i. e. assertions in this case) |
| 176 | + future.get(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void assertTestedProjects() { |
| 181 | + Assert.assertEquals(PROJECTS_COUNT, env.getProjects().size()); |
| 182 | + Assert.assertEquals(PROJECTS_COUNT, env.getRepositories().size()); |
| 183 | + Assert.assertEquals(PROJECTS_COUNT, env.getProjectRepositoriesMap().size()); |
| 184 | + env.getProjectRepositoriesMap().forEach((project, repositories) -> { |
| 185 | + Assert.assertEquals(1, repositories.size()); |
| 186 | + }); |
| 187 | + } |
| 188 | +} |
0 commit comments