/**
* Test method for {@link org.apache.niolex.commons.seda.Stage#addThread()}.
*/
@Test
public final void testAddThreadToMax() throws Exception {
SleepStage ss = new SleepStage("abc", dispatcher);
Field f = Stage.class.getDeclaredField("lastAdjustTime");
f.setAccessible(true);
TInput in = mock(TInput.class);
// Prepare ready.
// stage 1.
for (int i = 0; i < 60; ++i) {
ss.addInput(in);
}
ss.exeCnt.addAndGet(30);
this.startAdjust(ss, f);
// stage 2.
for (int i = 0; i < 210; ++i) {
ss.addInput(in);
}
ss.exeCnt.addAndGet(100);
this.startAdjust(ss, f);
// stage 3.
for (int i = 0; i < 550; ++i) {
ss.addInput(in);
}
ss.exeCnt.addAndGet(200);
this.startAdjust(ss, f);
// stage 4.
for (int i = 0; i < 1300; ++i) {
ss.addInput(in);
}
ss.exeCnt.addAndGet(500);
this.startAdjust(ss, f);
// stage 5.
System.out.println("up stage finished. we start to drop.");
// No input, But we adjust the executed item.
for (int i = 0; i < 500; ++i) {
ss.inputQueue.poll();
}
ss.exeCnt.addAndGet(500);
this.startAdjust(ss, f);
for (int i = 0; i < 500; ++i) {
ss.inputQueue.poll();
}
ss.exeCnt.addAndGet(500);
this.startAdjust(ss, f);
// stage 6.
// No input, We adjust even more executed item.
ss.exeCnt.addAndGet(600);
for (int i = 0; i < 600; ++i) {
ss.inputQueue.poll();
}
System.out.println("stage6 " + ss.currentPoolSize);
int a = ss.currentPoolSize;
this.startAdjust(ss, f);
System.out.println("stage6 " + ss.currentPoolSize);
a -= ss.currentPoolSize;
// We are in tremble.
assertTrue(a == 0);
// stage 7.
for (int i = 0; i < 600; ++i) {
ss.inputQueue.poll();
}
ss.exeCnt.addAndGet(600);
this.startAdjust(ss, f);
System.out.println("stage7 " + ss.currentPoolSize);
// stage 8.
ss.exeCnt.addAndGet(200);
this.startAdjust(ss, f);
System.out.println("stage8 " + ss.currentPoolSize);
// stage 9.
ss.exeCnt.addAndGet(3);
this.startAdjust(ss, f);
System.out.println("stage9 " + ss.currentPoolSize);
// stage 10.
ss.exeCnt.addAndGet(3);
this.startAdjust(ss, f);
System.out.println("stage10 " + ss.currentPoolSize);
assertEquals(1, ss.currentPoolSize);
ss.shutdown();
}