-
Notifications
You must be signed in to change notification settings - Fork 97
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
fix #178 by introducing a BuilderAllowSpec intermediate step in the builder #179
Closed
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d635dbf
Add constant STATIC_INITIALIZER for allowBlockingCalls
simonbasle 405bb1d
Add mention of constant in the allow/disallow methods javadoc
simonbasle 82d3fb2
Merge branch 'master' into 176-allowInStaticInitDiscoverability
simonbasle 32c596e
Merge branch 'master' into 176-allowInStaticInitDiscoverability
simonbasle dc9186b
Use a BuilderAllowSpec, as a phase of the builder
simonbasle d0114bc
Remove and(), spec is single call, vararg forMethods
simonbasle edbe539
Note that constructors are currently not supported in allowance
simonbasle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
example/src/test/java/com/example/BlockingAllowSpecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (c) 2020-Present Pivotal Software Inc, All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
import reactor.blockhound.BlockHound; | ||
import reactor.blockhound.BlockingOperationError; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
public class BlockingAllowSpecTest { | ||
|
||
static { | ||
BlockHound.install(b -> b | ||
.allowBlockingCallsInside(BlockingClassA.class.getName()) | ||
.forStaticInitializer() | ||
.forMethod("block") | ||
.and() | ||
.allowBlockingCallsInside(BlockingClassB.class.getName()) | ||
.forMethod("block1") | ||
.forMethod("block2") | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldInstrumentBlockingClassA() { | ||
Mono.fromCallable(BlockingClassA::new) | ||
.map(BlockingClassA::block) | ||
.subscribeOn(Schedulers.parallel()) | ||
.block(); | ||
} | ||
|
||
@Test | ||
public void shouldInstrumentBlockingClassB() { | ||
Mono.fromCallable(() -> { | ||
BlockingClassB b = new BlockingClassB(); | ||
b.block1(); | ||
b.block2(); | ||
return b; | ||
}).subscribeOn(Schedulers.parallel()).block(); | ||
} | ||
|
||
@Test | ||
public void usingAllowSpecWithoutCallingMethodIsIgnored() { | ||
//getting the BuilderAllowSpec and not acting on it DOESN'T equate to allowing any method: it does nothing | ||
BlockHound.install(b -> b.allowBlockingCallsInside(BlockingClassC.class.getName())); | ||
|
||
Mono<String> mono = Mono.fromCallable(BlockingClassC::new) | ||
.publishOn(Schedulers.parallel()) | ||
.map(BlockingClassC::block1); | ||
|
||
Assertions.assertThatExceptionOfType(RuntimeException.class) | ||
.isThrownBy(mono::block) | ||
.havingCause() | ||
.isInstanceOf(BlockingOperationError.class) | ||
.withMessage("Blocking call! java.lang.Thread.yield") | ||
.withStackTraceContaining("at com.example.BlockingAllowSpecTest$BlockingClassC.block1"); | ||
} | ||
|
||
static class BlockingClassA { | ||
static { | ||
try { | ||
Thread.sleep(0); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
String block() { | ||
Thread.yield(); | ||
return "hello A"; | ||
} | ||
} | ||
|
||
static class BlockingClassB { | ||
|
||
void block1() { | ||
try { | ||
Thread.sleep(0); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
void block2() { | ||
Thread.yield(); | ||
} | ||
} | ||
|
||
static class BlockingClassC { | ||
|
||
String block1() { | ||
Thread.yield(); | ||
return "hello C"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see anything for "Disallow". Doesn't that need the same? The same builder could support both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could, but I wasn't able to test the disallow feature. Either I misunderstand it or there is something wrong with the feature (which isn't tested)... so for now I prefer to focus on the allow-static-initializer case which is demonstrably supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonbasle re "disallow": see https://github.com/reactor/BlockHound/blob/master/docs/customization.md#dis-allowing-blocking-calls-inside-methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that and didn't find it crystal clear.
The following tests fail:
What am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to be missing how BlockHound works, see https://github.com/reactor/BlockHound/blob/master/docs/how_it_works.md and especially "Blocking call decision".
tl;dr:
disallowBlockingCallsInside
disallows blocking calls inside provided method (as per the API method's name). It does not, however, mark the method as blocking, so yourFlux.just
orNonBlockingClass.example
makes no sense.