|
| 1 | +package io.argus.cli.command; |
| 2 | + |
| 3 | +import io.argus.cli.command.TraceCommand.CallNode; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +class TraceCommandTest { |
| 11 | + |
| 12 | + // ------------------------------------------------------------------------- |
| 13 | + // extractMatchingStack |
| 14 | + // ------------------------------------------------------------------------- |
| 15 | + |
| 16 | + private static final String DUMP_WITH_TARGET = |
| 17 | + "\"main\" #1 prio=5 os_prio=0 tid=0x00007f nid=0x1234 runnable\n" + |
| 18 | + " java.lang.Thread.State: RUNNABLE\n" + |
| 19 | + " at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:100)\n" + |
| 20 | + " at com.example.OrderRepository.save(OrderRepository.java:45)\n" + |
| 21 | + " at com.example.OrderService.createOrder(OrderService.java:22)\n" + |
| 22 | + " at com.example.Controller.handle(Controller.java:10)\n" + |
| 23 | + "\n" + |
| 24 | + "\"GC task thread\" #2 daemon prio=9 os_prio=0 tid=0x00007f nid=0x5678 runnable\n" + |
| 25 | + " java.lang.Thread.State: RUNNABLE\n" + |
| 26 | + " at java.lang.Object.wait(Object.java:1)\n"; |
| 27 | + |
| 28 | + @Test |
| 29 | + void extractMatchingStack_findsTargetFrame() { |
| 30 | + List<String> stack = TraceCommand.extractMatchingStack( |
| 31 | + DUMP_WITH_TARGET, "com.example.OrderService", "createOrder"); |
| 32 | + assertNotNull(stack, "Should find matching stack"); |
| 33 | + assertFalse(stack.isEmpty()); |
| 34 | + // First element should be the target method |
| 35 | + assertTrue(stack.get(0).startsWith("com.example.OrderService.createOrder"), |
| 36 | + "First frame should be the target method"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void extractMatchingStack_includesCalleeFrames() { |
| 41 | + List<String> stack = TraceCommand.extractMatchingStack( |
| 42 | + DUMP_WITH_TARGET, "com.example.OrderService", "createOrder"); |
| 43 | + assertNotNull(stack); |
| 44 | + // Should include callee frames (innermost first) |
| 45 | + assertTrue(stack.size() >= 3, "Should include callee frames"); |
| 46 | + assertTrue(stack.stream().anyMatch(f -> f.startsWith("com.example.OrderRepository.save"))); |
| 47 | + assertTrue(stack.stream().anyMatch(f -> f.startsWith("org.hibernate.internal.SessionImpl.merge"))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void extractMatchingStack_returnsNullWhenNoMatch() { |
| 52 | + List<String> stack = TraceCommand.extractMatchingStack( |
| 53 | + DUMP_WITH_TARGET, "com.example.NonExistent", "missing"); |
| 54 | + assertNull(stack, "Should return null when target not found"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void extractMatchingStack_handlesEmptyDump() { |
| 59 | + assertNull(TraceCommand.extractMatchingStack("", "com.example.Foo", "bar")); |
| 60 | + assertNull(TraceCommand.extractMatchingStack(null, "com.example.Foo", "bar")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void extractMatchingStack_matchesOnlyTargetThread() { |
| 65 | + // GC thread does not contain the target — only main thread does |
| 66 | + List<String> stack = TraceCommand.extractMatchingStack( |
| 67 | + DUMP_WITH_TARGET, "com.example.OrderService", "createOrder"); |
| 68 | + assertNotNull(stack); |
| 69 | + // Should not contain Object.wait from GC thread |
| 70 | + assertTrue(stack.stream().noneMatch(f -> f.startsWith("java.lang.Object.wait"))); |
| 71 | + } |
| 72 | + |
| 73 | + // ------------------------------------------------------------------------- |
| 74 | + // buildCallTree |
| 75 | + // ------------------------------------------------------------------------- |
| 76 | + |
| 77 | + @Test |
| 78 | + void buildCallTree_singleStack() { |
| 79 | + List<List<String>> stacks = List.of( |
| 80 | + List.of("com.example.OrderService.createOrder(OrderService.java:22)", |
| 81 | + "com.example.OrderRepository.save(OrderRepository.java:45)") |
| 82 | + ); |
| 83 | + CallNode root = TraceCommand.buildCallTree(stacks); |
| 84 | + assertEquals(1, root.children.size()); |
| 85 | + CallNode orderServiceNode = root.children.values().iterator().next(); |
| 86 | + assertEquals(1, orderServiceNode.hits); |
| 87 | + assertEquals(1, orderServiceNode.children.size()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void buildCallTree_aggregatesRepeatedStacks() { |
| 92 | + List<List<String>> stacks = List.of( |
| 93 | + List.of("com.example.OrderService.createOrder(OrderService.java:22)", |
| 94 | + "com.example.OrderRepository.save(OrderRepository.java:45)"), |
| 95 | + List.of("com.example.OrderService.createOrder(OrderService.java:22)", |
| 96 | + "com.example.OrderRepository.save(OrderRepository.java:45)"), |
| 97 | + List.of("com.example.OrderService.createOrder(OrderService.java:22)", |
| 98 | + "com.example.Validator.validate(Validator.java:10)") |
| 99 | + ); |
| 100 | + CallNode root = TraceCommand.buildCallTree(stacks); |
| 101 | + CallNode target = root.children.values().iterator().next(); |
| 102 | + assertEquals(3, target.hits, "Root target should have 3 hits"); |
| 103 | + // Should have 2 distinct children |
| 104 | + assertEquals(2, target.children.size()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void buildCallTree_emptyStacks() { |
| 109 | + CallNode root = TraceCommand.buildCallTree(List.of()); |
| 110 | + assertTrue(root.children.isEmpty()); |
| 111 | + } |
| 112 | + |
| 113 | + // ------------------------------------------------------------------------- |
| 114 | + // shortenFrame |
| 115 | + // ------------------------------------------------------------------------- |
| 116 | + |
| 117 | + @Test |
| 118 | + void shortenFrame_shortFrameUnchanged() { |
| 119 | + String frame = "com.example.Foo.bar(Foo.java:10)"; |
| 120 | + assertEquals(frame, TraceCommand.shortenFrame(frame, 100)); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void shortenFrame_stripsSourceInfo() { |
| 125 | + String frame = "com.example.OrderService.createOrder(OrderService.java:22)"; |
| 126 | + String shortened = TraceCommand.shortenFrame(frame, 40); |
| 127 | + assertFalse(shortened.contains("(OrderService.java"), "Should strip source file info"); |
| 128 | + assertTrue(shortened.length() <= 40); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void shortenFrame_truncatesWithEllipsis() { |
| 133 | + String frame = "com.example.very.long.package.name.OrderService.createOrder"; |
| 134 | + String shortened = TraceCommand.shortenFrame(frame, 20); |
| 135 | + assertTrue(shortened.length() <= 20); |
| 136 | + assertTrue(shortened.startsWith("\u2026"), "Should start with ellipsis when truncated"); |
| 137 | + } |
| 138 | +} |
0 commit comments