mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-24 17:51:51 +02:00
Implement and test SwapJobImpl
This commit is contained in:
committed by
Michael Mazour
parent
a595acd0a6
commit
c459cd57af
@@ -7,10 +7,8 @@ import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
|
||||
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.resource.ResourceCache;
|
||||
import uk.ac.ic.wlgitbridge.bridge.snapshot.SnapshotAPI;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.SwapJob;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.SwapStore;
|
||||
|
||||
import java.time.Duration;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.job.SwapJob;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.store.SwapStore;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -52,9 +50,9 @@ public class BridgeTest {
|
||||
|
||||
@Test
|
||||
public void shutdownStopsSwapJob() {
|
||||
bridge.startSwapJob(Duration.ofSeconds(1));
|
||||
bridge.startSwapJob();
|
||||
bridge.doShutdown();
|
||||
verify(swapJob).start(Duration.ofSeconds(1));
|
||||
verify(swapJob).start();
|
||||
verify(swapJob).stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -132,4 +132,19 @@ public class SqliteDBStoreTest {
|
||||
assertEquals("older", dbStore.getOldestUnswappedProject());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNumUnswappedProjects() {
|
||||
dbStore.setLatestVersionForProject("asdf", 1);
|
||||
dbStore.setLastAccessedTime(
|
||||
"asdf",
|
||||
Timestamp.valueOf(LocalDateTime.now())
|
||||
);
|
||||
assertEquals(1, dbStore.getNumUnswappedProjects());
|
||||
dbStore.setLastAccessedTime(
|
||||
"asdf",
|
||||
null
|
||||
);
|
||||
assertEquals(0, dbStore.getNumUnswappedProjects());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,8 +7,18 @@ public class DeleteFilesForProjectSQLUpdateTest {
|
||||
|
||||
@Test
|
||||
public void testGetSQL() {
|
||||
DeleteFilesForProjectSQLUpdate update = new DeleteFilesForProjectSQLUpdate("projname", "path1", "path2");
|
||||
assertEquals("DELETE FROM `url_index_store` WHERE `project_name` = ? AND path IN (?, ?);\n", update.getSQL());
|
||||
DeleteFilesForProjectSQLUpdate update =
|
||||
new DeleteFilesForProjectSQLUpdate(
|
||||
"projname",
|
||||
"path1",
|
||||
"path2"
|
||||
);
|
||||
assertEquals(
|
||||
"DELETE FROM `url_index_store` " +
|
||||
"WHERE `project_name` = ? " +
|
||||
"AND path IN (?, ?);\n",
|
||||
update.getSQL()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,20 @@ import static org.junit.Assert.assertEquals;
|
||||
*/
|
||||
public class FSRepoStoreTest {
|
||||
|
||||
public static File makeTempRepoDir(
|
||||
TemporaryFolder tmpFolder,
|
||||
String name
|
||||
) throws IOException {
|
||||
File tmp = tmpFolder.newFolder(name);
|
||||
Path rootdir = Paths.get(
|
||||
"src/test/resources/uk/ac/ic/wlgitbridge/"
|
||||
+ "bridge/repo/FSRepoStoreTest/rootdir"
|
||||
);
|
||||
FileUtils.copyDirectory(rootdir.toFile(), tmp);
|
||||
Files.renameAll(tmp, "DOTgit", ".git");
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private FSRepoStore repoStore;
|
||||
private File original;
|
||||
|
||||
@@ -28,13 +42,7 @@ public class FSRepoStoreTest {
|
||||
public void setup() throws IOException {
|
||||
TemporaryFolder tmpFolder = new TemporaryFolder();
|
||||
tmpFolder.create();
|
||||
File tmp = tmpFolder.newFolder("repostore");
|
||||
Path rootdir = Paths.get(
|
||||
"src/test/resources/uk/ac/ic/wlgitbridge/"
|
||||
+ "bridge/repo/FSRepoStoreTest/rootdir"
|
||||
);
|
||||
FileUtils.copyDirectory(rootdir.toFile(), tmp);
|
||||
Files.renameAll(tmp, "DOTgit", ".git");
|
||||
File tmp = makeTempRepoDir(tmpFolder, "rootdir");
|
||||
original = tmpFolder.newFolder("original");
|
||||
FileUtils.copyDirectory(tmp, original);
|
||||
repoStore = new FSRepoStore(tmp.getAbsolutePath());
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package uk.ac.ic.wlgitbridge.bridge.swap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
|
||||
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Created by winston on 20/08/2016.
|
||||
*/
|
||||
public class SwapJobImplTest {
|
||||
|
||||
private SwapJobImpl swapJob;
|
||||
|
||||
private ProjectLock lock;
|
||||
private RepoStore repoStore;
|
||||
private DBStore dbStore;
|
||||
private SwapStore swapStore;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
lock = mock(ProjectLock.class);
|
||||
repoStore = mock(RepoStore.class);
|
||||
dbStore = mock(DBStore.class);
|
||||
swapStore = mock(SwapStore.class);
|
||||
swapJob = new SwapJobImpl(
|
||||
SwapJobConfig.DEFAULT,
|
||||
lock,
|
||||
repoStore,
|
||||
dbStore,
|
||||
swapStore
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startingTimerAlwaysCausesASwap() {
|
||||
assertEquals(0, swapJob.swaps.get());
|
||||
swapJob.start(Duration.ofHours(1));
|
||||
while (swapJob.swaps.get() <= 0);
|
||||
assertTrue(swapJob.swaps.get() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void swapsHappenEveryInterval() {
|
||||
assertEquals(0, swapJob.swaps.get());
|
||||
swapJob.start(Duration.ofMillis(1));
|
||||
while (swapJob.swaps.get() <= 1);
|
||||
assertTrue(swapJob.swaps.get() > 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package uk.ac.ic.wlgitbridge.bridge.swap.job;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.db.sqlite.SqliteDBStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.lock.ProjectLock;
|
||||
import uk.ac.ic.wlgitbridge.bridge.repo.FSRepoStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.repo.FSRepoStoreTest;
|
||||
import uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.store.InMemorySwapStore;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.store.SwapStore;
|
||||
import uk.ac.ic.wlgitbridge.data.ProjectLockImpl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by winston on 20/08/2016.
|
||||
*/
|
||||
public class SwapJobImplTest {
|
||||
|
||||
private SwapJobImpl swapJob;
|
||||
|
||||
private ProjectLock lock;
|
||||
private RepoStore repoStore;
|
||||
private DBStore dbStore;
|
||||
private SwapStore swapStore;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
TemporaryFolder tmpFolder = new TemporaryFolder();
|
||||
tmpFolder.create();
|
||||
lock = new ProjectLockImpl();
|
||||
repoStore = new FSRepoStore(
|
||||
FSRepoStoreTest.makeTempRepoDir(
|
||||
tmpFolder,
|
||||
"repostore"
|
||||
).getAbsolutePath()
|
||||
);
|
||||
dbStore = new SqliteDBStore(tmpFolder.newFile());
|
||||
dbStore.setLatestVersionForProject("proj1", 0);
|
||||
dbStore.setLatestVersionForProject("proj2", 0);
|
||||
dbStore.setLastAccessedTime(
|
||||
"proj1",
|
||||
Timestamp.valueOf(LocalDateTime.now())
|
||||
);
|
||||
dbStore.setLastAccessedTime(
|
||||
"proj2",
|
||||
Timestamp.valueOf(
|
||||
LocalDateTime.now().minus(1, ChronoUnit.SECONDS)
|
||||
)
|
||||
);
|
||||
swapStore = new InMemorySwapStore();
|
||||
swapJob = new SwapJobImpl(
|
||||
1,
|
||||
15000,
|
||||
30000,
|
||||
Duration.ofMillis(100),
|
||||
lock,
|
||||
repoStore,
|
||||
dbStore,
|
||||
swapStore
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startingTimerAlwaysCausesASwap() {
|
||||
swapJob.lowWatermarkBytes = 16384;
|
||||
swapJob.interval = Duration.ofHours(1);
|
||||
assertEquals(0, swapJob.swaps.get());
|
||||
swapJob.start();
|
||||
while (swapJob.swaps.get() <= 0);
|
||||
assertTrue(swapJob.swaps.get() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void swapsHappenEveryInterval() {
|
||||
swapJob.lowWatermarkBytes = 16384;
|
||||
assertEquals(0, swapJob.swaps.get());
|
||||
swapJob.start();
|
||||
while (swapJob.swaps.get() <= 1);
|
||||
assertTrue(swapJob.swaps.get() > 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noProjectsGetSwappedWhenUnderHighWatermark() {
|
||||
swapJob.highWatermarkBytes = 65536;
|
||||
assertEquals(2, dbStore.getNumUnswappedProjects());
|
||||
swapJob.start();
|
||||
while (swapJob.swaps.get() < 1);
|
||||
assertEquals(2, dbStore.getNumUnswappedProjects());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correctProjGetSwappedWhenOverHighWatermark(
|
||||
) throws IOException {
|
||||
swapJob.lowWatermarkBytes = 16384;
|
||||
assertEquals(2, dbStore.getNumUnswappedProjects());
|
||||
assertEquals("proj2", dbStore.getOldestUnswappedProject());
|
||||
swapJob.start();
|
||||
while (swapJob.swaps.get() < 1);
|
||||
assertEquals(1, dbStore.getNumUnswappedProjects());
|
||||
assertEquals("proj1", dbStore.getOldestUnswappedProject());
|
||||
swapJob.restore("proj2");
|
||||
int numSwaps = swapJob.swaps.get();
|
||||
while (swapJob.swaps.get() <= numSwaps);
|
||||
assertEquals(1, dbStore.getNumUnswappedProjects());
|
||||
assertEquals("proj2", dbStore.getOldestUnswappedProject());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package uk.ac.ic.wlgitbridge.bridge.swap;
|
||||
package uk.ac.ic.wlgitbridge.bridge.swap.store;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import uk.ac.ic.wlgitbridge.bridge.swap.store.InMemorySwapStore;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -51,7 +52,8 @@ public class InMemorySwapStoreTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uploadingForTheSameProjectOverwritesTheFile() throws IOException {
|
||||
public void uploadingForTheSameProjectOverwritesTheFile(
|
||||
) throws IOException {
|
||||
byte[] proj1Contents = "helloproj1".getBytes();
|
||||
byte[] proj1NewContents = "goodbyeproj1".getBytes();
|
||||
swapStore.upload(
|
||||
@@ -1,4 +1,4 @@
|
||||
package uk.ac.ic.wlgitbridge.bridge.swap;
|
||||
package uk.ac.ic.wlgitbridge.bridge.swap.store;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
Reference in New Issue
Block a user