Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qmwu2000 committed Jun 24, 2022
1 parent be00efd commit 6d735e7
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 559 deletions.
106 changes: 57 additions & 49 deletions cat-client/src/main/java/com/dianping/cat/Cat.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.dianping.cat.message.Event;
import com.dianping.cat.message.Heartbeat;
import com.dianping.cat.message.MessageTree;
import com.dianping.cat.message.Metric;
import com.dianping.cat.message.Trace;
import com.dianping.cat.message.Transaction;
Expand Down Expand Up @@ -205,9 +206,9 @@ public static void logMetricForSum(String name, double sum, int quantity) {
* @param domain
* domain is default, if use default config, the performance of server storage is bad。
*/
// public static void logRemoteCallClient(Context ctx) {
// logRemoteCallClient(ctx, "default");
// }
public static void logRemoteCallClient(Context ctx) {
logRemoteCallClient(ctx, null);
}

/**
* logRemoteCallClient is used in rpc client
Expand All @@ -217,59 +218,54 @@ public static void logMetricForSum(String name, double sum, int quantity) {
* @param domain
* domain is project name of rpc server name
*/
// public static void logRemoteCallClient(Context ctx, String domain) {
// try {
// MessageTree tree = getManager().getThreadLocalMessageTree();
// String messageId = tree.getMessageId();
//
// if (messageId == null) {
// messageId = Cat.createMessageId();
// tree.setMessageId(messageId);
// }
//
// String childId = CAT.m_factory.getNextId(domain);
// Cat.logEvent(CatClientConstants.TYPE_REMOTE_CALL, "", Event.SUCCESS, childId);
//
// String root = tree.getRootMessageId();
//
// if (root == null) {
// root = messageId;
// }
//
// ctx.addProperty(Context.ROOT, root);
// ctx.addProperty(Context.PARENT, messageId);
// ctx.addProperty(Context.CHILD, childId);
// } catch (Exception e) {
// errorHandler(e);
// }
// }
public static void logRemoteCallClient(Context ctx, String domain) {
try {
MessageTree tree = TraceContextHelper.threadLocal().getMessageTree();
String messageId = tree.getMessageId();
String childId = TraceContextHelper.createMessageId(domain);

Cat.logEvent(CatClientConstants.TYPE_REMOTE_CALL, "", Event.SUCCESS, childId);

String root = tree.getRootMessageId();

if (root == null) {
root = messageId;
}

ctx.addProperty(Context.ROOT, root);
ctx.addProperty(Context.PARENT, messageId);
ctx.addProperty(Context.CHILD, childId);
} catch (Exception e) {
errorHandler(e);
}
}

/**
* used in rpc server,use clild id as server message tree id.
*
* @param ctx
* ctx is rpc context ,such as duboo context , please use rpc context implement Context
*/
// public static void logRemoteCallServer(Context ctx) {
// try {
// MessageTree tree = getManager().getThreadLocalMessageTree();
// String childId = ctx.getProperty(Context.CHILD);
// String rootId = ctx.getProperty(Context.ROOT);
// String parentId = ctx.getProperty(Context.PARENT);
//
// if (parentId != null) {
// tree.setParentMessageId(parentId);
// }
// if (rootId != null) {
// tree.setRootMessageId(rootId);
// }
// if (childId != null) {
// tree.setMessageId(childId);
// }
// } catch (Exception e) {
// errorHandler(e);
// }
// }
public static void logRemoteCallServer(Context ctx) {
try {
MessageTree tree = TraceContextHelper.threadLocal().getMessageTree();
String childId = ctx.getProperty(Context.CHILD);
String rootId = ctx.getProperty(Context.ROOT);
String parentId = ctx.getProperty(Context.PARENT);

if (parentId != null) {
tree.setParentMessageId(parentId);
}
if (rootId != null) {
tree.setRootMessageId(rootId);
}
if (childId != null) {
tree.setMessageId(childId);
}
} catch (Exception e) {
errorHandler(e);
}
}

public static void logTrace(String type, String name) {
try {
Expand Down Expand Up @@ -328,4 +324,16 @@ public static Transaction newTransaction(String type, String name) {
return NullMessage.TRANSACTION;
}
}

public static interface Context {
public final String ROOT = "_catRootMessageId";

public final String PARENT = "_catParentMessageId";

public final String CHILD = "_catChildMessageId";

public void addProperty(String key, String value);

public String getProperty(String key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public DefaultComponentContext() {
registerFactory(new SystemComponentFactory());

m_lifecycle = new DefaultComponentLifecycle(this);
m_lifecycle.initialize(this);
}

@Override
Expand All @@ -41,8 +40,6 @@ public void dispose() {
m_lifecycle.onStop(factory);
}

m_lifecycle.dispose();

m_singletons.clear();
m_factories.clear();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,43 @@
package com.dianping.cat.component;

import java.util.concurrent.atomic.AtomicBoolean;

import com.dianping.cat.component.lifecycle.Disposable;
import com.dianping.cat.component.lifecycle.Initializable;
import com.dianping.cat.component.lifecycle.LogEnabled;
import com.dianping.cat.component.lifecycle.Logger;

public class DefaultComponentLifecycle implements ComponentLifecycle, Initializable, Disposable {
public class DefaultComponentLifecycle implements ComponentLifecycle {
private ComponentContext m_ctx;

private Logger m_logger;

private AtomicBoolean m_initialized = new AtomicBoolean();

public DefaultComponentLifecycle(ComponentContext ctx) {
m_ctx = ctx;
}

@Override
public void dispose() {
if (m_initialized.get()) {
m_logger = null;
m_initialized.set(false);
private Logger getLogger() {
// lazy load to avoid cyclically dependency resolution
if (m_logger == null) {
m_logger = m_ctx.lookup(Logger.class);
}
}

@Override
public void initialize(ComponentContext ctx) {
m_initialized.set(true);
m_logger = m_ctx.lookup(Logger.class);
return m_logger;
}

@Override
public void onStart(Object component) {
if (m_initialized.get()) {
if (component instanceof LogEnabled) {
((LogEnabled) component).enableLogging(m_logger);
}

if (component instanceof Initializable) {
((Initializable) component).initialize(m_ctx);
}
} else {
throw new IllegalStateException("Component lifecycle has been shutdown!");
if (component instanceof LogEnabled) {
((LogEnabled) component).enableLogging(getLogger());
}

if (component instanceof Initializable) {
((Initializable) component).initialize(m_ctx);
}
}

@Override
public void onStop(Object component) {
if (m_initialized.get()) {
if (component instanceof Disposable) {
((Disposable) component).dispose();
}
} else {
throw new IllegalStateException("Component lifecycle has been shutdown!");
if (component instanceof Disposable) {
((Disposable) component).dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ public class TraceContextHelper {

public static String createMessageId() {
initialize();

return s_factory.getNextId();
}

public static String createMessageId(String domain) {
initialize();

if (domain == null) {
return s_factory.getNextId();
} else {
return s_factory.getNextId(domain);
}
}

public static TraceContext extractFrom(HttpServletRequest req) {
Object ctx = req.getAttribute(CAT_MESSAGE_CONTEXT);

Expand Down
6 changes: 1 addition & 5 deletions cat-client/src/test/java/com/dianping/cat/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
@RunWith(Suite.class)
@SuiteClasses({

CatTest.class,

CatBootstrapTest.class,

/* .component */
Expand All @@ -60,11 +58,9 @@
/* .configuration */
ConfigureManagerTest.class,

CatEnvironmentTest.class,

/* .message */
MessageTest.class,

MetricTest.class,

EventTest.class,
Expand Down
54 changes: 49 additions & 5 deletions cat-client/src/test/java/com/dianping/cat/CatBootstrapTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
package com.dianping.cat;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class CatBootstrapTest {
import com.dianping.cat.configuration.ConfigureManager;

public class CatBootstrapTest extends ComponentTestCase {
@After
public void after() {
}

@Before
public void before() {
}

@Test
public void testInitializeByDomain() {
Cat.getBootstrap().initializeByDomain("MyDomain");

ConfigureManager manager = context().lookup(ConfigureManager.class);

Assert.assertEquals("MyDomain", manager.getDomain());
}

@Test
public void testLazyInitialize() {
Cat.newTransaction("Type", "Name");
public void testInitializeByDomainAndServers() {
Cat.getBootstrap().initializeByDomain("MyDomain", "server1", "server2");

ConfigureManager manager = context().lookup(ConfigureManager.class);

Assert.assertEquals("MyDomain", manager.getDomain());
Assert.assertEquals(2, manager.getServers().size());
Assert.assertEquals("server1", manager.getServers().get(0).getIp());
Assert.assertEquals("server2", manager.getServers().get(1).getIp());
}

@Test
public void testInitializeByServer() {
Cat.getBootstrap().initialize("localhost");
public void testInitializeByServers() {
Cat.getBootstrap().initialize("server1", "server2");

ConfigureManager manager = context().lookup(ConfigureManager.class);

Assert.assertEquals(2, manager.getServers().size());
Assert.assertEquals("server1", manager.getServers().get(0).getIp());
Assert.assertEquals("server2", manager.getServers().get(1).getIp());
}

@Test
public void testLazyInitialization() {
Assert.assertEquals(false, Cat.getBootstrap().isInitialized());

// CAT API call will trigger lazy initialization
Cat.newTransaction("Type", "Name").success().complete();

Assert.assertEquals(true, Cat.getBootstrap().isInitialized());
}
}
Loading

0 comments on commit 6d735e7

Please sign in to comment.