Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset installer Thread's state #124

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}