Skip to content

Commit

Permalink
fix CatFilterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
qmwu2000 committed Jun 24, 2022
1 parent 526e3fb commit be00efd
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 186 deletions.
6 changes: 6 additions & 0 deletions cat-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.2.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.dianping.cat.Cat;
import com.dianping.cat.CatClientConstants;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.MessageTree;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.context.TraceContextHelper;
import com.dianping.cat.message.internal.DefaultTransaction;
Expand Down Expand Up @@ -82,6 +83,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
}

m_handlers.add(CatHandler.ENVIRONMENT);
m_handlers.add(CatHandler.ID_SETUP);
m_handlers.add(CatHandler.LOG_SPAN);
m_handlers.add(CatHandler.LOG_CLIENT_PAYLOAD);
}
Expand All @@ -104,6 +106,26 @@ public void handle(Context ctx) throws IOException, ServletException {
}
},

ID_SETUP {
@Override
public void handle(Context ctx) throws IOException, ServletException {
HttpServletRequest req = ctx.getRequest();
String id = req.getHeader("x-cat-id");
String parentId = req.getHeader("x-cat-parent-id");
String rootId = req.getHeader("x-cat-root-id");

if (id != null) {
MessageTree tree = TraceContextHelper.threadLocal().getMessageTree();

tree.setMessageId(id);
tree.setParentMessageId(parentId);
tree.setRootMessageId(rootId);
}

ctx.handle();
}
},

LOG_CLIENT_PAYLOAD {
@Override
public void handle(Context ctx) throws IOException, ServletException {
Expand Down Expand Up @@ -163,7 +185,7 @@ private void customizeStatus(Transaction t, HttpServletRequest req) {
if (catStatus != null) {
t.setStatus(catStatus.toString());
} else {
t.success();
t.setStatus(Message.SUCCESS);
}
}

Expand Down Expand Up @@ -232,19 +254,13 @@ public void handle(Context ctx) throws IOException, ServletException {
};
}

protected static class Context {
private static class Context {
private FilterChain m_chain;

private List<Handler> m_handlers;

private int m_index;

private String m_rootId;

private String m_parentId;

private String m_id;

private HttpServletRequest m_request;

private HttpServletResponse m_response;
Expand All @@ -261,26 +277,10 @@ public Context(HttpServletRequest request, HttpServletResponse response, FilterC
m_handlers = handlers;
}

public String getId() {
return m_id;
}

public String getParentId() {
return m_parentId;
}

public HttpServletRequest getRequest() {
return m_request;
}

public HttpServletResponse getResponse() {
return m_response;
}

public String getRootId() {
return m_rootId;
}

public String getType() {
return m_type;
}
Expand All @@ -299,18 +299,6 @@ public boolean isTop() {
return m_top;
}

public void setId(String id) {
m_id = id;
}

public void setParentId(String parentId) {
m_parentId = parentId;
}

public void setRootId(String rootId) {
m_rootId = rootId;
}

public void setTop(boolean top) {
m_top = top;
}
Expand All @@ -320,7 +308,7 @@ public void setType(String type) {
}
}

protected static interface Handler {
private static interface Handler {
public void handle(Context ctx) throws IOException, ServletException;
}
}
}
166 changes: 123 additions & 43 deletions cat-client/src/test/java/com/dianping/cat/message/MessageAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static EventAssert event() {
return new EventAssert((Event) message);
}

public static AssertSupport<Event, EventAssert> event(String type) {
public static EventAssert eventBy(String type) {
List<String> types = new ArrayList<String>();

for (MessageTree tree : new ArrayList<MessageTree>(s_trees)) {
Expand All @@ -50,6 +50,26 @@ public static HeaderAssert header() {
return new HeaderAssert(tree);
}

public static HeaderAssert headerByTransaction(String type) {
List<String> types = new ArrayList<String>();

for (MessageTree tree : new ArrayList<MessageTree>(s_trees)) {
Message message = tree.getMessage();

if (message instanceof Transaction) {
if (message.getType().equals(type)) {
return new HeaderAssert(tree);
} else if (!types.contains(message.getType())) {
types.add(message.getType());
}
}
}

Assert.fail(String.format("No message tree(%s) found, but was %s!", type, types.toString()));

return null; // this will NEVER be reached
}

public static void newTree(MessageTree tree) {
s_trees.push(tree);
}
Expand All @@ -71,70 +91,60 @@ public static TransactionAssert transaction() {
return new TransactionAssert((Transaction) message);
}

public static MessageTreeAssert tree(String messageId) {
List<String> messageIds = new ArrayList<String>();
public static TransactionAssert transactionBy(String type) {
List<String> types = new ArrayList<String>();

for (MessageTree tree : new ArrayList<MessageTree>(s_trees)) {
messageIds.add(tree.getMessageId());
Message message = tree.getMessage();

if (tree.getMessageId().equals(messageId)) {
return new MessageTreeAssert(tree);
if (message instanceof Transaction) {
if (message.getType().equals(type)) {
return new TransactionAssert((Transaction) message);
} else if (!types.contains(message.getType())) {
types.add(message.getType());
}
}
}

Assert.fail(String.format("No message tree(%s) found, but was %s!", messageId, messageIds.toString()));
Assert.fail(String.format("No transaction(%s) found, but was %s!", type, types.toString()));

return null; // this will NEVER be reached
}

public static class EventAssert extends AssertSupport<Event, EventAssert> {
private Event m_event;

public EventAssert(Event event) {
super(event, "Event");
public static MessageTreeAssert tree(String messageId) {
List<String> messageIds = new ArrayList<String>();

m_event = event;
}
for (MessageTree tree : new ArrayList<MessageTree>(s_trees)) {
messageIds.add(tree.getMessageId());

public Event event() {
return m_event;
if (tree.getMessageId().equals(messageId)) {
return new MessageTreeAssert(tree);
}
}
}

public static class HeaderAssert {
private MessageTree m_tree;
Assert.fail(String.format("No message tree(%s) found, but was %s!", messageId, messageIds.toString()));

public HeaderAssert(MessageTree tree) {
m_tree = tree;
}
return null; // this will NEVER be reached
}

public HeaderAssert domain(String domain) {
Assert.assertEquals("Domain mismatched!", domain, m_tree.getDomain());
return this;
}
public static MessageTreeAssert treeByTransaction(String type) {
List<String> types = new ArrayList<String>();

public HeaderAssert messageId(String messageId) {
Assert.assertEquals("Message id mismatched!", messageId, m_tree.getMessageId());
return this;
}
for (MessageTree tree : new ArrayList<MessageTree>(s_trees)) {
Message message = tree.getMessage();

public HeaderAssert messageIdStartsWith(String messageIdPrefix) {
if (!m_tree.getMessageId().startsWith(messageIdPrefix)) {
Assert.fail(String.format("Message id %s does not start with %s!", m_tree.getMessageId(), messageIdPrefix));
if (message instanceof Transaction) {
if (message.getType().equals(type)) {
return new MessageTreeAssert(tree);
} else if (!types.contains(message.getType())) {
types.add(message.getType());
}
}

return this;
}

public HeaderAssert parentMessageId(String parentMessageId) {
Assert.assertEquals("Parent message id mismatched!", parentMessageId, m_tree.getParentMessageId());
return this;
}
Assert.fail(String.format("No message tree(%s) found, but was %s!", type, types.toString()));

public HeaderAssert rootMessageId(String rootMessageId) {
Assert.assertEquals("Root message id mismatched!", rootMessageId, m_tree.getRootMessageId());
return this;
}
return null; // this will NEVER be reached
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -197,6 +207,76 @@ public T type(String type) {
Assert.assertEquals(format("%s type mismatched!", m_class), type, m_message.getType());
return (T) this;
}

public AssertSupport<R, T> withData() {
Assert.assertNotNull("Message id is NULL!", m_message.getData());
return this;
}
}

public static class EventAssert extends AssertSupport<Event, EventAssert> {
private Event m_event;

public EventAssert(Event event) {
super(event, "Event");

m_event = event;
}

public Event event() {
return m_event;
}
}

public static class HeaderAssert {
private MessageTree m_tree;

public HeaderAssert(MessageTree tree) {
m_tree = tree;
}

public HeaderAssert domain(String domain) {
Assert.assertEquals("Domain mismatched!", domain, m_tree.getDomain());
return this;
}

public HeaderAssert messageId(String messageId) {
Assert.assertEquals("Message id mismatched!", messageId, m_tree.getMessageId());
return this;
}

public HeaderAssert messageIdStartsWith(String messageIdPrefix) {
if (!m_tree.getMessageId().startsWith(messageIdPrefix)) {
Assert.fail(String.format("Message id %s does not start with %s!", m_tree.getMessageId(), messageIdPrefix));
}

return this;
}

public HeaderAssert parentMessageId(String parentMessageId) {
Assert.assertEquals("Parent message id mismatched!", parentMessageId, m_tree.getParentMessageId());
return this;
}

public HeaderAssert rootMessageId(String rootMessageId) {
Assert.assertEquals("Root message id mismatched!", rootMessageId, m_tree.getRootMessageId());
return this;
}

public HeaderAssert withMessageId() {
Assert.assertNotNull("Message id is NULL!", m_tree.getMessageId());
return this;
}

public HeaderAssert withParentMessageId() {
Assert.assertNotNull("Message id is NULL!", m_tree.getParentMessageId());
return this;
}

public HeaderAssert withRootMessageId() {
Assert.assertNotNull("Message id is NULL!", m_tree.getRootMessageId());
return this;
}
}

public static class MessageTreeAssert {
Expand Down
Loading

0 comments on commit be00efd

Please sign in to comment.