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

Add default contextView() implem to [Flux|Mono|Synchronous]Sink #3021

Merged
merged 3 commits into from
Apr 20, 2022
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
5 changes: 4 additions & 1 deletion reactor-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ task japicmp(type: JapicmpTask) {
'reactor.core.scheduler.Schedulers#newElastic(java.lang.String, int)',
'reactor.core.scheduler.Schedulers#newElastic(java.lang.String, int, boolean)',
'reactor.core.scheduler.Schedulers#newElastic(int, java.util.concurrent.ThreadFactory)',
'reactor.core.scheduler.Schedulers$Factory#newElastic(int, java.util.concurrent.ThreadFactory)'
'reactor.core.scheduler.Schedulers$Factory#newElastic(int, java.util.concurrent.ThreadFactory)',
'reactor.core.publisher.FluxSink#contextView()',
'reactor.core.publisher.MonoSink#contextView()',
'reactor.core.publisher.SynchronousSink#contextView()'
]
}

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);
}
}