Skip to content

Commit 71366d6

Browse files
authored
Thread pool unit test (#1802)
* reformat the code, and move the test into the correct package * unit test for c.a.d.c.threadpool
1 parent f78f6d4 commit 71366d6

File tree

6 files changed

+416
-5
lines changed

6 files changed

+416
-5
lines changed

dubbo-common/src/test/java/com/alibaba/dubbo/common/threadpool/AbortPolicyWithReportTest.java renamed to dubbo-common/src/test/java/com/alibaba/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* (the "License"); you may not use this file except in compliance with
77
* the License. You may obtain a copy of the License at
88
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
9+
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package com.alibaba.dubbo.common.threadpool;
17+
package com.alibaba.dubbo.common.threadpool.support;
1818

1919
import com.alibaba.dubbo.common.URL;
2020
import com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport;
@@ -37,11 +37,11 @@ public void run() {
3737
System.out.println("hello");
3838
}
3939
}, (ThreadPoolExecutor) Executors.newFixedThreadPool(1));
40-
}catch (RejectedExecutionException rj){
41-
40+
} catch (RejectedExecutionException rj) {
41+
// ignore
4242
}
4343

44-
Thread.currentThread().sleep(1000);
44+
Thread.sleep(1000);
4545

4646
}
4747
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.common.threadpool.support.cached;
19+
20+
import com.alibaba.dubbo.common.Constants;
21+
import com.alibaba.dubbo.common.URL;
22+
import com.alibaba.dubbo.common.threadlocal.InternalThread;
23+
import com.alibaba.dubbo.common.threadpool.ThreadPool;
24+
import com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport;
25+
import org.hamcrest.Matchers;
26+
import org.junit.Test;
27+
28+
import java.util.concurrent.BlockingQueue;
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.LinkedBlockingQueue;
31+
import java.util.concurrent.RejectedExecutionHandler;
32+
import java.util.concurrent.SynchronousQueue;
33+
import java.util.concurrent.ThreadPoolExecutor;
34+
import java.util.concurrent.TimeUnit;
35+
36+
import static org.hamcrest.Matchers.instanceOf;
37+
import static org.hamcrest.Matchers.is;
38+
import static org.hamcrest.Matchers.startsWith;
39+
import static org.junit.Assert.assertThat;
40+
41+
public class CachedThreadPoolTest {
42+
@Test
43+
public void getExecutor1() throws Exception {
44+
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" +
45+
Constants.THREAD_NAME_KEY + "=demo&" +
46+
Constants.CORE_THREADS_KEY + "=1&" +
47+
Constants.THREADS_KEY + "=2&" +
48+
Constants.ALIVE_KEY + "=1000&" +
49+
Constants.QUEUES_KEY + "=0");
50+
ThreadPool threadPool = new CachedThreadPool();
51+
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
52+
assertThat(executor.getCorePoolSize(), is(1));
53+
assertThat(executor.getMaximumPoolSize(), is(2));
54+
assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(SynchronousQueue.class));
55+
assertThat(executor.getRejectedExecutionHandler(),
56+
Matchers.<RejectedExecutionHandler>instanceOf(AbortPolicyWithReport.class));
57+
58+
final CountDownLatch latch = new CountDownLatch(1);
59+
executor.execute(new Runnable() {
60+
@Override
61+
public void run() {
62+
Thread thread = Thread.currentThread();
63+
assertThat(thread, instanceOf(InternalThread.class));
64+
assertThat(thread.getName(), startsWith("demo"));
65+
latch.countDown();
66+
}
67+
});
68+
69+
latch.await(5000, TimeUnit.MICROSECONDS);
70+
assertThat(latch.getCount(), is(0L));
71+
}
72+
73+
@Test
74+
public void getExecutor2() throws Exception {
75+
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + Constants.QUEUES_KEY + "=1");
76+
ThreadPool threadPool = new CachedThreadPool();
77+
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
78+
assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(LinkedBlockingQueue.class));
79+
}
80+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.common.threadpool.support.eager;
19+
20+
import com.alibaba.dubbo.common.Constants;
21+
import com.alibaba.dubbo.common.URL;
22+
import com.alibaba.dubbo.common.threadlocal.InternalThread;
23+
import com.alibaba.dubbo.common.threadpool.ThreadPool;
24+
import com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport;
25+
import org.hamcrest.Matchers;
26+
import org.junit.Test;
27+
28+
import java.util.concurrent.BlockingQueue;
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.RejectedExecutionHandler;
31+
import java.util.concurrent.ThreadPoolExecutor;
32+
import java.util.concurrent.TimeUnit;
33+
34+
import static org.hamcrest.Matchers.instanceOf;
35+
import static org.hamcrest.Matchers.is;
36+
import static org.hamcrest.Matchers.startsWith;
37+
import static org.junit.Assert.assertThat;
38+
39+
public class EagerThreadPoolTest {
40+
@Test
41+
public void getExecutor1() throws Exception {
42+
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" +
43+
Constants.THREAD_NAME_KEY + "=demo&" +
44+
Constants.CORE_THREADS_KEY + "=1&" +
45+
Constants.THREADS_KEY + "=2&" +
46+
Constants.ALIVE_KEY + "=1000&" +
47+
Constants.QUEUES_KEY + "=0");
48+
ThreadPool threadPool = new EagerThreadPool();
49+
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
50+
assertThat(executor, instanceOf(EagerThreadPoolExecutor.class));
51+
assertThat(executor.getCorePoolSize(), is(1));
52+
assertThat(executor.getMaximumPoolSize(), is(2));
53+
assertThat(executor.getKeepAliveTime(TimeUnit.MILLISECONDS), is(1000L));
54+
assertThat(executor.getQueue().remainingCapacity(), is(1));
55+
assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(TaskQueue.class));
56+
assertThat(executor.getRejectedExecutionHandler(),
57+
Matchers.<RejectedExecutionHandler>instanceOf(AbortPolicyWithReport.class));
58+
59+
final CountDownLatch latch = new CountDownLatch(1);
60+
executor.execute(new Runnable() {
61+
@Override
62+
public void run() {
63+
Thread thread = Thread.currentThread();
64+
assertThat(thread, instanceOf(InternalThread.class));
65+
assertThat(thread.getName(), startsWith("demo"));
66+
latch.countDown();
67+
}
68+
});
69+
70+
latch.await(5000, TimeUnit.MICROSECONDS);
71+
assertThat(latch.getCount(), is(0L));
72+
}
73+
74+
@Test
75+
public void getExecutor2() throws Exception {
76+
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + Constants.QUEUES_KEY + "=2");
77+
ThreadPool threadPool = new EagerThreadPool();
78+
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
79+
assertThat(executor.getQueue().remainingCapacity(), is(2));
80+
}
81+
82+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.common.threadpool.support.eager;
19+
20+
import org.junit.Test;
21+
import org.mockito.Mockito;
22+
23+
import java.util.concurrent.RejectedExecutionException;
24+
import java.util.concurrent.TimeUnit;
25+
26+
import static org.hamcrest.Matchers.is;
27+
import static org.junit.Assert.assertThat;
28+
import static org.mockito.Mockito.mock;
29+
30+
public class TaskQueueTest {
31+
32+
@Test(expected = RejectedExecutionException.class)
33+
public void testOffer1() throws Exception {
34+
TaskQueue<Runnable> queue = new TaskQueue<Runnable>(1);
35+
queue.offer(mock(Runnable.class));
36+
}
37+
38+
@Test
39+
public void testOffer2() throws Exception {
40+
TaskQueue<Runnable> queue = new TaskQueue<Runnable>(1);
41+
EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class);
42+
Mockito.when(executor.getPoolSize()).thenReturn(2);
43+
Mockito.when(executor.getSubmittedTaskCount()).thenReturn(1);
44+
queue.setExecutor(executor);
45+
assertThat(queue.offer(mock(Runnable.class)), is(true));
46+
}
47+
48+
@Test
49+
public void testOffer3() throws Exception {
50+
TaskQueue<Runnable> queue = new TaskQueue<Runnable>(1);
51+
EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class);
52+
Mockito.when(executor.getPoolSize()).thenReturn(2);
53+
Mockito.when(executor.getSubmittedTaskCount()).thenReturn(2);
54+
Mockito.when(executor.getMaximumPoolSize()).thenReturn(4);
55+
queue.setExecutor(executor);
56+
assertThat(queue.offer(mock(Runnable.class)), is(false));
57+
}
58+
59+
@Test
60+
public void testOffer4() throws Exception {
61+
TaskQueue<Runnable> queue = new TaskQueue<Runnable>(1);
62+
EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class);
63+
Mockito.when(executor.getPoolSize()).thenReturn(4);
64+
Mockito.when(executor.getSubmittedTaskCount()).thenReturn(4);
65+
Mockito.when(executor.getMaximumPoolSize()).thenReturn(4);
66+
queue.setExecutor(executor);
67+
assertThat(queue.offer(mock(Runnable.class)), is(true));
68+
}
69+
70+
@Test(expected = RejectedExecutionException.class)
71+
public void testRetryOffer1() throws Exception {
72+
TaskQueue<Runnable> queue = new TaskQueue<Runnable>(1);
73+
EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class);
74+
Mockito.when(executor.isShutdown()).thenReturn(true);
75+
queue.setExecutor(executor);
76+
queue.retryOffer(mock(Runnable.class), 1000, TimeUnit.MILLISECONDS);
77+
}
78+
79+
80+
@Test
81+
public void testRetryOffer2() throws Exception {
82+
TaskQueue<Runnable> queue = new TaskQueue<Runnable>(1);
83+
EagerThreadPoolExecutor executor = mock(EagerThreadPoolExecutor.class);
84+
Mockito.when(executor.isShutdown()).thenReturn(false);
85+
queue.setExecutor(executor);
86+
assertThat(queue.retryOffer(mock(Runnable.class), 1000, TimeUnit.MILLISECONDS), is(true));
87+
}
88+
89+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.common.threadpool.support.fixed;
19+
20+
import com.alibaba.dubbo.common.Constants;
21+
import com.alibaba.dubbo.common.URL;
22+
import com.alibaba.dubbo.common.threadlocal.InternalThread;
23+
import com.alibaba.dubbo.common.threadpool.ThreadPool;
24+
import com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport;
25+
import com.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool;
26+
import org.hamcrest.Matchers;
27+
import org.junit.Test;
28+
29+
import java.util.concurrent.BlockingQueue;
30+
import java.util.concurrent.CountDownLatch;
31+
import java.util.concurrent.LinkedBlockingQueue;
32+
import java.util.concurrent.RejectedExecutionHandler;
33+
import java.util.concurrent.SynchronousQueue;
34+
import java.util.concurrent.ThreadPoolExecutor;
35+
import java.util.concurrent.TimeUnit;
36+
37+
import static org.hamcrest.Matchers.instanceOf;
38+
import static org.hamcrest.Matchers.is;
39+
import static org.hamcrest.Matchers.startsWith;
40+
import static org.junit.Assert.assertThat;
41+
42+
public class FixedThreadPoolTest {
43+
@Test
44+
public void getExecutor1() throws Exception {
45+
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" +
46+
Constants.THREAD_NAME_KEY + "=demo&" +
47+
Constants.CORE_THREADS_KEY + "=1&" +
48+
Constants.THREADS_KEY + "=2&" +
49+
Constants.QUEUES_KEY + "=0");
50+
ThreadPool threadPool = new FixedThreadPool();
51+
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
52+
assertThat(executor.getCorePoolSize(), is(2));
53+
assertThat(executor.getMaximumPoolSize(), is(2));
54+
assertThat(executor.getKeepAliveTime(TimeUnit.MILLISECONDS), is(0L));
55+
assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(SynchronousQueue.class));
56+
assertThat(executor.getRejectedExecutionHandler(),
57+
Matchers.<RejectedExecutionHandler>instanceOf(AbortPolicyWithReport.class));
58+
59+
final CountDownLatch latch = new CountDownLatch(1);
60+
executor.execute(new Runnable() {
61+
@Override
62+
public void run() {
63+
Thread thread = Thread.currentThread();
64+
assertThat(thread, instanceOf(InternalThread.class));
65+
assertThat(thread.getName(), startsWith("demo"));
66+
latch.countDown();
67+
}
68+
});
69+
70+
latch.await(5000, TimeUnit.MICROSECONDS);
71+
assertThat(latch.getCount(), is(0L));
72+
}
73+
74+
@Test
75+
public void getExecutor2() throws Exception {
76+
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + Constants.QUEUES_KEY + "=1");
77+
ThreadPool threadPool = new FixedThreadPool();
78+
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor(url);
79+
assertThat(executor.getQueue(), Matchers.<BlockingQueue<Runnable>>instanceOf(LinkedBlockingQueue.class));
80+
}
81+
}

0 commit comments

Comments
 (0)