Java Code Examples for io.reactivex.subjects.Subject#subscribe()

The following examples show how to use io.reactivex.subjects.Subject#subscribe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteEvaluatesJobInputsAsExpected() throws InterruptedException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${inputs.foo}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();
    assertThat(stringFromStdout).isEqualTo("a"); // from spec
}
 
Example 2
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteEvaluatesJoinAsExpected() throws InterruptedException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${join(',', inputs.someList)}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();
    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();

    assertThat(stringFromStdout).isEqualTo("a,b,c,d"); // From the input fixture
}
 
Example 3
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteEvaluatesToStringAsExpected() throws InterruptedException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${toString(inputs.someString)}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();
    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();

    assertThat(stringFromStdout).isEqualTo("hello, world!"); // from input fixture
}
 
Example 4
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteEvaluatesOutputDirAsExpected() throws InterruptedException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${outputDir}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();
    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();

    assertThat(Files.exists(Paths.get(stringFromStdout)));
}
 
Example 5
Source File: JobManager.java    From jobson with Apache License 2.0 5 votes vote down vote up
private void advanceJobQueue() {
    final QueuedJob queuedJob = jobQueue.poll();

    if (queuedJob == null) return;

    final Subject<byte[]> stdout = PublishSubject.create();
    final Subject<byte[]> stderr = PublishSubject.create();

    jobDAO.appendStdout(queuedJob.getId(), stdout);
    jobDAO.appendStderr(queuedJob.getId(), stderr);

    stdout.subscribe(queuedJob.getQueuedListeners().getOnStdoutListener());
    stderr.subscribe(queuedJob.getQueuedListeners().getOnStderrListener());

    try {
        final CancelablePromise<JobExecutionResult> executionPromise =
                jobExecutor.execute(queuedJob, JobEventListeners.create(stdout, stderr));

        final ExecutingJob executingJob =
                ExecutingJob.fromQueuedJob(queuedJob, now(), stdout, stderr);

        executingJobs.put(executingJob.getId(), executingJob);

        updateJobStatus(queuedJob.getId(), RUNNING, "Submitted to executor");

        executionPromise.thenAccept(res -> {
            onExecutionFinished(executingJob, res);
        });

        executingJob.getCompletionPromise().onCancel(() -> {
            executionPromise.cancel(true);
        });
    } catch (Throwable ex) {
        log.error("Error starting job execution: " + ex.toString());
        updateJobStatus(queuedJob.getId(), FATAL_ERROR, "Error executing job: " + ex.toString());
    }
}
 
Example 6
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWritesStdoutToTheStdoutListener() throws Throwable {
    final JobExecutor jobExecutor = getInstance();
    final String msgSuppliedToEcho = generateRandomString();
    final PersistedJob req =
            standardRequestWithCommand("echo", msgSuppliedToEcho);
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();
    assertThat(stringFromStdout).isEqualTo(msgSuppliedToEcho);
}
 
Example 7
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWritesStderrToTheStderrListener() throws Throwable {
    final JobExecutor jobExecutor = getInstance();
    final String msgSuppliedToEcho = generateRandomString();
    final String bashArg = "echo " + msgSuppliedToEcho + " 1>&2"; // TODO: Naughty.
    final PersistedJob req =
            standardRequestWithCommand("bash", "-c", bashArg);
    final AtomicReference<byte[]> bytesEchoedToStderr = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stderrSubject = PublishSubject.create();

    stderrSubject.subscribe(bytes ->
            bytesEchoedToStderr.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stderrSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStderrListener(stderrSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStderr = new String(bytesEchoedToStderr.get()).trim();
    assertThat(stringFromStderr).isEqualTo(msgSuppliedToEcho);
}
 
Example 8
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteEvaluatesToJSONFunctionAsExpected() throws InterruptedException, IOException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${toJSON(inputs)}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();

    TestHelpers.assertJSONEqual(stringFromStdout, toJSON(STANDARD_REQUEST.getInputs()));
}
 
Example 9
Source File: JobExecutorTest.java    From jobson with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteEvaluatesToFileAsExpected() throws InterruptedException, IOException {
    final JobExecutor jobExecutor = getInstance();
    final PersistedJob req =
            standardRequestWithCommand("echo", "${toFile(toJSON(inputs))}");
    final AtomicReference<byte[]> bytesEchoedToStdout = new AtomicReference<>(new byte[]{});
    final Subject<byte[]> stdoutSubject = PublishSubject.create();

    stdoutSubject.subscribe(bytes ->
            bytesEchoedToStdout.getAndUpdate(existingBytes ->
                    Bytes.concat(existingBytes, bytes)));

    final Semaphore s = new Semaphore(1);
    s.acquire();
    stdoutSubject.doOnComplete(s::release).subscribe();

    final JobEventListeners listeners =
            createStdoutListener(stdoutSubject);

    jobExecutor.execute(req, listeners);

    s.tryAcquire(TestConstants.DEFAULT_TIMEOUT, MILLISECONDS);

    final String stringFromStdout = new String(bytesEchoedToStdout.get()).trim();
    final Path p = Paths.get(stringFromStdout);

    assertThat(p.toFile().exists());

    final String loadedJson = new String(Files.readAllBytes(p));

    TestHelpers.assertJSONEqual(loadedJson, toJSON(STANDARD_REQUEST.getInputs()));
}
 
Example 10
Source File: Sandbox.java    From Reactive-Android-Programming with MIT License 5 votes vote down vote up
private static void demo0() {
    Subject<String> subject = null;

    subject.subscribe(v -> log(v));

    Observable.just("1")
            .subscribe(subject);
}
 
Example 11
Source File: Sandbox.java    From Reactive-Android-Programming with MIT License 3 votes vote down vote up
private static void demo5() throws InterruptedException {
    Subject<String> subject = AsyncSubject.create();

    Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(4)
            .map(Objects::toString)
            .subscribe(subject);

    subject.subscribe(v -> log(v));

    Thread.sleep(5100);


    subject.subscribe(v -> log(v));
}
 
Example 12
Source File: Sandbox.java    From Reactive-Android-Programming with MIT License 3 votes vote down vote up
private static void demo4() throws InterruptedException {
    Subject<String> subject = ReplaySubject.create();

    Observable.interval(0, 1, TimeUnit.SECONDS)
            .map(Objects::toString)
            .subscribe(subject);

    Thread.sleep(3100);

    subject.subscribe(v -> log(v));

}
 
Example 13
Source File: Sandbox.java    From Reactive-Android-Programming with MIT License 3 votes vote down vote up
private static void demo3() {
    Subject<String> subject = BehaviorSubject.create();

    Observable.interval(0, 2, TimeUnit.SECONDS)
            .map(v -> "A" + v)
            .subscribe(subject);

    subject.subscribe(v -> log(v));

    Observable.interval(1, 1, TimeUnit.SECONDS)
            .map(v -> "B" + v)
            .subscribe(subject);


}