Skip to content

Commit

Permalink
Backport: contextView() implem of [Flux|Mono|Synchronous]Sink (#3026)
Browse files Browse the repository at this point in the history
This commit backports #3021, a default implementation of the
`contextView()` method of the FluxSink, MonoSink and SynchronousSink
"old sinks" classes.

The backport is necessary because the new method was not added only in
3.5.0-M1 as initially stated, but in 3.4.17 as well.

Backport of commit 6eecf85.
See #2971, #2974 and #3021.
  • Loading branch information
simonbasle authored Apr 20, 2022
1 parent a49fd63 commit 1be0d59
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
7 changes: 6 additions & 1 deletion reactor-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ task japicmp(type: JapicmpTask) {
// TODO after a .0 release, bump the gradle.properties baseline
// TODO after a .0 release, remove the reactor-core exclusions below if any
classExcludes = [ ]
methodExcludes = [ 'reactor.core.publisher.Sinks$EmitFailureHandler#busyLooping(java.time.Duration)' ]
methodExcludes = [
'reactor.core.publisher.Sinks$EmitFailureHandler#busyLooping(java.time.Duration)',
'reactor.core.publisher.FluxSink#contextView()',
'reactor.core.publisher.MonoSink#contextView()',
'reactor.core.publisher.SynchronousSink#contextView()'
]
}

gradle.taskGraph.afterTask { task, state ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public interface FluxSink<T> {
*
* @return the current subscriber {@link ContextView}.
*/
ContextView contextView();
default ContextView contextView() {
return currentContext();
}


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public interface MonoSink<T> {
*
* @return the current subscriber {@link ContextView}.
*/
ContextView contextView();
default ContextView contextView() {
return this.currentContext();
}

/**
* Attaches a {@link LongConsumer} to this {@link MonoSink} that will be notified of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public interface SynchronousSink<T> {
*
* @return the current subscriber {@link ContextView}.
*/
ContextView contextView();
default ContextView contextView() {
return currentContext();
}

/**
* @param e the exception to signal, not null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2019-2022 VMware Inc. or its affiliates, 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.
Expand Down Expand Up @@ -27,10 +27,15 @@

import org.junit.jupiter.api.Test;

import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.MonoSink;
import reactor.core.publisher.SynchronousSink;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static org.assertj.core.api.Assertions.assertThat;

public class CurrentContextArchTest {
class ContextBestPracticesArchTest {

static JavaClasses CORE_SUBSCRIBER_CLASSES = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
Expand All @@ -50,7 +55,7 @@ void smokeTestWhereClassesLoaded() {
}

@Test
public void corePublishersShouldNotUseDefaultCurrentContext() {
void corePublishersShouldNotUseDefaultCurrentContext() {
classes()
.that().implement(CoreSubscriber.class)
.and().doNotHaveModifier(JavaModifier.ABSTRACT)
Expand Down Expand Up @@ -78,7 +83,7 @@ public void check(JavaClass item, ConditionEvents events) {
@Test
// This is ok as this class tests the deprecated FluxProcessor. Will be removed with it in 3.5.
@SuppressWarnings("deprecation")
public void fluxProcessorsShouldNotUseDefaultCurrentContext() {
void fluxProcessorsShouldNotUseDefaultCurrentContext() {
classes()
.that().areAssignableTo(reactor.core.publisher.FluxProcessor.class)
.and().doNotHaveModifier(JavaModifier.ABSTRACT)
Expand All @@ -105,4 +110,35 @@ public void check(JavaClass item, ConditionEvents events) {
})
.check(FLUXPROCESSOR_CLASSES);
}

@Test
void oldSinksShouldNotUseDefaultCurrentContext() {
classes()
.that().implement(SynchronousSink.class)
.or().implement(FluxSink.class)
.or().implement(MonoSink.class)
.and().doNotHaveModifier(JavaModifier.ABSTRACT)
.should(new ArchCondition<JavaClass>("not use the default contextView()") {
@Override
public void check(JavaClass item, ConditionEvents events) {
boolean overridesMethod = item
.getAllMethods()
.stream()
.filter(it -> "contextView".equals(it.getName()))
.filter(it -> it.getRawParameterTypes().isEmpty())
.anyMatch(it -> !it.getOwner().isEquivalentTo(SynchronousSink.class)
&& !it.getOwner().isEquivalentTo(FluxSink.class)
&& !it.getOwner().isEquivalentTo(MonoSink.class)
);

if (!overridesMethod) {
events.add(SimpleConditionEvent.violated(
item,
item.getFullName() + item.getSourceCodeLocation() + ": contextView() is not overridden"
));
}
}
})
.check(CORE_SUBSCRIBER_CLASSES);
}
}

0 comments on commit 1be0d59

Please sign in to comment.