Skip to content

Commit 6d735e7

Browse files
committed
add test cases
1 parent be00efd commit 6d735e7

File tree

14 files changed

+322
-559
lines changed

14 files changed

+322
-559
lines changed

cat-client/src/main/java/com/dianping/cat/Cat.java

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.dianping.cat.message.Event;
2424
import com.dianping.cat.message.Heartbeat;
25+
import com.dianping.cat.message.MessageTree;
2526
import com.dianping.cat.message.Metric;
2627
import com.dianping.cat.message.Trace;
2728
import com.dianping.cat.message.Transaction;
@@ -205,9 +206,9 @@ public static void logMetricForSum(String name, double sum, int quantity) {
205206
* @param domain
206207
* domain is default, if use default config, the performance of server storage is bad。
207208
*/
208-
// public static void logRemoteCallClient(Context ctx) {
209-
// logRemoteCallClient(ctx, "default");
210-
// }
209+
public static void logRemoteCallClient(Context ctx) {
210+
logRemoteCallClient(ctx, null);
211+
}
211212

212213
/**
213214
* logRemoteCallClient is used in rpc client
@@ -217,59 +218,54 @@ public static void logMetricForSum(String name, double sum, int quantity) {
217218
* @param domain
218219
* domain is project name of rpc server name
219220
*/
220-
// public static void logRemoteCallClient(Context ctx, String domain) {
221-
// try {
222-
// MessageTree tree = getManager().getThreadLocalMessageTree();
223-
// String messageId = tree.getMessageId();
224-
//
225-
// if (messageId == null) {
226-
// messageId = Cat.createMessageId();
227-
// tree.setMessageId(messageId);
228-
// }
229-
//
230-
// String childId = CAT.m_factory.getNextId(domain);
231-
// Cat.logEvent(CatClientConstants.TYPE_REMOTE_CALL, "", Event.SUCCESS, childId);
232-
//
233-
// String root = tree.getRootMessageId();
234-
//
235-
// if (root == null) {
236-
// root = messageId;
237-
// }
238-
//
239-
// ctx.addProperty(Context.ROOT, root);
240-
// ctx.addProperty(Context.PARENT, messageId);
241-
// ctx.addProperty(Context.CHILD, childId);
242-
// } catch (Exception e) {
243-
// errorHandler(e);
244-
// }
245-
// }
221+
public static void logRemoteCallClient(Context ctx, String domain) {
222+
try {
223+
MessageTree tree = TraceContextHelper.threadLocal().getMessageTree();
224+
String messageId = tree.getMessageId();
225+
String childId = TraceContextHelper.createMessageId(domain);
226+
227+
Cat.logEvent(CatClientConstants.TYPE_REMOTE_CALL, "", Event.SUCCESS, childId);
228+
229+
String root = tree.getRootMessageId();
230+
231+
if (root == null) {
232+
root = messageId;
233+
}
234+
235+
ctx.addProperty(Context.ROOT, root);
236+
ctx.addProperty(Context.PARENT, messageId);
237+
ctx.addProperty(Context.CHILD, childId);
238+
} catch (Exception e) {
239+
errorHandler(e);
240+
}
241+
}
246242

247243
/**
248244
* used in rpc server,use clild id as server message tree id.
249245
*
250246
* @param ctx
251247
* ctx is rpc context ,such as duboo context , please use rpc context implement Context
252248
*/
253-
// public static void logRemoteCallServer(Context ctx) {
254-
// try {
255-
// MessageTree tree = getManager().getThreadLocalMessageTree();
256-
// String childId = ctx.getProperty(Context.CHILD);
257-
// String rootId = ctx.getProperty(Context.ROOT);
258-
// String parentId = ctx.getProperty(Context.PARENT);
259-
//
260-
// if (parentId != null) {
261-
// tree.setParentMessageId(parentId);
262-
// }
263-
// if (rootId != null) {
264-
// tree.setRootMessageId(rootId);
265-
// }
266-
// if (childId != null) {
267-
// tree.setMessageId(childId);
268-
// }
269-
// } catch (Exception e) {
270-
// errorHandler(e);
271-
// }
272-
// }
249+
public static void logRemoteCallServer(Context ctx) {
250+
try {
251+
MessageTree tree = TraceContextHelper.threadLocal().getMessageTree();
252+
String childId = ctx.getProperty(Context.CHILD);
253+
String rootId = ctx.getProperty(Context.ROOT);
254+
String parentId = ctx.getProperty(Context.PARENT);
255+
256+
if (parentId != null) {
257+
tree.setParentMessageId(parentId);
258+
}
259+
if (rootId != null) {
260+
tree.setRootMessageId(rootId);
261+
}
262+
if (childId != null) {
263+
tree.setMessageId(childId);
264+
}
265+
} catch (Exception e) {
266+
errorHandler(e);
267+
}
268+
}
273269

274270
public static void logTrace(String type, String name) {
275271
try {
@@ -328,4 +324,16 @@ public static Transaction newTransaction(String type, String name) {
328324
return NullMessage.TRANSACTION;
329325
}
330326
}
327+
328+
public static interface Context {
329+
public final String ROOT = "_catRootMessageId";
330+
331+
public final String PARENT = "_catParentMessageId";
332+
333+
public final String CHILD = "_catChildMessageId";
334+
335+
public void addProperty(String key, String value);
336+
337+
public String getProperty(String key);
338+
}
331339
}

cat-client/src/main/java/com/dianping/cat/component/DefaultComponentContext.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public DefaultComponentContext() {
2828
registerFactory(new SystemComponentFactory());
2929

3030
m_lifecycle = new DefaultComponentLifecycle(this);
31-
m_lifecycle.initialize(this);
3231
}
3332

3433
@Override
@@ -41,8 +40,6 @@ public void dispose() {
4140
m_lifecycle.onStop(factory);
4241
}
4342

44-
m_lifecycle.dispose();
45-
4643
m_singletons.clear();
4744
m_factories.clear();
4845
}
Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,43 @@
11
package com.dianping.cat.component;
22

3-
import java.util.concurrent.atomic.AtomicBoolean;
4-
53
import com.dianping.cat.component.lifecycle.Disposable;
64
import com.dianping.cat.component.lifecycle.Initializable;
75
import com.dianping.cat.component.lifecycle.LogEnabled;
86
import com.dianping.cat.component.lifecycle.Logger;
97

10-
public class DefaultComponentLifecycle implements ComponentLifecycle, Initializable, Disposable {
8+
public class DefaultComponentLifecycle implements ComponentLifecycle {
119
private ComponentContext m_ctx;
1210

1311
private Logger m_logger;
1412

15-
private AtomicBoolean m_initialized = new AtomicBoolean();
16-
1713
public DefaultComponentLifecycle(ComponentContext ctx) {
1814
m_ctx = ctx;
1915
}
2016

21-
@Override
22-
public void dispose() {
23-
if (m_initialized.get()) {
24-
m_logger = null;
25-
m_initialized.set(false);
17+
private Logger getLogger() {
18+
// lazy load to avoid cyclically dependency resolution
19+
if (m_logger == null) {
20+
m_logger = m_ctx.lookup(Logger.class);
2621
}
27-
}
2822

29-
@Override
30-
public void initialize(ComponentContext ctx) {
31-
m_initialized.set(true);
32-
m_logger = m_ctx.lookup(Logger.class);
23+
return m_logger;
3324
}
3425

3526
@Override
3627
public void onStart(Object component) {
37-
if (m_initialized.get()) {
38-
if (component instanceof LogEnabled) {
39-
((LogEnabled) component).enableLogging(m_logger);
40-
}
41-
42-
if (component instanceof Initializable) {
43-
((Initializable) component).initialize(m_ctx);
44-
}
45-
} else {
46-
throw new IllegalStateException("Component lifecycle has been shutdown!");
28+
if (component instanceof LogEnabled) {
29+
((LogEnabled) component).enableLogging(getLogger());
30+
}
31+
32+
if (component instanceof Initializable) {
33+
((Initializable) component).initialize(m_ctx);
4734
}
4835
}
4936

5037
@Override
5138
public void onStop(Object component) {
52-
if (m_initialized.get()) {
53-
if (component instanceof Disposable) {
54-
((Disposable) component).dispose();
55-
}
56-
} else {
57-
throw new IllegalStateException("Component lifecycle has been shutdown!");
39+
if (component instanceof Disposable) {
40+
((Disposable) component).dispose();
5841
}
5942
}
6043
}

cat-client/src/main/java/com/dianping/cat/message/context/TraceContextHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ public class TraceContextHelper {
2222

2323
public static String createMessageId() {
2424
initialize();
25-
25+
2626
return s_factory.getNextId();
2727
}
2828

29+
public static String createMessageId(String domain) {
30+
initialize();
31+
32+
if (domain == null) {
33+
return s_factory.getNextId();
34+
} else {
35+
return s_factory.getNextId(domain);
36+
}
37+
}
38+
2939
public static TraceContext extractFrom(HttpServletRequest req) {
3040
Object ctx = req.getAttribute(CAT_MESSAGE_CONTEXT);
3141

cat-client/src/test/java/com/dianping/cat/AllTests.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
@RunWith(Suite.class)
4343
@SuiteClasses({
4444

45-
CatTest.class,
46-
4745
CatBootstrapTest.class,
4846

4947
/* .component */
@@ -60,11 +58,9 @@
6058
/* .configuration */
6159
ConfigureManagerTest.class,
6260

63-
CatEnvironmentTest.class,
64-
6561
/* .message */
6662
MessageTest.class,
67-
63+
6864
MetricTest.class,
6965

7066
EventTest.class,
Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
11
package com.dianping.cat;
22

3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
36
import org.junit.Test;
47

5-
public class CatBootstrapTest {
8+
import com.dianping.cat.configuration.ConfigureManager;
9+
10+
public class CatBootstrapTest extends ComponentTestCase {
11+
@After
12+
public void after() {
13+
}
14+
15+
@Before
16+
public void before() {
17+
}
18+
19+
@Test
20+
public void testInitializeByDomain() {
21+
Cat.getBootstrap().initializeByDomain("MyDomain");
22+
23+
ConfigureManager manager = context().lookup(ConfigureManager.class);
24+
25+
Assert.assertEquals("MyDomain", manager.getDomain());
26+
}
27+
628
@Test
7-
public void testLazyInitialize() {
8-
Cat.newTransaction("Type", "Name");
29+
public void testInitializeByDomainAndServers() {
30+
Cat.getBootstrap().initializeByDomain("MyDomain", "server1", "server2");
31+
32+
ConfigureManager manager = context().lookup(ConfigureManager.class);
933

34+
Assert.assertEquals("MyDomain", manager.getDomain());
35+
Assert.assertEquals(2, manager.getServers().size());
36+
Assert.assertEquals("server1", manager.getServers().get(0).getIp());
37+
Assert.assertEquals("server2", manager.getServers().get(1).getIp());
1038
}
1139

1240
@Test
13-
public void testInitializeByServer() {
14-
Cat.getBootstrap().initialize("localhost");
41+
public void testInitializeByServers() {
42+
Cat.getBootstrap().initialize("server1", "server2");
43+
44+
ConfigureManager manager = context().lookup(ConfigureManager.class);
45+
46+
Assert.assertEquals(2, manager.getServers().size());
47+
Assert.assertEquals("server1", manager.getServers().get(0).getIp());
48+
Assert.assertEquals("server2", manager.getServers().get(1).getIp());
49+
}
50+
51+
@Test
52+
public void testLazyInitialization() {
53+
Assert.assertEquals(false, Cat.getBootstrap().isInitialized());
54+
55+
// CAT API call will trigger lazy initialization
56+
Cat.newTransaction("Type", "Name").success().complete();
57+
58+
Assert.assertEquals(true, Cat.getBootstrap().isInitialized());
1559
}
1660
}

0 commit comments

Comments
 (0)