mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-23 04:28:35 +00:00
committed by
Greyson Parrelli
parent
39b4484887
commit
d28fa304c8
@@ -1,94 +0,0 @@
|
||||
package org.signal.core.util.concurrent;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public final class LatestPrioritizedSerialExecutorTest {
|
||||
|
||||
@Test
|
||||
public void execute_sortsInPriorityOrder() {
|
||||
TestExecutor executor = new TestExecutor();
|
||||
Runnable placeholder = new TestRunnable();
|
||||
|
||||
Runnable first = spy(new TestRunnable());
|
||||
Runnable second = spy(new TestRunnable());
|
||||
Runnable third = spy(new TestRunnable());
|
||||
|
||||
LatestPrioritizedSerialExecutor subject = new LatestPrioritizedSerialExecutor(executor);
|
||||
subject.execute(0, placeholder); // The first thing we execute can't be sorted, so we put in this placeholder
|
||||
subject.execute(1, third);
|
||||
subject.execute(2, second);
|
||||
subject.execute(3, first);
|
||||
|
||||
executor.next(); // Clear the placeholder task
|
||||
|
||||
executor.next();
|
||||
verify(first).run();
|
||||
|
||||
executor.next();
|
||||
verify(second).run();
|
||||
|
||||
executor.next();
|
||||
verify(third).run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void execute_replacesDupes() {
|
||||
TestExecutor executor = new TestExecutor();
|
||||
Runnable placeholder = new TestRunnable();
|
||||
|
||||
Runnable firstReplaced = spy(new TestRunnable());
|
||||
Runnable first = spy(new TestRunnable());
|
||||
Runnable second = spy(new TestRunnable());
|
||||
Runnable thirdReplaced = spy(new TestRunnable());
|
||||
Runnable third = spy(new TestRunnable());
|
||||
|
||||
LatestPrioritizedSerialExecutor subject = new LatestPrioritizedSerialExecutor(executor);
|
||||
subject.execute(0, placeholder); // The first thing we execute can't be sorted, so we put in this placeholder
|
||||
subject.execute(1, thirdReplaced);
|
||||
subject.execute(1, third);
|
||||
subject.execute(2, second);
|
||||
subject.execute(3, firstReplaced);
|
||||
subject.execute(3, first);
|
||||
|
||||
executor.next(); // Clear the placeholder task
|
||||
|
||||
executor.next();
|
||||
verify(first).run();
|
||||
|
||||
executor.next();
|
||||
verify(second).run();
|
||||
|
||||
executor.next();
|
||||
verify(third).run();
|
||||
|
||||
verify(firstReplaced, never()).run();
|
||||
verify(thirdReplaced, never()).run();
|
||||
}
|
||||
|
||||
private static final class TestExecutor implements Executor {
|
||||
|
||||
private final Queue<Runnable> tasks = new LinkedList<>();
|
||||
|
||||
@Override
|
||||
public void execute(Runnable command) {
|
||||
tasks.add(command);
|
||||
}
|
||||
|
||||
public void next() {
|
||||
tasks.remove().run();
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package org.signal.core.util.concurrent
|
||||
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.util.concurrent.Executor
|
||||
|
||||
class LatestPrioritizedSerialExecutorTest {
|
||||
@Test
|
||||
fun execute_sortsInPriorityOrder() {
|
||||
val executor = TestExecutor()
|
||||
val placeholder = TestRunnable()
|
||||
|
||||
val first = TestRunnable()
|
||||
val second = TestRunnable()
|
||||
val third = TestRunnable()
|
||||
|
||||
val subject = LatestPrioritizedSerialExecutor(executor)
|
||||
subject.execute(0, placeholder) // The first thing we execute can't be sorted, so we put in this placeholder
|
||||
subject.execute(1, third)
|
||||
subject.execute(2, second)
|
||||
subject.execute(3, first)
|
||||
|
||||
executor.next() // Clear the placeholder task
|
||||
|
||||
executor.next()
|
||||
assertTrue(first.didRun)
|
||||
|
||||
executor.next()
|
||||
assertTrue(second.didRun)
|
||||
|
||||
executor.next()
|
||||
assertTrue(third.didRun)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun execute_replacesDupes() {
|
||||
val executor = TestExecutor()
|
||||
val placeholder = TestRunnable()
|
||||
|
||||
val firstReplaced = TestRunnable()
|
||||
val first = TestRunnable()
|
||||
val second = TestRunnable()
|
||||
val thirdReplaced = TestRunnable()
|
||||
val third = TestRunnable()
|
||||
|
||||
val subject = LatestPrioritizedSerialExecutor(executor)
|
||||
subject.execute(0, placeholder) // The first thing we execute can't be sorted, so we put in this placeholder
|
||||
subject.execute(1, thirdReplaced)
|
||||
subject.execute(1, third)
|
||||
subject.execute(2, second)
|
||||
subject.execute(3, firstReplaced)
|
||||
subject.execute(3, first)
|
||||
|
||||
executor.next() // Clear the placeholder task
|
||||
|
||||
executor.next()
|
||||
assertTrue(first.didRun)
|
||||
|
||||
executor.next()
|
||||
assertTrue(second.didRun)
|
||||
|
||||
executor.next()
|
||||
assertTrue(third.didRun)
|
||||
|
||||
assertFalse(firstReplaced.didRun)
|
||||
assertFalse(thirdReplaced.didRun)
|
||||
}
|
||||
|
||||
private class TestExecutor : Executor {
|
||||
private val tasks = ArrayDeque<Runnable>()
|
||||
|
||||
override fun execute(command: Runnable) {
|
||||
tasks.add(command)
|
||||
}
|
||||
|
||||
fun next() {
|
||||
tasks.removeLast().run()
|
||||
}
|
||||
}
|
||||
|
||||
class TestRunnable : Runnable {
|
||||
private var _didRun = false
|
||||
val didRun get() = _didRun
|
||||
|
||||
override fun run() {
|
||||
_didRun = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user