Skip to content

Moving unit tests to test source folders #439 #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 4 additions & 55 deletions rxjava-core/src/main/java/rx/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@
*/
package rx;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mockito;

import rx.concurrency.TestScheduler;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.MultipleAssignmentSubscription;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Func1;
import rx.util.functions.Func2;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Represents an object that schedules units of work.
* <p>
Expand Down Expand Up @@ -264,46 +255,4 @@ public long now() {
public int degreeOfParallelism() {
return Runtime.getRuntime().availableProcessors();
}

public static class UnitTest {
@SuppressWarnings("unchecked")
// mocking is unchecked, unfortunately
@Test
public void testPeriodicScheduling() {
final Func1<Long, Void> calledOp = mock(Func1.class);

final TestScheduler scheduler = new TestScheduler();
Subscription subscription = scheduler.schedulePeriodically(new Action0() {
@Override
public void call() {
System.out.println(scheduler.now());
calledOp.call(scheduler.now());
}
}, 1, 2, TimeUnit.SECONDS);

verify(calledOp, never()).call(anyLong());

InOrder inOrder = Mockito.inOrder(calledOp);

scheduler.advanceTimeBy(999L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, never()).call(anyLong());

scheduler.advanceTimeBy(1L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, times(1)).call(1000L);

scheduler.advanceTimeBy(1999L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, never()).call(3000L);

scheduler.advanceTimeBy(1L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, times(1)).call(3000L);

scheduler.advanceTimeBy(5L, TimeUnit.SECONDS);
inOrder.verify(calledOp, times(1)).call(5000L);
inOrder.verify(calledOp, times(1)).call(7000L);

subscription.unsubscribe();
scheduler.advanceTimeBy(11L, TimeUnit.SECONDS);
inOrder.verify(calledOp, never()).call(anyLong());
}
}
}
136 changes: 4 additions & 132 deletions rxjava-core/src/main/java/rx/concurrency/CurrentThreadScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
*/
package rx.concurrency;

import static org.mockito.Mockito.*;
import rx.Scheduler;
import rx.Subscription;
import rx.util.functions.Func2;

import java.util.PriorityQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
import org.mockito.InOrder;

import rx.Scheduler;
import rx.Subscription;
import rx.util.functions.Action0;
import rx.util.functions.Func2;

/**
* Schedules work on the current thread but does not execute immediately. Work is put in a queue and executed after the current unit of work is completed.
*/
Expand All @@ -41,7 +35,7 @@ public static CurrentThreadScheduler getInstance() {

private static final ThreadLocal<PriorityQueue<TimedAction>> QUEUE = new ThreadLocal<PriorityQueue<TimedAction>>();

private CurrentThreadScheduler() {
CurrentThreadScheduler() {
}

private final AtomicInteger counter = new AtomicInteger(0);
Expand Down Expand Up @@ -102,126 +96,4 @@ public int compareTo(TimedAction that) {
return result;
}
}

public static class UnitTest {

@Test
public void testNestedActions() {
final CurrentThreadScheduler scheduler = new CurrentThreadScheduler();

final Action0 firstStepStart = mock(Action0.class);
final Action0 firstStepEnd = mock(Action0.class);

final Action0 secondStepStart = mock(Action0.class);
final Action0 secondStepEnd = mock(Action0.class);

final Action0 thirdStepStart = mock(Action0.class);
final Action0 thirdStepEnd = mock(Action0.class);

final Action0 firstAction = new Action0() {
@Override
public void call() {
firstStepStart.call();
firstStepEnd.call();
}
};
final Action0 secondAction = new Action0() {
@Override
public void call() {
secondStepStart.call();
scheduler.schedule(firstAction);
secondStepEnd.call();

}
};
final Action0 thirdAction = new Action0() {
@Override
public void call() {
thirdStepStart.call();
scheduler.schedule(secondAction);
thirdStepEnd.call();
}
};

InOrder inOrder = inOrder(firstStepStart, firstStepEnd, secondStepStart, secondStepEnd, thirdStepStart, thirdStepEnd);

scheduler.schedule(thirdAction);

inOrder.verify(thirdStepStart, times(1)).call();
inOrder.verify(thirdStepEnd, times(1)).call();
inOrder.verify(secondStepStart, times(1)).call();
inOrder.verify(secondStepEnd, times(1)).call();
inOrder.verify(firstStepStart, times(1)).call();
inOrder.verify(firstStepEnd, times(1)).call();
}

@Test
public void testSequenceOfActions() {
final CurrentThreadScheduler scheduler = new CurrentThreadScheduler();

final Action0 first = mock(Action0.class);
final Action0 second = mock(Action0.class);

scheduler.schedule(first);
scheduler.schedule(second);

verify(first, times(1)).call();
verify(second, times(1)).call();

}

@Test
public void testSequenceOfDelayedActions() {
final CurrentThreadScheduler scheduler = new CurrentThreadScheduler();

final Action0 first = mock(Action0.class);
final Action0 second = mock(Action0.class);

scheduler.schedule(new Action0() {
@Override
public void call() {
scheduler.schedule(first, 30, TimeUnit.MILLISECONDS);
scheduler.schedule(second, 10, TimeUnit.MILLISECONDS);
}
});

InOrder inOrder = inOrder(first, second);

inOrder.verify(second, times(1)).call();
inOrder.verify(first, times(1)).call();


}

@Test
public void testMixOfDelayedAndNonDelayedActions() {
final CurrentThreadScheduler scheduler = new CurrentThreadScheduler();

final Action0 first = mock(Action0.class);
final Action0 second = mock(Action0.class);
final Action0 third = mock(Action0.class);
final Action0 fourth = mock(Action0.class);

scheduler.schedule(new Action0() {
@Override
public void call() {
scheduler.schedule(first);
scheduler.schedule(second, 300, TimeUnit.MILLISECONDS);
scheduler.schedule(third, 100, TimeUnit.MILLISECONDS);
scheduler.schedule(fourth);
}
});

InOrder inOrder = inOrder(first, second, third, fourth);

inOrder.verify(first, times(1)).call();
inOrder.verify(fourth, times(1)).call();
inOrder.verify(third, times(1)).call();
inOrder.verify(second, times(1)).call();


}

}

}
103 changes: 21 additions & 82 deletions rxjava-core/src/main/java/rx/concurrency/ImmediateScheduler.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2013 Netflix, Inc.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,96 +15,35 @@
*/
package rx.concurrency;

import static org.mockito.Mockito.*;

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.mockito.InOrder;

import rx.Scheduler;
import rx.Subscription;
import rx.util.functions.Action0;
import rx.util.functions.Func2;

import java.util.concurrent.TimeUnit;

/**
* Executes work immediately on the current thread.
*/
public final class ImmediateScheduler extends Scheduler {
private static final ImmediateScheduler INSTANCE = new ImmediateScheduler();

public static ImmediateScheduler getInstance() {
return INSTANCE;
}

private ImmediateScheduler() {
}

@Override
public <T> Subscription schedule(T state, Func2<? super Scheduler, ? super T, ? extends Subscription> action) {
return action.call(this, state);
}

@Override
public <T> Subscription schedule(T state, Func2<? super Scheduler, ? super T, ? extends Subscription> action, long dueTime, TimeUnit unit) {
// since we are executing immediately on this thread we must cause this thread to sleep
long execTime = now() + unit.toMillis(dueTime);

return schedule(state, new SleepingAction<T>(action, this, execTime));
}

public static class UnitTest {

@Test
public void testNestedActions() {
final ImmediateScheduler scheduler = new ImmediateScheduler();

final Action0 firstStepStart = mock(Action0.class);
final Action0 firstStepEnd = mock(Action0.class);

final Action0 secondStepStart = mock(Action0.class);
final Action0 secondStepEnd = mock(Action0.class);

final Action0 thirdStepStart = mock(Action0.class);
final Action0 thirdStepEnd = mock(Action0.class);

final Action0 firstAction = new Action0() {
@Override
public void call() {
firstStepStart.call();
firstStepEnd.call();
}
};
final Action0 secondAction = new Action0() {
@Override
public void call() {
secondStepStart.call();
scheduler.schedule(firstAction);
secondStepEnd.call();

}
};
final Action0 thirdAction = new Action0() {
@Override
public void call() {
thirdStepStart.call();
scheduler.schedule(secondAction);
thirdStepEnd.call();
}
};
private static final ImmediateScheduler INSTANCE = new ImmediateScheduler();

InOrder inOrder = inOrder(firstStepStart, firstStepEnd, secondStepStart, secondStepEnd, thirdStepStart, thirdStepEnd);
public static ImmediateScheduler getInstance() {
return INSTANCE;
}

scheduler.schedule(thirdAction);
ImmediateScheduler() {
}

inOrder.verify(thirdStepStart, times(1)).call();
inOrder.verify(secondStepStart, times(1)).call();
inOrder.verify(firstStepStart, times(1)).call();
inOrder.verify(firstStepEnd, times(1)).call();
inOrder.verify(secondStepEnd, times(1)).call();
inOrder.verify(thirdStepEnd, times(1)).call();
}
@Override
public <T> Subscription schedule(T state, Func2<? super Scheduler, ? super T, ? extends Subscription> action) {
return action.call(this, state);
}

}
@Override
public <T> Subscription schedule(T state, Func2<? super Scheduler, ? super T, ? extends Subscription> action, long dueTime, TimeUnit unit) {
// since we are executing immediately on this thread we must cause this thread to sleep
long execTime = now() + unit.toMillis(dueTime);

return schedule(state, new SleepingAction<T>(action, this, execTime));
}
}
Loading