Skip to content

Commit

Permalink
fix * imports issue (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
htynkn authored and beiwei30 committed May 3, 2018
1 parent db039c6 commit 04ffae0
Show file tree
Hide file tree
Showing 16 changed files with 1,093 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ coverage:
default:
threshold: 0.1%
ignore:
- "dubbo-demo/.*"
- "dubbo-demo/.*"
- "dubbo-common/src/main/java/com/alibaba/dubbo/common/json/*.java" # internal JSON impl is deprecate, ignore test coverage for them
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.common.concurrent;

import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class ExecutionListTest {
private ExecutionList executionList;

@Before
public void setUp() throws Exception {
this.executionList = new ExecutionList();
}

@Test(expected = NullPointerException.class)
public void testAddNullRunnable() {
this.executionList.add(null, mock(Executor.class));
}

@Test
public void testAddRunnableToExecutor() {
Executor mockedExecutor = mock(Executor.class);

this.executionList.add(mock(Runnable.class), mockedExecutor);
this.executionList.execute();

verify(mockedExecutor).execute(any(Runnable.class));
}

@Test
public void testExecuteRunnableWithDefaultExecutor() throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(1);
this.executionList.add(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
}
}, null);

this.executionList.execute();
countDownLatch.await();
}

@Test
public void testExceptionForExecutor() {
Executor mockedExecutor = mock(Executor.class);
doThrow(new RuntimeException()).when(mockedExecutor).execute(any(Runnable.class));

this.executionList.add(mock(Runnable.class), mockedExecutor);
this.executionList.execute();
}

@Test
public void testNotRunSameRunnableTwice() {
Executor mockedExecutor = mock(Executor.class);

this.executionList.add(mock(Runnable.class), mockedExecutor);

this.executionList.execute();
this.executionList.execute();

verify(mockedExecutor).execute(any(Runnable.class));
}

@Test
public void testRunImmediatelyAfterExecuted() {
Executor mockedExecutor = mock(Executor.class);

this.executionList.add(mock(Runnable.class), mockedExecutor);
this.executionList.execute();
this.executionList.add(mock(Runnable.class), mockedExecutor);

verify(mockedExecutor, times(2)).execute(any(Runnable.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.common.concurrent;

import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class ListenableFutureTaskTest {
@Test
public void testCreate() throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(1);
ListenableFutureTask<Boolean> futureTask = ListenableFutureTask.create(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
countDownLatch.countDown();
return true;
}
});
futureTask.run();
countDownLatch.await();
}

@Test
public void testRunnableResponse() throws ExecutionException, InterruptedException {
ListenableFutureTask<Boolean> futureTask = ListenableFutureTask.create(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, true);
futureTask.run();

Boolean result = futureTask.get();
assertThat(result, is(true));
}

@Test
public void testListener() throws InterruptedException {
ListenableFutureTask<String> futureTask = ListenableFutureTask.create(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(500);
return "hello";
}
});
final CountDownLatch countDownLatch = new CountDownLatch(1);
futureTask.addListener(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
}
});
futureTask.run();
countDownLatch.await();
}


@Test
public void testCustomExecutor() {
Executor mockedExecutor = mock(Executor.class);
ListenableFutureTask<Integer> futureTask = ListenableFutureTask.create(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 0;
}
});
futureTask.addListener(mock(Runnable.class), mockedExecutor);
futureTask.run();

verify(mockedExecutor).execute(any(Runnable.class));
}
}
Loading

0 comments on commit 04ffae0

Please sign in to comment.