Search in sources :

Example 71 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveChangeStreamOperationSupportTests method changeStreamEventsShouldBeEmittedCorrectly.

@SneakyThrows
// DATAMONGO-2089
@Test
public void changeStreamEventsShouldBeEmittedCorrectly() {
    BlockingQueue<ChangeStreamEvent<Document>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = // 
    template.changeStream(Document.class).watchCollection(// 
    "person").listen().doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 39);
    Person person3 = new Person("MongoDB", 37);
    Flux.merge(template.insert(person1).delayElement(Duration.ofMillis(2)), template.insert(person2).delayElement(Duration.ofMillis(2)), // 
    template.insert(person3).delayElement(Duration.ofMillis(2))).as(// 
    StepVerifier::create).expectNextCount(// 
    3).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).hasSize(3).allMatch(Document.class::isInstance);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StepVerifier(reactor.test.StepVerifier) Document(org.bson.Document) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Example 72 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveChangeStreamOperationSupportTests method changeStreamEventsShouldBeFilteredCorrectly.

// DATAMONGO-1803
@Test
public void changeStreamEventsShouldBeFilteredCorrectly() throws InterruptedException {
    BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
    Disposable disposable = // 
    template.changeStream(Person.class).watchCollection(// 
    Person.class).filter(// 
    where("age").gte(38)).listen().doOnNext(documents::add).subscribe();
    // just give it some time to link to the collection.
    Thread.sleep(500);
    Person person1 = new Person("Spring", 38);
    Person person2 = new Person("Data", 37);
    Person person3 = new Person("MongoDB", 39);
    Flux.merge(template.save(person1), template.save(person2).delayElement(Duration.ofMillis(50)), // 
    template.save(person3).delayElement(Duration.ofMillis(100))).as(// 
    StepVerifier::create).expectNextCount(// 
    3).verifyComplete();
    // just give it some time to link receive all events
    Thread.sleep(500);
    try {
        assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsOnly(person1, person3);
    } finally {
        disposable.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 73 with Disposable

use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.

the class ReactiveMongoRepositoryTests method shouldUseTailableCursorWithProjection.

// DATAMONGO-1444
@Test
void shouldUseTailableCursorWithProjection() throws Exception {
    // 
    template.dropCollection(Capped.class).then(// 
    template.createCollection(// 
    Capped.class, CollectionOptions.empty().size(1000).maxDocuments(100).capped())).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    template.insert(new Capped("value", Math.random())).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    BlockingQueue<CappedProjection> documents = new LinkedBlockingDeque<>(100);
    Disposable disposable = cappedRepository.findProjectionByKey("value").doOnNext(documents::add).subscribe();
    CappedProjection projection1 = documents.poll(5, TimeUnit.SECONDS);
    assertThat(projection1).isNotNull();
    assertThat(projection1.getRandom()).isNotEqualTo(0);
    template.insert(new Capped("value", Math.random())).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    CappedProjection projection2 = documents.poll(5, TimeUnit.SECONDS);
    assertThat(projection2).isNotNull();
    assertThat(projection2.getRandom()).isNotEqualTo(0);
    assertThat(documents).isEmpty();
    disposable.dispose();
}
Also used : Disposable(reactor.core.Disposable) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 74 with Disposable

use of reactor.core.Disposable in project sts4 by spring-projects.

the class ClasspathListenerManager method addClasspathListener.

public Disposable addClasspathListener(ClasspathListener classpathListener) {
    String callbackCommandId = "sts4.classpath." + RandomStringUtils.randomAlphabetic(8);
    // 1. register callback command handler in SimpleLanguageServer
    Disposable unregisterCommand = server.onCommand(callbackCommandId, (ExecuteCommandParams callbackParams) -> async.invoke(() -> {
        List<Object> args = callbackParams.getArguments();
        // Note: not sure... but args might be deserialized as com.google.gson.JsonElement's.
        // If so the code below is not correct (casts will fail).
        String projectUri = ((JsonElement) args.get(0)).getAsString();
        String name = ((JsonElement) args.get(1)).getAsString();
        boolean deleted = ((JsonElement) args.get(2)).getAsBoolean();
        Classpath classpath = gson.fromJson((JsonElement) args.get(3), Classpath.class);
        classpathListener.changed(new ClasspathListener.Event(projectUri, name, deleted, classpath));
        return "done";
    }));
    // 2. register the callback command with the client
    String registrationId = UUID.randomUUID().toString();
    RegistrationParams params = new RegistrationParams(ImmutableList.of(new Registration(registrationId, WORKSPACE_EXECUTE_COMMAND, ImmutableMap.of("commands", ImmutableList.of(callbackCommandId)))));
    server.getClient().registerCapability(params).join();
    // 3. call the client to ask it to call that callback
    server.getClient().addClasspathListener(new ClasspathListenerParams(callbackCommandId)).join();
    // Cleanups:
    return () -> {
        try {
            log.info("Unregistering classpath callback " + callbackCommandId + " ...");
            this.server.getClient().removeClasspathListener(new ClasspathListenerParams(callbackCommandId)).join();
            log.info("Unregistering classpath callback " + callbackCommandId + " OK");
            this.server.getClient().unregisterCapability(new UnregistrationParams(ImmutableList.of(new Unregistration(registrationId, WORKSPACE_EXECUTE_COMMAND)))).join();
            unregisterCommand.dispose();
        } catch (Exception e) {
            log.error("", e);
        }
    };
}
Also used : Disposable(reactor.core.Disposable) UnregistrationParams(org.eclipse.lsp4j.UnregistrationParams) Unregistration(org.eclipse.lsp4j.Unregistration) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) RegistrationParams(org.eclipse.lsp4j.RegistrationParams) JsonElement(com.google.gson.JsonElement) Registration(org.eclipse.lsp4j.Registration) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 75 with Disposable

use of reactor.core.Disposable in project azure-tools-for-java by Microsoft.

the class SpringCloudDeploymentConfigurationState method execute.

@Override
@Nullable
public ExecutionResult execute(Executor executor, @NotNull ProgramRunner<?> runner) throws ExecutionException {
    final RunProcessHandler processHandler = new RunProcessHandler();
    processHandler.addDefaultListener();
    processHandler.startNotify();
    processHandler.setProcessTerminatedHandler(RunProcessHandler.DO_NOTHING);
    final ConsoleMessager messager = new ConsoleMessager(processHandler);
    final ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(this.project).getConsole();
    consoleView.attachToProcess(processHandler);
    final Disposable subscribe = Mono.fromCallable(() -> this.execute(messager)).doOnTerminate(processHandler::notifyComplete).subscribeOn(Schedulers.boundedElastic()).subscribe((res) -> messager.success("Deploy succeed!"), messager::error);
    processHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(@NotNull ProcessEvent event) {
            subscribe.dispose();
        }
    });
    return new DefaultExecutionResult(consoleView, processHandler);
}
Also used : Disposable(reactor.core.Disposable) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) DefaultExecutionResult(com.intellij.execution.DefaultExecutionResult) ConsoleView(com.intellij.execution.ui.ConsoleView) ProcessEvent(com.intellij.execution.process.ProcessEvent) RunProcessHandler(com.microsoft.intellij.RunProcessHandler) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Disposable (reactor.core.Disposable)118 Test (org.junit.jupiter.api.Test)99 CountDownLatch (java.util.concurrent.CountDownLatch)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 StepVerifier (reactor.test.StepVerifier)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Duration (java.time.Duration)15 List (java.util.List)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Subscription (org.reactivestreams.Subscription)15 TimeUnit (java.util.concurrent.TimeUnit)14 Timeout (org.junit.jupiter.api.Timeout)13 CoreSubscriber (reactor.core.CoreSubscriber)12 ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 Disabled (org.junit.jupiter.api.Disabled)11 Scannable (reactor.core.Scannable)11 Fuseable (reactor.core.Fuseable)10