Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-1681] add test for dubbo common module #1721

Merged
merged 1 commit into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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