Skip to content

Commit

Permalink
[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module (
Browse files Browse the repository at this point in the history
…#1982)

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module

* fix unit test failure

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module
  • Loading branch information
beiwei30 authored Jun 25, 2018
1 parent f186f2b commit eee0be3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void startQosServer(URL url) {

int port = Integer.parseInt(url.getParameter(QOS_PORT,"22222"));
boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP,"true"));
Server server = org.apache.dubbo.qos.server.Server.getInstance();
Server server = Server.getInstance();
server.setPort(port);
server.setAcceptForeignIp(acceptForeignIp);
server.start();
Expand All @@ -92,4 +92,11 @@ private void startQosServer(URL url) {
//throw new RpcException("fail to start qos server", throwable);
}
}

/*package*/ void stopServer() {
if (hasStarted.compareAndSet(true, false)) {
Server server = Server.getInstance();
server.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private Server() {

private String welcome;

private AtomicBoolean hasStarted = new AtomicBoolean();
private AtomicBoolean started = new AtomicBoolean();

/**
* welcome message
Expand All @@ -78,7 +78,7 @@ public int getPort() {
* start server, bind port
*/
public void start() throws Throwable {
if (!hasStarted.compareAndSet(false, true)) {
if (!started.compareAndSet(false, true)) {
return;
}
boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true));
Expand Down Expand Up @@ -132,4 +132,8 @@ public void setAcceptForeignIp(boolean acceptForeignIp) {
public String getWelcome() {
return welcome;
}

public boolean isStarted() {
return started.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.apache.dubbo.qos.protocol;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.qos.command.BaseCommand;
import org.apache.dubbo.qos.server.Server;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class QosProtocolWrapperTest {
private URL url = Mockito.mock(URL.class);
private Invoker invoker = mock(Invoker.class);
private Protocol protocol = mock(Protocol.class);
private QosProtocolWrapper wrapper = new QosProtocolWrapper(protocol);
private Server server = Server.getInstance();

@Before
public void setUp() throws Exception {
when(url.getParameter(Constants.QOS_ENABLE, "true")).thenReturn("true");
when(url.getParameter(Constants.QOS_PORT, "22222")).thenReturn("12345");
when(url.getParameter(Constants.ACCEPT_FOREIGN_IP, "true")).thenReturn("false");
when(invoker.getUrl()).thenReturn(url);
when(url.getProtocol()).thenReturn(Constants.REGISTRY_PROTOCOL);
}

@After
public void tearDown() throws Exception {
if (server.isStarted()) {
server.stop();
}
}

@Test
public void testExport() throws Exception {
wrapper.export(invoker);
assertThat(server.isStarted(), is(true));
assertThat(server.getPort(), is(12345));
assertThat(server.isAcceptForeignIp(), is(false));
verify(protocol).export(invoker);
}

@Test
public void testRefer() throws Exception {
wrapper.refer(BaseCommand.class, url);
assertThat(server.isStarted(), is(true));
assertThat(server.getPort(), is(12345));
assertThat(server.isAcceptForeignIp(), is(false));
verify(protocol).refer(BaseCommand.class, url);
}
}

0 comments on commit eee0be3

Please sign in to comment.