Skip to content

Commit ee3b467

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 658527e + 06f4639 commit ee3b467

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

timbermill-java/timbermill-api/src/main/java/com/datorama/oss/timbermill/EventLogger.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void close() {
205205
<T> Callable<T> wrapCallable(Callable<T> callable) {
206206
final String currentTaskId = getCurrentTaskId();
207207
return () -> {
208-
try(Scope scope = new Scope(currentTaskId)) {
208+
try(Scope scope = threadInstance.get().new Scope(currentTaskId)) {
209209
return scope.apply(callable);
210210
}
211211
};
@@ -214,7 +214,7 @@ <T> Callable<T> wrapCallable(Callable<T> callable) {
214214
<T, R> Function<T, R> wrapFunction(Function<T, R> function) {
215215
final String currentTaskId = getCurrentTaskId();
216216
return (in) -> {
217-
try(Scope scope = new Scope(currentTaskId)) {
217+
try(Scope scope = threadInstance.get().new Scope(currentTaskId)) {
218218
return scope.apply(function, in);
219219
}
220220
};

timbermill-java/timbermill-api/src/test/java/com/datorama/oss/timbermill/EventLoggerTest.java

+32-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.concurrent.Callable;
5+
import java.util.concurrent.CompletableFuture;
56
import java.util.concurrent.TimeUnit;
67
import java.util.function.Function;
78

@@ -68,14 +69,14 @@ public void testSimpleEventLogger() {
6869
public void testWrapCallable() throws Exception {
6970
String parentTaskId = el.startEvent(QUERY, EMPTY_LOG_PARAMS);
7071
Callable<String> wrappedCallable = el.wrapCallable(() -> {
71-
return el.getCurrentTaskId();
72+
return EventLogger.get().getCurrentTaskId();
7273
});
7374

7475
String childTaskId = el.startEvent(QUERY, EMPTY_LOG_PARAMS);
7576

7677
String callableVisibleTaskId = wrappedCallable.call();
7778

78-
assertEquals(callableVisibleTaskId, parentTaskId);
79+
assertEquals(parentTaskId, callableVisibleTaskId);
7980
}
8081

8182
@Test
@@ -92,18 +93,45 @@ public void testWrapCallableOnEmptyStack() throws Exception {
9293
assertNull(callableVisibleTaskId);
9394
}
9495

96+
@Test
97+
public void testWrapCallableThreaded() throws Exception {
98+
String parentTaskId = el.startEvent(QUERY, EMPTY_LOG_PARAMS);
99+
Callable<String> wrappedCallable = el.wrapCallable(() -> {
100+
return EventLogger.get().getCurrentTaskId();
101+
});
102+
103+
String childTaskId = el.startEvent(QUERY, EMPTY_LOG_PARAMS);
104+
105+
CompletableFuture<String> future = new CompletableFuture<String>();
106+
107+
new Thread() {
108+
@Override
109+
public void run() {
110+
try {
111+
future.complete(wrappedCallable.call());
112+
} catch (Exception ex) {
113+
future.completeExceptionally(ex);
114+
}
115+
}
116+
}.start();
117+
118+
String futureVisibleTaskId = future.get();
119+
120+
assertEquals(parentTaskId, futureVisibleTaskId);
121+
}
122+
95123
@Test
96124
public void testWrapFunction() throws Exception {
97125
String parentTaskId = el.startEvent(QUERY, EMPTY_LOG_PARAMS);
98126
Function<Integer, String> wrappedFunction = el.<Integer, String>wrapFunction(ignored -> {
99-
return el.getCurrentTaskId();
127+
return EventLogger.get().getCurrentTaskId();
100128
});
101129

102130
String childTaskId = el.startEvent(QUERY, EMPTY_LOG_PARAMS);
103131

104132
String callableVisibleTaskId = wrappedFunction.apply(42);
105133

106-
assertEquals(callableVisibleTaskId, parentTaskId);
134+
assertEquals(parentTaskId, callableVisibleTaskId);
107135
}
108136

109137
@Test

0 commit comments

Comments
 (0)