Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AbstractLoadBalancer(ConsumerBootstrap consumerBootstrap) {

@Override
public ProviderInfo select(SofaRequest request, List<ProviderInfo> providerInfos) throws SofaRpcException {
if (providerInfos.size() == 0) {
if (providerInfos.isEmpty()) {
throw noAvailableProviderException(request.getTargetServiceUniqueName());
}
if (providerInfos.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected boolean isMatch(String invokerId) {
return true;
} else {
//如果没有排除,那么只生效指定id,其余不生效。
if (excludeId.size() == 0) {
if (excludeId.isEmpty()) {
return effectiveId.contains(invokerId);
//如果有排除,那么除排除id外,其余都生效。
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -55,14 +54,12 @@ public class RouterChain {
/**
* 服务端自动激活的 {"alias":ExtensionClass}
*/
private final static Map<String, ExtensionClass<Router>> PROVIDER_AUTO_ACTIVES = Collections
.synchronizedMap(new ConcurrentHashMap<String, ExtensionClass<Router>>());
private final static Map<String, ExtensionClass<Router>> PROVIDER_AUTO_ACTIVES = new ConcurrentHashMap<String, ExtensionClass<Router>>();

/**
* 调用端自动激活的 {"alias":ExtensionClass}
*/
private final static Map<String, ExtensionClass<Router>> CONSUMER_AUTO_ACTIVES = Collections
.synchronizedMap(new ConcurrentHashMap<String, ExtensionClass<Router>>());
private final static Map<String, ExtensionClass<Router>> CONSUMER_AUTO_ACTIVES = new ConcurrentHashMap<String, ExtensionClass<Router>>();

/**
* 扩展加载器
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public static synchronized void unSubscribe(String key, RpcConfigListener listen
List<RpcConfigListener> listeners = CFG_LISTENER.get(key);
if (listeners != null) {
listeners.remove(listener);
if (listeners.size() == 0) {
if (listeners.isEmpty()) {
CFG_LISTENER.remove(key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ public void putHeadCache(Short key, String value) {
* @param key the key
* @param value the value
*/
public void invalidateHeadCache(Byte key, String value) {
if (headerCache != null && headerCache.containsKey(key)) {
public void invalidateHeadCache(Short key, String value) {
if (headerCache != null) {
String old = headerCache.get(key);
if (!old.equals(value)) {
throw new SofaRpcRuntimeException("Value of old is not match current");
if (old != null) {
if (!old.equals(value)) {
throw new SofaRpcRuntimeException("Value of old is not match current");
}
headerCache.remove(key);
}
headerCache.remove(key);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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.alipay.sofa.rpc.transport;

import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import org.junit.Assert;
import org.junit.Test;

/**
* Unit tests for {@link ChannelContext}.
*/
public class ChannelContextTest {

@Test
public void testPutAndGetHeadCache() {
ChannelContext context = new ChannelContext();
// headerCache is null initially
Assert.assertNull(context.getHeader((short) 1));

context.putHeadCache((short) 1, "testValue");
Assert.assertEquals("testValue", context.getHeader((short) 1));
}

@Test
public void testPutHeadCacheDoesNotOverwrite() {
ChannelContext context = new ChannelContext();
context.putHeadCache((short) 1, "first");
context.putHeadCache((short) 1, "second");
// Should keep the first value since containsKey is checked
Assert.assertEquals("first", context.getHeader((short) 1));
}

@Test
public void testGetHeaderKey() {
ChannelContext context = new ChannelContext();
context.putHeadCache((short) 1, "testValue");
Assert.assertEquals(Short.valueOf((short) 1), context.getHeaderKey("testValue"));
Assert.assertNull(context.getHeaderKey("nonexistent"));
}

@Test
public void testInvalidateHeadCacheSuccess() {
ChannelContext context = new ChannelContext();
context.putHeadCache((short) 1, "testValue");
Assert.assertEquals("testValue", context.getHeader((short) 1));

// Invalidate with matching value should succeed
context.invalidateHeadCache((short) 1, "testValue");
Assert.assertNull(context.getHeader((short) 1));
}

@Test(expected = SofaRpcRuntimeException.class)
public void testInvalidateHeadCacheMismatch() {
ChannelContext context = new ChannelContext();
context.putHeadCache((short) 1, "testValue");

// Invalidate with non-matching value should throw
context.invalidateHeadCache((short) 1, "wrongValue");
}

@Test
public void testInvalidateHeadCacheNonExistentKey() {
ChannelContext context = new ChannelContext();
context.putHeadCache((short) 1, "testValue");

// Invalidate a key that doesn't exist should be a no-op
context.invalidateHeadCache((short) 99, "anyValue");
Assert.assertEquals("testValue", context.getHeader((short) 1));
}

@Test
public void testInvalidateHeadCacheWhenCacheIsNull() {
ChannelContext context = new ChannelContext();
// Should not throw when headerCache is null
context.invalidateHeadCache((short) 1, "value");
}

@Test
public void testGetAvailableRefIndexConsumerToProvider() {
ChannelContext context = new ChannelContext();
Short index = context.getAvailableRefIndex(true);
Assert.assertNotNull(index);
Assert.assertEquals(Short.valueOf((short) 0), index);

// After putting key 0, next available should be 1
context.putHeadCache((short) 0, "val0");
index = context.getAvailableRefIndex(true);
Assert.assertEquals(Short.valueOf((short) 1), index);
}

@Test
public void testGetAvailableRefIndexProviderToConsumer() {
ChannelContext context = new ChannelContext();
Short index = context.getAvailableRefIndex(false);
Assert.assertNotNull(index);
Assert.assertEquals(Short.valueOf((short) -1), index);

// After putting key -1, next available should be -2
context.putHeadCache((short) -1, "val-1");
index = context.getAvailableRefIndex(false);
Assert.assertEquals(Short.valueOf((short) -2), index);
}

@Test
public void testGetHeaderWithNullKey() {
ChannelContext context = new ChannelContext();
Assert.assertNull(context.getHeader(null));
}

@Test
public void testGetHeaderKeyWithEmpty() {
ChannelContext context = new ChannelContext();
Assert.assertNull(context.getHeaderKey(""));
Assert.assertNull(context.getHeaderKey(null));
}

@Test
public void testSettersAndGetters() {
ChannelContext context = new ChannelContext();

context.setDstVersion(100);
Assert.assertEquals(Integer.valueOf(100), context.getDstVersion());

context.setClientAppId("appId");
Assert.assertEquals("appId", context.getClientAppId());

context.setClientAppName("appName");
Assert.assertEquals("appName", context.getClientAppName());

context.setClientInstanceId("instanceId");
Assert.assertEquals("instanceId", context.getClientInstanceId());

context.setProtocol("bolt");
Assert.assertEquals("bolt", context.getProtocol());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void run(Executor executor) {
int i = 0;
boolean flushedOnce = false;
while ((item = snapshot.poll()) != null) {
if (snapshot.size() == 0) {
if (snapshot.isEmpty()) {
flushedOnce = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void unSubscribe(ConsumerConfig config) {
List<ConsumerConfig> listeners = notifyListeners.get(key);
if (listeners != null) {
listeners.remove(config);
if (listeners.size() == 0) {
if (listeners.isEmpty()) {
notifyListeners.remove(key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void unRegisterProcessor(ProviderConfig providerConfig, boolean closeIfNo
String key = getUniqueName(providerConfig);
serverHandler.getInvokerMap().remove(key);
// 如果最后一个需要关闭,则关闭
if (closeIfNoEntry && serverHandler.getInvokerMap().size() == 0) {
if (closeIfNoEntry && serverHandler.getInvokerMap().isEmpty()) {
stop();
}
}
Expand Down
Loading