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+ package com .alibaba .dubbo .common .concurrent ;
18+
19+ import org .junit .Before ;
20+ import org .junit .Test ;
21+
22+ import java .util .concurrent .CountDownLatch ;
23+ import java .util .concurrent .Executor ;
24+
25+ import static org .mockito .ArgumentMatchers .any ;
26+ import static org .mockito .Mockito .doThrow ;
27+ import static org .mockito .Mockito .mock ;
28+ import static org .mockito .Mockito .times ;
29+ import static org .mockito .Mockito .verify ;
30+
31+ public class ExecutionListTest {
32+ private ExecutionList executionList ;
33+
34+ @ Before
35+ public void setUp () throws Exception {
36+ this .executionList = new ExecutionList ();
37+ }
38+
39+ @ Test (expected = NullPointerException .class )
40+ public void testAddNullRunnable () {
41+ this .executionList .add (null , mock (Executor .class ));
42+ }
43+
44+ @ Test
45+ public void testAddRunnableToExecutor () {
46+ Executor mockedExecutor = mock (Executor .class );
47+
48+ this .executionList .add (mock (Runnable .class ), mockedExecutor );
49+ this .executionList .execute ();
50+
51+ verify (mockedExecutor ).execute (any (Runnable .class ));
52+ }
53+
54+ @ Test
55+ public void testExecuteRunnableWithDefaultExecutor () throws InterruptedException {
56+ final CountDownLatch countDownLatch = new CountDownLatch (1 );
57+ this .executionList .add (new Runnable () {
58+ @ Override
59+ public void run () {
60+ countDownLatch .countDown ();
61+ }
62+ }, null );
63+
64+ this .executionList .execute ();
65+ countDownLatch .await ();
66+ }
67+
68+ @ Test
69+ public void testExceptionForExecutor () {
70+ Executor mockedExecutor = mock (Executor .class );
71+ doThrow (new RuntimeException ()).when (mockedExecutor ).execute (any (Runnable .class ));
72+
73+ this .executionList .add (mock (Runnable .class ), mockedExecutor );
74+ this .executionList .execute ();
75+ }
76+
77+ @ Test
78+ public void testNotRunSameRunnableTwice () {
79+ Executor mockedExecutor = mock (Executor .class );
80+
81+ this .executionList .add (mock (Runnable .class ), mockedExecutor );
82+
83+ this .executionList .execute ();
84+ this .executionList .execute ();
85+
86+ verify (mockedExecutor ).execute (any (Runnable .class ));
87+ }
88+
89+ @ Test
90+ public void testRunImmediatelyAfterExecuted () {
91+ Executor mockedExecutor = mock (Executor .class );
92+
93+ this .executionList .add (mock (Runnable .class ), mockedExecutor );
94+ this .executionList .execute ();
95+ this .executionList .add (mock (Runnable .class ), mockedExecutor );
96+
97+ verify (mockedExecutor , times (2 )).execute (any (Runnable .class ));
98+ }
99+ }
0 commit comments