MessageProducerTest.java
4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.dianping.cat.message.internal;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.io.File;
import java.io.IOException;
import java.util.Queue;
import java.util.Stack;
import junit.framework.Assert;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.unidal.helper.Files;
import org.unidal.helper.Reflects;
import com.dianping.cat.Cat;
import com.dianping.cat.configuration.client.entity.ClientConfig;
import com.dianping.cat.configuration.client.entity.Domain;
import com.dianping.cat.message.CatTestCase;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.MessageProducer;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.io.TransportManager;
import com.dianping.cat.message.spi.MessageCodec;
import com.dianping.cat.message.spi.MessageTree;
@RunWith(JUnit4.class)
public class MessageProducerTest extends CatTestCase {
private Queue<MessageTree> m_queue;
@BeforeClass
public static void beforeClass() throws IOException {
ClientConfig clientConfig = new ClientConfig();
clientConfig.setMode("client");
clientConfig.addDomain(new Domain("Test").setEnabled(true));
File configFile = new File("target/client.xml").getCanonicalFile();
configFile.getParentFile().mkdirs();
Files.forIO().writeTo(configFile, clientConfig.toString());
Cat.destroy();
Cat.initialize(configFile);
}
@Before
public void before() throws Exception {
TransportManager manager = Cat.lookup(TransportManager.class);
Initializable queue = Reflects.forField().getDeclaredFieldValue(manager.getSender().getClass(), "m_queue",
manager.getSender());
queue.initialize();
m_queue = Reflects.forField().getDeclaredFieldValue(queue.getClass(), "m_queue", queue);
}
@Test
public void testNormal() throws Exception {
MessageProducer producer = Cat.getProducer();
Transaction t = producer.newTransaction("URL", "MyPage");
try {
// do your business here
t.addData("k1", "v1");
t.addData("k2", "v2");
t.addData("k3", "v3");
Thread.sleep(20);
producer.logEvent("URL", "Payload", Message.SUCCESS, "host=my-host&ip=127.0.0.1&agent=...");
t.setStatus(Message.SUCCESS);
} catch (Exception e) {
t.setStatus(e);
} finally {
t.complete();
}
// please stop CAT server when you run this test case
Assert.assertEquals("One message should be in the queue.", 1, m_queue.size());
MessageTree tree = m_queue.poll();
Message m = tree.getMessage();
Assert.assertTrue(Transaction.class.isAssignableFrom(m.getClass()));
Transaction trans = (Transaction) m;
Assert.assertEquals("URL", trans.getType());
Assert.assertEquals("MyPage", trans.getName());
Assert.assertEquals("0", trans.getStatus());
Assert.assertTrue(trans.getDurationInMillis() > 0);
Assert.assertEquals("k1=v1&k2=v2&k3=v3", trans.getData().toString());
Assert.assertEquals(1, trans.getChildren().size());
Message c = trans.getChildren().get(0);
Assert.assertEquals("URL", c.getType());
Assert.assertEquals("Payload", c.getName());
Assert.assertEquals("0", c.getStatus());
Assert.assertEquals("host=my-host&ip=127.0.0.1&agent=...", c.getData().toString());
}
@Test
public void testNested() throws Exception {
Stack<Transaction> stack = new Stack<Transaction>();
for (int i = 0; i < 10; i++) {
Transaction t = Cat.getProducer().newTransaction("Test", "TestName");
t.addData("k1", "v1");
t.addData("k2", "v2");
t.addData("k3", "v3");
stack.push(t);
}
while (!stack.isEmpty()) {
Transaction t = stack.pop();
t.setStatus(Message.SUCCESS);
t.complete();
}
// please stop CAT server when you run this test case
Assert.assertEquals("One message should be in the queue.", 1, m_queue.size());
MessageTree tree = m_queue.poll();
MessageCodec codec = Cat.lookup(MessageCodec.class, "plain-text");
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(4 * 1024);
codec.encode(tree, buf);
buf.readInt();
MessageTree tree2 = codec.decode(buf);
Assert.assertEquals(tree.toString(), tree2.toString());
}
}