mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-13 15:42:23 +00:00
Create system for job migrations.
This commit is contained in:
committed by
Alan Evans
parent
9580bb0a38
commit
af42d5b671
@@ -0,0 +1,106 @@
|
||||
package org.thoughtcrime.securesms.jobmanager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
|
||||
import org.thoughtcrime.securesms.jobmanager.persistence.JobStorage;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class JobMigratorTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
Log.initialize(mock(Log.Logger.class));
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void JobMigrator_crashWhenTooFewMigrations() {
|
||||
new JobMigrator(1, 2, Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void JobMigrator_crashWhenTooManyMigrations() {
|
||||
new JobMigrator(1, 2, Arrays.asList(new EmptyMigration(2), new EmptyMigration(3)));
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void JobMigrator_crashWhenSkippingMigrations() {
|
||||
new JobMigrator(1, 3, Arrays.asList(new EmptyMigration(2), new EmptyMigration(4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void JobMigrator_properInitialization() {
|
||||
new JobMigrator(1, 3, Arrays.asList(new EmptyMigration(2), new EmptyMigration(3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void migrate_callsAppropriateMigrations_fullSet() {
|
||||
JobMigration migration1 = spy(new EmptyMigration(2));
|
||||
JobMigration migration2 = spy(new EmptyMigration(3));
|
||||
|
||||
JobMigrator subject = new JobMigrator(1, 3, Arrays.asList(migration1, migration2));
|
||||
int version = subject.migrate(simpleJobStorage(), mock(Data.Serializer.class));
|
||||
|
||||
assertEquals(3, version);
|
||||
verify(migration1).migrate(any());
|
||||
verify(migration2).migrate(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void migrate_callsAppropriateMigrations_subset() {
|
||||
JobMigration migration1 = spy(new EmptyMigration(2));
|
||||
JobMigration migration2 = spy(new EmptyMigration(3));
|
||||
|
||||
JobMigrator subject = new JobMigrator(2, 3, Arrays.asList(migration1, migration2));
|
||||
int version = subject.migrate(simpleJobStorage(), mock(Data.Serializer.class));
|
||||
|
||||
assertEquals(3, version);
|
||||
verify(migration1, never()).migrate(any());
|
||||
verify(migration2).migrate(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void migrate_callsAppropriateMigrations_none() {
|
||||
JobMigration migration1 = spy(new EmptyMigration(2));
|
||||
JobMigration migration2 = spy(new EmptyMigration(3));
|
||||
|
||||
JobMigrator subject = new JobMigrator(3, 3, Arrays.asList(migration1, migration2));
|
||||
int version = subject.migrate(simpleJobStorage(), mock(Data.Serializer.class));
|
||||
|
||||
assertEquals(3, version);
|
||||
verify(migration1, never()).migrate(any());
|
||||
verify(migration2, never()).migrate(any());
|
||||
}
|
||||
|
||||
private static JobStorage simpleJobStorage() {
|
||||
JobStorage jobStorage = mock(JobStorage.class);
|
||||
when(jobStorage.getAllJobSpecs()).thenReturn(new ArrayList<>(Collections.singletonList(new JobSpec("1", "f1", null, 1, 1, 1, 1, 1, 1, 1, "", false))));
|
||||
return jobStorage;
|
||||
}
|
||||
|
||||
private static class EmptyMigration extends JobMigration {
|
||||
|
||||
protected EmptyMigration(int endVersion) {
|
||||
super(endVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NonNull JobData migrate(@NonNull JobData jobData) {
|
||||
return jobData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,43 @@ public class FastJobStorageTest {
|
||||
assertFalse(subject.getJobSpec("2").isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateJobs_writesToDatabase() {
|
||||
JobDatabase database = noopDatabase();
|
||||
FastJobStorage subject = new FastJobStorage(database);
|
||||
List<JobSpec> jobs = Collections.emptyList();
|
||||
|
||||
subject.updateJobs(jobs);
|
||||
|
||||
verify(database).updateJobs(jobs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateJobs_updatesAllFields() {
|
||||
|
||||
FullSpec fullSpec1 = new FullSpec(new JobSpec("1", "f1", null, 1, 1, 1, 1, 1, 1, 1, EMPTY_DATA, false),
|
||||
Collections.emptyList(),
|
||||
Collections.emptyList());
|
||||
FullSpec fullSpec2 = new FullSpec(new JobSpec("2", "f2", null, 1, 1, 1, 1, 1, 1, 1, EMPTY_DATA, false),
|
||||
Collections.emptyList(),
|
||||
Collections.emptyList());
|
||||
FullSpec fullSpec3 = new FullSpec(new JobSpec("3", "f3", null, 1, 1, 1, 1, 1, 1, 1, EMPTY_DATA, false),
|
||||
Collections.emptyList(),
|
||||
Collections.emptyList());
|
||||
|
||||
FastJobStorage subject = new FastJobStorage(fixedDataDatabase(Arrays.asList(fullSpec1, fullSpec2, fullSpec3)));
|
||||
|
||||
JobSpec update1 = new JobSpec("1", "g1", "q1", 2, 2, 2, 2, 2, 2, 2, "abc", true);
|
||||
JobSpec update2 = new JobSpec("2", "g2", "q2", 3, 3, 3, 3, 3, 3, 3, "def", true);
|
||||
|
||||
subject.init();
|
||||
subject.updateJobs(Arrays.asList(update1, update2));
|
||||
|
||||
assertEquals(update1, subject.getJobSpec("1"));
|
||||
assertEquals(update2, subject.getJobSpec("2"));
|
||||
assertEquals(fullSpec3.getJobSpec(), subject.getJobSpec("3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateJobRunningState_writesToDatabase() {
|
||||
JobDatabase database = noopDatabase();
|
||||
|
||||
Reference in New Issue
Block a user