Skip to content

Commit

Permalink
[#3977]fix logs information not clear problem (#3999)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Oct 30, 2023
1 parent 4f59e71 commit 4ed65bf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ public synchronized SCBEngine run() {
try {
doRun();
waitStatusUp();
printServiceInfo();
} catch (TimeoutException e) {
LOGGER.warn("{}", e.getMessage());
} catch (Throwable e) {
Expand All @@ -332,8 +333,6 @@ public synchronized SCBEngine run() {
}
status = SCBStatus.FAILED;
throw new IllegalStateException("ServiceComb init failed.", e);
} finally {
printServiceInfo();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,20 @@
import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.core.Transport;
import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
import org.apache.servicecomb.registry.api.registry.MicroserviceInstance;
import org.junit.Test;

import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class TestTransportManager {
@Test
public void testTransportManagerInitFail(@Mocked SCBEngine scbEngine, @Injectable Transport transport)
public void testTransportManagerInitFail()
throws Exception {
new Expectations() {
{
transport.getName();
result = "test";
transport.init();
result = false;
transport.canInit();
result = true;
}
};
SCBEngine scbEngine = Mockito.mock(SCBEngine.class);
Transport transport = Mockito.mock(Transport.class);
Mockito.when(transport.getName()).thenReturn("test");
Mockito.when(transport.init()).thenReturn(false);
Mockito.when(transport.canInit()).thenReturn(true);
List<Transport> transports = Arrays.asList(transport);

TransportManager manager = new TransportManager();
manager.addTransportsBeforeInit(transports);

Expand All @@ -57,20 +47,14 @@ public void testTransportManagerInitFail(@Mocked SCBEngine scbEngine, @Injectabl
}

@Test
public void testTransportManagerInitSucc(@Mocked SCBEngine scbEngine, @Injectable Transport transport,
@Injectable Endpoint endpoint, @Injectable MicroserviceInstance instance) throws Exception {
new Expectations() {
{
transport.getName();
result = "test";
transport.canInit();
result = true;
transport.init();
result = true;
transport.getPublishEndpoint();
result = endpoint;
}
};
public void testTransportManagerInitSuccess() throws Exception {
SCBEngine scbEngine = Mockito.mock(SCBEngine.class);
Transport transport = Mockito.mock(Transport.class);
Endpoint endpoint = Mockito.mock(Endpoint.class);
Mockito.when(transport.getName()).thenReturn("test");
Mockito.when(transport.init()).thenReturn(true);
Mockito.when(transport.canInit()).thenReturn(true);
Mockito.when(transport.getPublishEndpoint()).thenReturn(endpoint);
List<Transport> transports = Arrays.asList(transport);

TransportManager manager = new TransportManager();
Expand All @@ -81,42 +65,31 @@ public void testTransportManagerInitSucc(@Mocked SCBEngine scbEngine, @Injectabl
}

@Test
public void testGroupByName(@Mocked Transport t1, @Mocked Transport t2_1, @Mocked Transport t2_2) {
new Expectations() {
{
t1.getName();
result = "t1";

t2_1.getName();
result = "t2";
t2_2.getName();
result = "t2";
}
};

public void testGroupByName() {
Transport t1 = Mockito.mock(Transport.class);
Mockito.when(t1.getName()).thenReturn("t1");
Transport t21 = Mockito.mock(Transport.class);
Mockito.when(t21.getName()).thenReturn("t2");
Transport t22 = Mockito.mock(Transport.class);
Mockito.when(t22.getName()).thenReturn("t2");
TransportManager manager = new TransportManager();
manager.addTransportsBeforeInit(Arrays.asList(t1, t2_1, t2_2));
manager.addTransportsBeforeInit(Arrays.asList(t1, t21, t22));

Map<String, List<Transport>> groups = manager.groupByName();
Assertions.assertEquals(2, groups.size());
Assertions.assertEquals(1, groups.get("t1").size());
Assertions.assertEquals(t1, groups.get("t1").get(0));
Assertions.assertEquals(2, groups.get("t2").size());
Assertions.assertEquals(t2_1, groups.get("t2").get(0));
Assertions.assertEquals(t2_2, groups.get("t2").get(1));
Assertions.assertEquals(t21, groups.get("t2").get(0));
Assertions.assertEquals(t22, groups.get("t2").get(1));
}

@Test
public void testCheckTransportGroupInvalid(@Mocked Transport t1, @Mocked Transport t2) {
new Expectations() {
{
t1.getOrder();
result = 1;

t2.getOrder();
result = 1;
}
};
public void testCheckTransportGroupInvalid() {
Transport t1 = Mockito.mock(Transport.class);
Mockito.when(t1.getOrder()).thenReturn(1);
Transport t2 = Mockito.mock(Transport.class);
Mockito.when(t2.getOrder()).thenReturn(1);

TransportManager manager = new TransportManager();
List<Transport> group = Arrays.asList(t1, t2);
Expand All @@ -125,23 +98,16 @@ public void testCheckTransportGroupInvalid(@Mocked Transport t1, @Mocked Transpo
manager.checkTransportGroup(group);
Assertions.fail("must throw exception");
} catch (ServiceCombException e) {
Assertions.assertEquals(
"org.apache.servicecomb.core.$Impl_Transport and org.apache.servicecomb.core.$Impl_Transport have the same order 1",
e.getMessage());
Assertions.assertTrue(e.getMessage().contains("have the same order"));
}
}

@Test
public void testCheckTransportGroupValid(@Mocked Transport t1, @Mocked Transport t2) {
new Expectations() {
{
t1.getOrder();
result = 1;

t2.getOrder();
result = 2;
}
};
public void testCheckTransportGroupValid() {
Transport t1 = Mockito.mock(Transport.class);
Mockito.when(t1.getOrder()).thenReturn(1);
Transport t2 = Mockito.mock(Transport.class);
Mockito.when(t2.getOrder()).thenReturn(2);

TransportManager manager = new TransportManager();
List<Transport> group = Arrays.asList(t1, t2);
Expand All @@ -154,18 +120,12 @@ public void testCheckTransportGroupValid(@Mocked Transport t1, @Mocked Transport
}

@Test
public void testChooseOneTransportFirst(@Mocked Transport t1, @Mocked Transport t2) {
new Expectations() {
{
t1.getOrder();
result = 1;
t1.canInit();
result = true;

t2.getOrder();
result = 2;
}
};
public void testChooseOneTransportFirst() {
Transport t1 = Mockito.mock(Transport.class);
Mockito.when(t1.getOrder()).thenReturn(1);
Mockito.when(t1.canInit()).thenReturn(true);
Transport t2 = Mockito.mock(Transport.class);
Mockito.when(t2.getOrder()).thenReturn(2);

TransportManager manager = new TransportManager();
List<Transport> group = Arrays.asList(t1, t2);
Expand All @@ -174,44 +134,28 @@ public void testChooseOneTransportFirst(@Mocked Transport t1, @Mocked Transport
}

@Test
public void testChooseOneTransportSecond(@Mocked Transport t1, @Mocked Transport t2) {
new Expectations() {
{
t1.getOrder();
result = Integer.MAX_VALUE;
t1.canInit();
result = true;

t2.getOrder();
result = -1000;
t2.canInit();
result = false;
}
};

public void testChooseOneTransportSecond() {
Transport t1 = Mockito.mock(Transport.class);
Mockito.when(t1.getOrder()).thenReturn(Integer.MAX_VALUE);
Mockito.when(t1.canInit()).thenReturn(true);
Transport t2 = Mockito.mock(Transport.class);
Mockito.when(t2.getOrder()).thenReturn(-1000);
Mockito.when(t2.canInit()).thenReturn(false);
TransportManager manager = new TransportManager();
List<Transport> group = Arrays.asList(t1, t2);

Assertions.assertEquals(t1, manager.chooseOneTransport(group));
}

@Test
public void testChooseOneTransportNone(@Mocked Transport t1, @Mocked Transport t2) {
new Expectations() {
{
t1.getName();
result = "t";
t1.getOrder();
result = 1;
t1.canInit();
result = false;

t2.getOrder();
result = 2;
t2.canInit();
result = false;
}
};
public void testChooseOneTransportNone() {
Transport t1 = Mockito.mock(Transport.class);
Mockito.when(t1.getName()).thenReturn("t");
Mockito.when(t1.getOrder()).thenReturn(1);
Mockito.when(t1.canInit()).thenReturn(false);
Transport t2 = Mockito.mock(Transport.class);
Mockito.when(t2.getOrder()).thenReturn(2);
Mockito.when(t2.canInit()).thenReturn(false);

TransportManager manager = new TransportManager();
List<Transport> group = Arrays.asList(t1, t2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,14 @@ private static boolean isFileExists(String name) {
return true;
}

ClassLoader classLoader =
Thread.currentThread().getContextClassLoader() == null ? VertxTLSBuilder.class.getClassLoader()
: Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource(name);
return resource != null;
try {
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader() == null ? VertxTLSBuilder.class.getClassLoader()
: Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource(name);
return resource != null;
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Map.Entry;

import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.registry.api.registry.MicroserviceInstance;
Expand Down Expand Up @@ -115,6 +116,9 @@ public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode p
// Create new child. And all later DiscoveryFilter will re-calculate based on this result.
DiscoveryTreeNode child = new DiscoveryTreeNode().subName(parent, KEY_ISOLATED).data(result);
parent.child(KEY_ISOLATED, child);
Invocation invocation = context.getInputParameters();
LOGGER.info("instance isolation discovery filter changed, current cached size {}/{}/{}",
invocation.getAppId(), invocation.getMicroserviceName(), result.size());
return child;
}
}

0 comments on commit 4ed65bf

Please sign in to comment.