Skip to content

Commit e8518ca

Browse files
committed
fix part 3
1 parent 0413a6a commit e8518ca

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.taobao.arthas.core.shell.command.internal;
2+
3+
import com.taobao.arthas.core.shell.term.Term;
4+
import io.netty.util.internal.InternalThreadLocalMap;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.mockito.Mockito;
8+
9+
public class TermHandlerTest {
10+
11+
@Test
12+
public void testShouldRemoveNettyInternalThreadLocalMapAfterWrite() {
13+
InternalThreadLocalMap.remove();
14+
15+
Term term = Mockito.mock(Term.class);
16+
Mockito.when(term.write(Mockito.anyString())).thenAnswer(invocation -> {
17+
InternalThreadLocalMap.get();
18+
return term;
19+
});
20+
21+
new TermHandler(term).apply("hello\n");
22+
23+
Assert.assertNull(InternalThreadLocalMap.getIfSet());
24+
}
25+
26+
@Test
27+
public void testShouldRemoveNettyInternalThreadLocalMapWhenWriteThrows() {
28+
InternalThreadLocalMap.remove();
29+
30+
Term term = Mockito.mock(Term.class);
31+
Mockito.when(term.write(Mockito.anyString())).thenAnswer(invocation -> {
32+
InternalThreadLocalMap.get();
33+
throw new RuntimeException("boom");
34+
});
35+
36+
try {
37+
new TermHandler(term).apply("hello\n");
38+
Assert.fail("should throw");
39+
} catch (RuntimeException e) {
40+
Assert.assertEquals("boom", e.getMessage());
41+
}
42+
43+
Assert.assertNull(InternalThreadLocalMap.getIfSet());
44+
}
45+
}
46+

0 commit comments

Comments
 (0)