-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a BuilderAllowSpec, as a phase of the builder
- Loading branch information
1 parent
32c596e
commit dc9186b
Showing
3 changed files
with
187 additions
and
16 deletions.
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
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