Search in sources :

Example 1 with BaseSubscriber

use of reactor.core.publisher.BaseSubscriber in project redisson by redisson.

the class RedissonListReactiveTest method testAddAllWithIndex.

@Test
public void testAddAllWithIndex() throws InterruptedException {
    final RListReactive<Long> list = redisson.getList("list");
    final CountDownLatch latch = new CountDownLatch(1);
    list.addAll(Arrays.asList(1L, 2L, 3L)).subscribe(new BaseSubscriber<Boolean>() {

        @Override
        public void hookOnNext(Boolean element) {
            list.addAll(Arrays.asList(1L, 24L, 3L)).subscribe(new BaseSubscriber<Boolean>() {

                @Override
                public void hookOnNext(Boolean value) {
                    latch.countDown();
                }

                @Override
                public void hookOnError(Throwable error) {
                    Assertions.fail(error.getMessage());
                }
            });
        }

        @Override
        public void hookOnError(Throwable error) {
            Assertions.fail(error.getMessage());
        }
    });
    latch.await();
    assertThat(sync(list)).containsExactly(1L, 2L, 3L, 1L, 24L, 3L);
}
Also used : BaseSubscriber(reactor.core.publisher.BaseSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with BaseSubscriber

use of reactor.core.publisher.BaseSubscriber in project spring-security by spring-projects.

the class SecurityReactorContextConfigurationTests method createSubscriberIfNecessaryWhenWebSecurityContextAvailableThenCreateWithParentContext.

@Test
public void createSubscriberIfNecessaryWhenWebSecurityContextAvailableThenCreateWithParentContext() {
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.servletRequest, this.servletResponse));
    SecurityContextHolder.getContext().setAuthentication(this.authentication);
    String testKey = "test_key";
    String testValue = "test_value";
    BaseSubscriber<Object> parent = new BaseSubscriber<Object>() {

        @Override
        public Context currentContext() {
            return Context.of(testKey, testValue);
        }
    };
    CoreSubscriber<Object> subscriber = this.subscriberRegistrar.createSubscriberIfNecessary(parent);
    Context resultContext = subscriber.currentContext();
    assertThat(resultContext.getOrEmpty(testKey)).hasValue(testValue);
    Map<Object, Object> securityContextAttributes = resultContext.getOrDefault(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES, null);
    assertThat(securityContextAttributes).hasSize(3);
    assertThat(securityContextAttributes).contains(entry(HttpServletRequest.class, this.servletRequest), entry(HttpServletResponse.class, this.servletResponse), entry(Authentication.class, this.authentication));
}
Also used : Context(reactor.util.context.Context) SpringTestContext(org.springframework.security.config.test.SpringTestContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Authentication(org.springframework.security.core.Authentication) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with BaseSubscriber

use of reactor.core.publisher.BaseSubscriber in project spring-security by spring-projects.

the class SecurityReactorContextConfigurationTests method createSubscriberIfNecessaryWhenSubscriberContextContainsSecurityContextAttributesThenReturnOriginalSubscriber.

@Test
public void createSubscriberIfNecessaryWhenSubscriberContextContainsSecurityContextAttributesThenReturnOriginalSubscriber() {
    Context context = Context.of(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES, new HashMap<>());
    BaseSubscriber<Object> originalSubscriber = new BaseSubscriber<Object>() {

        @Override
        public Context currentContext() {
            return context;
        }
    };
    CoreSubscriber<Object> resultSubscriber = this.subscriberRegistrar.createSubscriberIfNecessary(originalSubscriber);
    assertThat(resultSubscriber).isSameAs(originalSubscriber);
}
Also used : Context(reactor.util.context.Context) SpringTestContext(org.springframework.security.config.test.SpringTestContext) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Test(org.junit.jupiter.api.Test)

Example 4 with BaseSubscriber

use of reactor.core.publisher.BaseSubscriber in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method cancelWhileItemCached.

// gh-22720
@Test
public void cancelWhileItemCached() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Mono.fromCallable(() -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        return dataBuffer;
    }), publisher -> {
        ZeroDemandSubscriber subscriber = new ZeroDemandSubscriber();
        publisher.subscribe(subscriber);
        return Mono.never();
    });
    BaseSubscriber<Void> subscriber = new BaseSubscriber<>() {
    };
    operator.subscribe(subscriber);
    subscriber.cancel();
    bufferFactory.checkForLeaks();
}
Also used : BaseSubscriber(reactor.core.publisher.BaseSubscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with BaseSubscriber

use of reactor.core.publisher.BaseSubscriber in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method errorFromWriteSourceWhileItemCached.

// gh-22720
@Test
public void errorFromWriteSourceWhileItemCached() {
    // 1. First item received
    // 2. writeFunction applied and writeCompletionBarrier subscribed to it
    // 3. Write Publisher fails right after that and before request(n) from server
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ZeroDemandSubscriber writeSubscriber = new ZeroDemandSubscriber();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Flux.create(sink -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        sink.next(dataBuffer);
        sink.error(new IllegalStateException("err"));
    }), publisher -> {
        publisher.subscribe(writeSubscriber);
        return Mono.never();
    });
    operator.subscribe(new BaseSubscriber<Void>() {
    });
    try {
        // Let cached signals ("foo" and error) be published..
        writeSubscriber.signalDemand(1);
    } catch (Throwable ex) {
        assertThat(ex.getCause()).isNotNull();
        assertThat(ex.getCause().getMessage()).isEqualTo("err");
    }
    bufferFactory.checkForLeaks();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Signal(reactor.core.publisher.Signal) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

BaseSubscriber (reactor.core.publisher.BaseSubscriber)8 Test (org.junit.jupiter.api.Test)7 SpringTestContext (org.springframework.security.config.test.SpringTestContext)3 Context (reactor.util.context.Context)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Publisher (org.reactivestreams.Publisher)2 Subscription (org.reactivestreams.Subscription)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 LeakAwareDataBufferFactory (org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory)2 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)2 Mono (reactor.core.publisher.Mono)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Executors (java.util.concurrent.Executors)1