forked from reactor/reactor-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API-preserving hide() method to ConnectableFlux (reactor#1577)
- Loading branch information
1 parent
b8aaf9f
commit 3a57c74
Showing
6 changed files
with
135 additions
and
5 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
64 changes: 64 additions & 0 deletions
64
reactor-core/src/main/java/reactor/core/publisher/ConnectableFluxHide.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,64 @@ | ||
/* | ||
* Copyright (c) 2011-2018 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 reactor.core.publisher; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import reactor.core.CoreSubscriber; | ||
import reactor.core.Disposable; | ||
import reactor.core.Scannable; | ||
import reactor.util.annotation.Nullable; | ||
|
||
/** | ||
* Hide a {@link ConnectableFlux} from fusion optimizations while keeping the {@link ConnectableFlux} | ||
* specific API visible. | ||
* | ||
* @author Simon Baslé | ||
*/ | ||
final class ConnectableFluxHide<T> extends ConnectableFlux<T> implements Scannable { | ||
|
||
final ConnectableFlux<T> source; | ||
|
||
ConnectableFluxHide(ConnectableFlux<T> source) { | ||
super(); | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public int getPrefetch() { | ||
return source.getPrefetch(); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Object scanUnsafe(Scannable.Attr key) { | ||
if (key == Scannable.Attr.PARENT) return source; | ||
if (key == Scannable.Attr.PREFETCH) return getPrefetch(); | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public void subscribe(CoreSubscriber<? super T> actual) { | ||
source.subscribe(actual); | ||
} | ||
|
||
@Override | ||
public void connect(Consumer<? super Disposable> cancelSupport) { | ||
source.connect(cancelSupport); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
reactor-core/src/test/java/reactor/core/publisher/ConnectableFluxHideTest.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,51 @@ | ||
/* | ||
* Copyright (c) 2011-2019 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 reactor.core.publisher; | ||
|
||
import org.junit.Test; | ||
|
||
import reactor.core.Fuseable; | ||
import reactor.core.Scannable; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ConnectableFluxHideTest { | ||
|
||
@Test | ||
public void hideApiPreservesConnectableFlux() { | ||
ConnectableFlux<Integer> connectableFlux = Flux.range(1, 4).replay(); | ||
|
||
assertThat(connectableFlux).as("original is Fuseable").isInstanceOf(Fuseable.class); | ||
|
||
//this validates the hide() maintains the API | ||
connectableFlux = connectableFlux.hide(); | ||
connectableFlux.connect(); | ||
|
||
assertThat(connectableFlux).as("hidden is not fuseable").isNotInstanceOf(Fuseable.class); | ||
} | ||
|
||
@Test | ||
public void scanOperator() throws Exception { | ||
ConnectableFlux<Integer> source = Flux.range(1, 4).publish(); | ||
ConnectableFluxHide<Integer> test = new ConnectableFluxHide<>(source); | ||
|
||
assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(source); | ||
assertThat(test.scan(Scannable.Attr.PREFETCH)) | ||
.isEqualTo(256) | ||
.isEqualTo(source.getPrefetch()); | ||
} | ||
} |
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