Skip to content

Commit

Permalink
Reset installer Thread's state. Closes #121 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed May 14, 2020
1 parent b9c6009 commit 697c98d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ public void install() {
BlockHoundRuntime.threadPredicate = threadPredicate;

onBlockingMethod = originalOnBlockingMethod;

// Re-evaluate the current thread's state after assigning user-provided predicates
BlockHoundRuntime.STATE.remove();
}

private void testInstrumentation() {
Expand Down
41 changes: 41 additions & 0 deletions example/src/test/java/com/example/DynamicCurrentThreadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example;

import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Test;
import reactor.blockhound.BlockHound;
import reactor.blockhound.BlockingOperationError;

public class DynamicCurrentThreadTest {

private static final ThreadLocal<Boolean> CAN_BLOCK = ThreadLocal.withInitial(() -> true);

static {
var testThread = Thread.currentThread();
BlockHound.install(b -> {
b.addDynamicThreadPredicate(testThread::equals);

b.nonBlockingThreadPredicate(p -> p.or(thread -> {
return !CAN_BLOCK.get();
}));
});
}

@After
public void tearDown() {
CAN_BLOCK.remove();
}

@Test
public void testChangingCurrentThreadsStatus() throws Exception {
Thread.sleep(0);

CAN_BLOCK.set(false);
Assertions.assertThatThrownBy(() -> Thread.sleep(0))
.isInstanceOf(BlockingOperationError.class);

// Reset to default
CAN_BLOCK.remove();
Thread.sleep(0);
}
}

0 comments on commit 697c98d

Please sign in to comment.