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

add unit test to dubbo-registry/dubbo-registry-api and dubbo-registry/dubbo-registry-default #3053

Merged
merged 4 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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 org.apache.dubbo.registry.retry;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.RegistryService;
import org.apache.dubbo.registry.support.FailbackRegistry;

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

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.mock;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class FailedRetryTaskTest {

private URL url;

private NotifyListener notifyListener;

@Before
public void setUp() {
url = new URL(Constants.REGISTRY_PROTOCOL, NetUtils.getLocalHost(), NetUtils.getAvailablePort())
.setServiceInterface(RegistryService.class.getName())
.addParameter(Constants.CHECK_KEY, false)
.addParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, 1000);
notifyListener = mock(NotifyListener.class);
}

@Test
public void testFailedRegisteredTask() {
FailedMockRegistry failedMockRegistry = new FailedMockRegistry(url, "register");
failedMockRegistry.register(url);
assertEquals(1, failedMockRegistry.getFailedRegistered().size());
doWait();
assertEquals(0, failedMockRegistry.getFailedRegistered().size());
assertEquals(1, failedMockRegistry.getRegistered().size());
}

@Test
public void testFailedUnRegisteredTask() {
FailedMockRegistry failedMockRegistry = new FailedMockRegistry(url, "unregister");
failedMockRegistry.register(url);
assertEquals(1, failedMockRegistry.getRegistered().size());
failedMockRegistry.unregister(url);
assertEquals(1, failedMockRegistry.getFailedUnregistered().size());
doWait();
assertEquals(0, failedMockRegistry.getFailedUnregistered().size());
assertEquals(0, failedMockRegistry.getRegistered().size());
}

@Test
public void testFailedSubscribeTask() {
FailedMockRegistry failedMockRegistry = new FailedMockRegistry(url, "subscribe");
failedMockRegistry.register(url);
assertEquals(1, failedMockRegistry.getRegistered().size());
failedMockRegistry.subscribe(url, mock(NotifyListener.class));
assertEquals(1, failedMockRegistry.getFailedSubscribed().size());
doWait();
assertEquals(0, failedMockRegistry.getFailedUnsubscribed().size());
}

@Test
public void testFailedUnsubscribeTask() {
FailedMockRegistry failedMockRegistry = new FailedMockRegistry(url, "unsubscribe");
failedMockRegistry.register(url);
assertEquals(1, failedMockRegistry.getRegistered().size());
NotifyListener notifyListener = mock(NotifyListener.class);
failedMockRegistry.subscribe(url, notifyListener);
assertEquals(1, failedMockRegistry.getSubscribed().size());
failedMockRegistry.unsubscribe(url, notifyListener);
assertEquals(1, failedMockRegistry.getFailedUnsubscribed().size());
doWait();
assertEquals(0, failedMockRegistry.getFailedUnsubscribed().size());
}

private void deal(AtomicInteger count) {
if (count.get() != 0) {
count.decrementAndGet();
throw new RuntimeException("Retry task, please ignore ...");
}
}

public void doWait() {
long period = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD)
+ 1000 * 2;
try {
Thread.sleep(period);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

private class FailedMockRegistry extends FailbackRegistry {

private AtomicInteger count = new AtomicInteger(1);

private List<String> options;

public FailedMockRegistry(URL url, String option) {
super(url);
if (StringUtils.isBlank(option)) {
throw new IllegalArgumentException("The argument option can not be null");
}
options = Arrays.asList(option.split(","));
}

@Override
public void doRegister(URL url) {
if (options.contains("register")) {
deal(count);
}
}

@Override
public void doUnregister(URL url) {
if (options.contains("unregister")) {
deal(count);
}
}

@Override
public void doSubscribe(URL url, NotifyListener listener) {
if (options.contains("subscribe"))
deal(count);
}

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
if (options.contains("unsubscribe")) {
deal(count);
}
}

@Override
public boolean isAvailable() {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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 org.apache.dubbo.registry.dubbo;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.RegistryService;
import org.apache.dubbo.registry.support.FailbackRegistry;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;

import static org.junit.Assert.assertEquals;

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

import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;

public class DubboRegistryTest {

private static final Logger logger = LoggerFactory.getLogger(DubboRegistryTest.class);

private DubboRegistry dubboRegistry;

private URL registryURL;

private URL serviceURL;

private NotifyListener notifyListener;

private Invoker<RegistryService> invoker;

private RegistryService registryService;

@Before
public void setUp() {
registryURL = new URL(Constants.REGISTRY_PROTOCOL, NetUtils.getLocalHost(), NetUtils.getAvailablePort())
.addParameter(Constants.CHECK_KEY, false)
.setServiceInterface(RegistryService.class.getName());
serviceURL = new URL(DubboProtocol.NAME, NetUtils.getLocalHost(), NetUtils.getAvailablePort())
.addParameter(Constants.CHECK_KEY, false)
.setServiceInterface(RegistryService.class.getName());

registryService = new MockDubboRegistry(registryURL);

invoker = mock(Invoker.class);
given(invoker.getUrl()).willReturn(serviceURL);
given(invoker.getInterface()).willReturn(RegistryService.class);
given(invoker.invoke(new RpcInvocation())).willReturn(null);

dubboRegistry = new DubboRegistry(invoker, registryService);
notifyListener = mock(NotifyListener.class);
}

@Test
public void testRegister() {
dubboRegistry.register(serviceURL);
assertEquals(1, getRegistereds());
}

@Test
public void testUnRegister() {
assertEquals(0, getRegistereds());
dubboRegistry.register(serviceURL);
assertEquals(1, getRegistereds());
dubboRegistry.unregister(serviceURL);
assertEquals(0, getRegistereds());
}

@Test
public void testSubscribe() {
dubboRegistry.register(serviceURL);
assertEquals(1, getRegistereds());
dubboRegistry.subscribe(serviceURL, notifyListener);
assertEquals(1, getSubscribeds());
assertEquals(1, getNotifiedListeners());
}

@Test
public void testUnsubscribe() {
dubboRegistry.subscribe(serviceURL, notifyListener);
assertEquals(1, getSubscribeds());
assertEquals(1, getNotifiedListeners());
dubboRegistry.unsubscribe(serviceURL, notifyListener);
assertEquals(0, getNotifiedListeners());
}

private class MockDubboRegistry extends FailbackRegistry {

private volatile boolean isAvaliable = false;

public MockDubboRegistry(URL url) {
super(url);
}

@Override
public void doRegister(URL url) {
logger.info("Begin to register: " + url);
isAvaliable = true;
}

@Override
public void doUnregister(URL url) {
logger.info("Begin to ungister: " + url);
isAvaliable = false;
}

@Override
public void doSubscribe(URL url, NotifyListener listener) {
logger.info("Begin to subscribe: " + url);
}

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
logger.info("Begin to unSubscribe: " + url);
}

@Override
public boolean isAvailable() {
return isAvaliable;
}
}

private int getNotifiedListeners() {
return dubboRegistry.getSubscribed().get(serviceURL).size();
}

private int getRegistereds() {
return dubboRegistry.getRegistered().size();
}

private int getSubscribeds() {
return dubboRegistry.getSubscribed().size();
}
}