SchedulerDead tests

This commit is contained in:
str4d
2015-07-27 08:18:46 +00:00
parent db9555dba3
commit e704baddd8
3 changed files with 95 additions and 0 deletions

View File

@ -90,6 +90,7 @@
<include name="**/*.class"/>
<!-- exclude Test classes -->
<exclude name="**/*Test.class" />
<exclude name="**/*TestBase.class" />
<exclude name="**/*IT.class" />
<exclude name="**/*IT$*.class" />
<exclude name="**/StreamingITBase.class" />

View File

@ -0,0 +1,56 @@
package net.i2p.client.streaming.impl;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.mockito.Mock;
public class SchedulerDeadTest extends SchedulerImplTestBase {
private SchedulerDead s;
@Mock private Connection con;
@Mock private ConnectionOptions opts;
protected void initScheduler() {
s = new SchedulerDead(context);
}
@Test
public void testAccept_null() {
assertFalse(s.accept(null));
}
private void setMocks(int now, int discSchOn, int connTimeout, int lifetime, int sendStreamId) {
when(clock.now()).thenReturn((long) now);
when(con.getDisconnectScheduledOn()).thenReturn((long) discSchOn);
when(con.getOptions()).thenReturn(opts);
when(opts.getConnectTimeout()).thenReturn((long) connTimeout);
when(con.getLifetime()).thenReturn((long) lifetime);
when(con.getSendStreamId()).thenReturn((long) sendStreamId);
}
@Test
public void testAccept_nothingLeftToDo() {
setMocks(10*60*1000, 9*60*1000 - Connection.DISCONNECT_TIMEOUT, 0, 0, 0);
assertTrue(s.accept(con));
}
@Test
public void testAccept_noDisconnectScheduled() {
setMocks(10*60*1000, 0, 0, 0, 0);
assertFalse(s.accept(con));
}
@Test
public void testAccept_timedOut() {
setMocks(0, 0, Connection.DISCONNECT_TIMEOUT/2, Connection.DISCONNECT_TIMEOUT, 0);
assertTrue(s.accept(con));
}
@Test
public void testEventOccurred() {
s.eventOccurred(con);
verify(con).disconnectComplete();
}
}

View File

@ -0,0 +1,38 @@
package net.i2p.client.streaming.impl;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import net.i2p.I2PAppContext;
import net.i2p.util.Clock;
import net.i2p.util.SimpleTimer2;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
public abstract class SchedulerImplTestBase {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Spy protected I2PAppContext context = I2PAppContext.getGlobalContext();
@Mock protected Clock clock;
@Mock protected SimpleTimer2 timer;
protected SchedulerDead scheduler;
@Before
public void setUp() {
when(context.clock()).thenReturn(clock);
when(context.simpleTimer2()).thenReturn(timer);
initScheduler();
}
protected abstract void initScheduler();
}