Search in sources :

Example 6 with BaseSubscriber

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

the class SecurityReactorContextConfigurationTests method createSubscriberIfNecessaryWhenParentContextContainsSecurityContextAttributesThenUseParentContext.

@Test
public void createSubscriberIfNecessaryWhenParentContextContainsSecurityContextAttributesThenUseParentContext() {
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.servletRequest, this.servletResponse));
    SecurityContextHolder.getContext().setAuthentication(this.authentication);
    Context parentContext = Context.of(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES, new HashMap<>());
    BaseSubscriber<Object> parent = new BaseSubscriber<Object>() {

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

Example 7 with BaseSubscriber

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

the class RedissonListReactiveTest method testAdd.

@Test
public void testAdd() throws InterruptedException {
    final RListReactive<Long> list = redisson.getList("list");
    final CountDownLatch latch = new CountDownLatch(1);
    list.add(1L).subscribe(new BaseSubscriber<Boolean>() {

        @Override
        public void hookOnNext(Boolean value) {
            list.add(2L).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);
}
Also used : BaseSubscriber(reactor.core.publisher.BaseSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 8 with BaseSubscriber

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

the class PublisherAdder method addAll.

public Publisher<Boolean> addAll(Publisher<? extends V> c) {
    CompletableFuture<Boolean> promise = new CompletableFuture<>();
    c.subscribe(new BaseSubscriber<V>() {

        volatile boolean completed;

        AtomicLong values = new AtomicLong();

        Subscription s;

        Boolean lastSize = false;

        @Override
        protected void hookOnSubscribe(Subscription s) {
            this.s = s;
            s.request(1);
        }

        @Override
        protected void hookOnNext(V o) {
            values.getAndIncrement();
            add(o).whenComplete((res, e) -> {
                if (e != null) {
                    promise.completeExceptionally(e);
                    return;
                }
                if (res) {
                    lastSize = true;
                }
                s.request(1);
                if (values.decrementAndGet() == 0 && completed) {
                    promise.complete(lastSize);
                }
            });
        }

        @Override
        protected void hookOnComplete() {
            completed = true;
            if (values.get() == 0) {
                promise.complete(lastSize);
            }
        }
    });
    return Mono.fromCompletionStage(promise);
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) CompletableFuture(java.util.concurrent.CompletableFuture) Mono(reactor.core.publisher.Mono) BaseSubscriber(reactor.core.publisher.BaseSubscriber) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Subscription(org.reactivestreams.Subscription)

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