rx.observables.MathObservable Java Examples

The following examples show how to use rx.observables.MathObservable. 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: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeNumericObservable_whenCalculatingMin_ThenSuccessfullObtainingMinValue() {
    // given
    Observable<Integer> sourceObservable = Observable.range(1, 20);
    TestSubscriber<Integer> subscriber = TestSubscriber.create();

    // when
    MathObservable.min(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(1);
}
 
Example #2
Source File: MergedObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleRemoteObservableMerge() {
    // setup
    Observable<Integer> os = Observable.range(0, 101);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.integer());
    server.start();
    // connect
    Observable<Integer> ro = RemoteObservable.connect("localhost", serverPort, Codecs.integer());

    MergedObservable<Integer> merged = MergedObservable.createWithReplay(1);
    merged.mergeIn("t1", ro);

    // assert
    MathObservable.sumInteger(Observable.merge(merged.get()))
            .toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #3
Source File: MergedObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test
public void testFixedMerge() throws InterruptedException {

    MergedObservable<Integer> merged = MergedObservable.createWithReplay(2);
    merged.mergeIn("t1", Observable.range(1, 50));
    merged.mergeIn("t2", Observable.range(51, 50));

    MathObservable.sumInteger(Observable.merge(merged.get()))
            .toBlocking()
            .forEach(new Action1<Integer>() {
                @Override
                public void call(Integer t1) {
                    Assert.assertEquals(5050, t1.intValue());
                }
            });
}
 
Example #4
Source File: DatabaseTestBase.java    From rxjava-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testJdbcObservableCountLettersInAllNames() {
    int count = MathObservable
            .sumInteger(db()
                    // select
                    .select("select name from person where name >?")
                    // set name
                    .parameter("ALEX")
                    // count letters
                    .get(COUNT_LETTERS_IN_NAME))
            // first result
            .first()
            // block and get result
            .toBlocking().single();
    assertEquals(19, count);
}
 
Example #5
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testError() {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    Observable<Integer> os = Observable.create(new OnSubscribe<Integer>() {
        @Override
        public void call(Subscriber<? super Integer> subscriber) {
            subscriber.onNext(1);
            subscriber.onError(new Exception("test-exception"));
        }
    });
    int serverPort = portSelector.acquirePort();
    RemoteObservable.serve(serverPort, os, Codecs.integer())
            .start();
    Observable<Integer> oc = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #6
Source File: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeNumericObservable_whenCalculatingAverage_ThenSuccessfull() {
    // given
    Observable<Integer> sourceObservable = Observable.range(1, 20);

    TestSubscriber<Integer> subscriber = TestSubscriber.create();

    // when
    MathObservable.averageInteger(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(10);
}
 
Example #7
Source File: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeNumericObservable_whenCalculatingSum_ThenSuccessfull() {
    // given
    Observable<Integer> sourceObservable = Observable.range(1, 20);
    TestSubscriber<Integer> subscriber = TestSubscriber.create();

    // when
    MathObservable.sumInteger(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(210);

}
 
Example #8
Source File: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRangeNumericObservable_whenCalculatingMax_ThenSuccessfullObtainingMaxValue() {
    // given
    Observable<Integer> sourceObservable = Observable.range(1, 20);
    TestSubscriber<Integer> subscriber = TestSubscriber.create();

    // when
    MathObservable.max(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(20);
}
 
Example #9
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testFailedToConnect() throws InterruptedException {
    // setup
    Observable<Integer> os = Observable.range(0, 101);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    int wrongPort = portSelector.acquirePort();

    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.integer());
    server.start();
    // connect
    Observable<Integer> oc = RemoteObservable.connect("localhost", wrongPort, Codecs.integer());
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #10
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test
public void testServeObservableByName() throws InterruptedException {
    // setup
    Observable<Integer> os = Observable.range(0, 101);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, "integers-from-0-100", os, Codecs.integer());
    server.start();
    // connect to observable by name
    Observable<Integer> oc = RemoteObservable.connect(new ConnectToObservable.Builder<Integer>()
            .host("localhost")
            .port(serverPort)
            .name("integers-from-0-100")
            .decoder(Codecs.integer())
            .build())
            .getObservable();
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #11
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test
public void testServeUsingByteEncodedObservable() throws InterruptedException {
    // manual encode data to byte[]
    Observable<byte[]> os = Observable.range(0, 101)
            // convert to bytes
            .map(new Func1<Integer, byte[]>() {
                @Override
                public byte[] call(Integer value) {
                    return ByteBuffer.allocate(4).putInt(value).array();
                }
            });
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.bytearray());
    server.start();
    // connect
    Observable<Integer> oc = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #12
Source File: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenItemObservable_whenCalculatingMaxWithComparator_ThenSuccessfullObtainingMaxItem() {
    // given
    Item five = new Item(5);
    List<Item> list = Arrays.asList(new Item(1), new Item(2), new Item(3), new Item(4), five);
    Observable<Item> itemObservable = Observable.from(list);

    TestSubscriber<Item> subscriber = TestSubscriber.create();

    // when
    MathObservable.from(itemObservable)
        .max(Comparator.comparing(Item::getId))
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(five);

}
 
Example #13
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test
public void testServeObservable() throws InterruptedException {
    // setup
    Observable<Integer> os = Observable.range(0, 101).subscribeOn(Schedulers.io());
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.integer());
    server.start();
    // connect
    Observable<Integer> oc = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #14
Source File: RxMathematicalOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenItemObservable_whenCalculatingMinWithComparator_ThenSuccessfullObtainingMinItem() {
    // given
    Item one = new Item(1);
    List<Item> list = Arrays.asList(one, new Item(2), new Item(3), new Item(4), new Item(5));
    TestSubscriber<Item> subscriber = TestSubscriber.create();
    Observable<Item> itemObservable = Observable.from(list);

    // when
    MathObservable.from(itemObservable)
        .min(Comparator.comparing(Item::getId))
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValue(one);

}
 
Example #15
Source File: DynamicConnectionSetTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeInWithDeltaEndpointService() {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();

    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, Observable.range(51, 50), Codecs.integer());

    server1.start();
    server2.start();

    ReplaySubject<List<Endpoint>> subject = ReplaySubject.create();
    List<Endpoint> endpoints = new LinkedList<Endpoint>();
    endpoints.add(new Endpoint("localhost", server1Port));
    endpoints.add(new Endpoint("localhost", server2Port));
    subject.onNext(endpoints);
    subject.onCompleted();

    DynamicConnectionSet<Integer> cm
            = DynamicConnectionSet.create(new ConnectToObservable.Builder<Integer>()
            .decoder(Codecs.integer()));
    cm.setEndpointInjector(new ToDeltaEndpointInjector(subject));

    int sum = MathObservable.sumInteger(Observable.merge(cm.observables()))
            .toBlocking()
            .last();
    Assert.assertEquals(5050, sum);

}
 
Example #16
Source File: MetricsTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionMetrics() {
    // setup
    Observable<Integer> os = Observable.range(1, 1000);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.integer());
    server.start();
    // connect
    ConnectToObservable<Integer> cc = new ConnectToObservable.Builder<Integer>()
            .host("localhost")
            .port(serverPort)
            .decoder(Codecs.integer())
            .build();

    RemoteRxConnection<Integer> rc = RemoteObservable.connect(cc);
    // assert
    MathObservable.sumInteger(rc.getObservable()).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(500500, t1.intValue()); // sum of number 0-100
        }
    });

    Assert.assertEquals(1000, rc.getMetrics().getOnNextCount());
    Assert.assertEquals(0, rc.getMetrics().getOnErrorCount());
    Assert.assertEquals(1, rc.getMetrics().getOnCompletedCount());
}
 
Example #17
Source File: FixedConnectionSetTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeInWithDeltaEndpointService() {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();

    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, Observable.range(51, 50), Codecs.integer());

    server1.start();
    server2.start();

    ReplaySubject<List<Endpoint>> subject = ReplaySubject.create();
    List<Endpoint> endpoints = new LinkedList<Endpoint>();
    endpoints.add(new Endpoint("localhost", server1Port));
    endpoints.add(new Endpoint("localhost", server2Port));
    subject.onNext(endpoints);

    FixedConnectionSet<Integer> cm
            = FixedConnectionSet.create(2, new ConnectToObservable.Builder<Integer>()
                    .decoder(Codecs.integer()),
            new ToDeltaEndpointInjector(subject));

    int sum = MathObservable.sumInteger(Observable.merge(cm.getObservables()))
            .toBlocking()
            .last();
    Assert.assertEquals(5050, sum);

}
 
Example #18
Source File: MetricsTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerMetrics() {
    // setup
    Observable<Integer> os = Observable.range(1, 1000);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.integer());
    server.start();
    // connect
    ConnectToObservable<Integer> cc = new ConnectToObservable.Builder<Integer>()
            .host("localhost")
            .port(serverPort)
            .decoder(Codecs.integer())
            .build();

    Observable<Integer> oc = RemoteObservable.connect(cc).getObservable();
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(500500, t1.intValue()); // sum of number 0-100
        }
    });

    Assert.assertEquals(1000, server.getMetrics().getOnNextCount());
    Assert.assertEquals(0, server.getMetrics().getOnErrorCount());
    Assert.assertEquals(1, server.getMetrics().getOnCompletedCount());
}
 
Example #19
Source File: ServeNestedTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testServeNested() {

    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();

    Observable<Observable<Integer>> oo = Observable.just(Observable.range(1, 100));

    RemoteRxServer server = new RemoteRxServer.Builder()
            .port(serverPort)
            .addObservable(new ServeNestedObservable.Builder<Integer>()
                    .name("ints")
                    .encoder(Codecs.integer())
                    .observable(oo)
                    .build())
            .build();

    server.start();

    Observable<Integer> ro = RemoteObservable.connect(new ConnectToObservable.Builder<Integer>()
            .host("localhost")
            .port(serverPort)
            .name("ints")
            .decoder(Codecs.integer())
            .build())
            .getObservable();


    MathObservable.sumInteger(ro).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #20
Source File: AggregationServiceImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public long getApplicationSize(ApplicationScope applicationScope) {
    final IndexLocationStrategy indexLocationStrategy = indexLocationStrategyFactory.getIndexLocationStrategy(applicationScope);
    EntityIndex entityIndex = entityIndexFactory.createEntityIndex(indexLocationStrategy);
    GraphManager graphManager = graphManagerFactory.createEdgeManager(applicationScope);
    Long sum = ObservableTimer.time(
        MathObservable.sumLong(
            graphManager.getEdgeTypesFromSource(new SimpleSearchEdgeType(applicationScope.getApplication(), CpNamingUtils.EDGE_COLL_PREFIX, Optional.<String>absent()))
                .map(type -> CpNamingUtils.createCollectionSearchEdge(applicationScope.getApplication(), type))
                .map(edge -> entityIndex.getTotalEntitySizeInBytes(edge))
        ), sumTimer).toBlocking().last();

    return sum.longValue();
}
 
Example #21
Source File: DynamicConnectionSetTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeInConnections() throws InterruptedException {

    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();

    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, Observable.range(51, 50), Codecs.integer());

    server1.start();
    server2.start();

    EndpointInjector staticEndpoints = new EndpointInjector() {
        @Override
        public Observable<EndpointChange> deltas() {
            return Observable.create(new OnSubscribe<EndpointChange>() {
                @Override
                public void call(Subscriber<? super EndpointChange> subscriber) {
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server1Port, "1")));
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server2Port, "2")));
                    subscriber.onCompleted();
                }
            });
        }
    };

    DynamicConnectionSet<Integer> cm
            = DynamicConnectionSet.create(new ConnectToObservable.Builder<Integer>()
            .decoder(Codecs.integer()));
    cm.setEndpointInjector(staticEndpoints);

    int sum = MathObservable.sumInteger(Observable.merge(cm.observables()))
            .toBlocking()
            .last();
    Assert.assertEquals(5050, sum);

}
 
Example #22
Source File: MergedObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
public void testMergeFromMultipleSources() {
    // setup
    Observable<Integer> os1 = Observable.range(1, 100);
    Observable<Integer> os2 = Observable.range(101, 100);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort1 = portSelector.acquirePort();
    int serverPort2 = portSelector.acquirePort();


    RemoteRxServer server1 = RemoteObservable.serve(serverPort1, os1, Codecs.integer());
    server1.start();

    RemoteRxServer server2 = RemoteObservable.serve(serverPort2, os2, Codecs.integer());
    server2.start();

    // connect
    Observable<Integer> ro1 = RemoteObservable.connect("localhost", serverPort1, Codecs.integer());
    Observable<Integer> ro2 = RemoteObservable.connect("localhost", serverPort2, Codecs.integer());

    MergedObservable<Integer> merged = MergedObservable.createWithReplay(2);
    merged.mergeIn("t1", ro1);
    merged.mergeIn("t2", ro2);

    // assert
    MathObservable.sumInteger(Observable.merge(merged.get()))
            .toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(20100, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #23
Source File: MergedObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testThreeRemoteObservablesMerge() {
    // setup
    Observable<Integer> os = Observable.range(0, 101);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = RemoteObservable.serve(serverPort, os, Codecs.integer());
    server.start();
    // connect
    Observable<Integer> ro1 = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    Observable<Integer> ro2 = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    Observable<Integer> ro3 = RemoteObservable.connect("localhost", serverPort, Codecs.integer());

    MergedObservable<Integer> merged = MergedObservable.createWithReplay(3);
    merged.mergeIn("t1", ro1);
    merged.mergeIn("t2", ro2);
    merged.mergeIn("t3", ro3);

    // assert
    MathObservable.sumInteger(Observable.merge(merged.get()))
            .toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(15150, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #24
Source File: MergedObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testMergeInBadObservable() throws InterruptedException {

    MergedObservable<Integer> merged = MergedObservable.createWithReplay(2);
    merged.mergeIn("t1", Observable.range(1, 50));

    Observable<Integer> badObservable = Observable.create(new OnSubscribe<Integer>() {
        @Override
        public void call(Subscriber<? super Integer> subscriber) {
            for (int i = 100; i < 200; i++) {
                subscriber.onNext(i);
                if (i == 150) {
                    subscriber.onError(new Exception("bad"));
                }
            }
        }
    });
    merged.mergeIn("t2", badObservable);

    MathObservable.sumInteger(Observable.merge(merged.get()))
            .toBlocking()
            .forEach(new Action1<Integer>() {
                @Override
                public void call(Integer t1) {
                    Assert.assertEquals(5050, t1.intValue());
                }
            });
}
 
Example #25
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubscribeParametersByFilteringOnServer() {

    // setup
    Observable<Integer> os = Observable.range(0, 101);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();
    RemoteRxServer server = new RemoteRxServer.Builder()
            .port(serverPort)
            .addObservable(new ServeObservable.Builder<Integer>()
                    .encoder(Codecs.integer())
                    .observable(os)
                    .serverSideFilter(ServerSideFilters.oddsAndEvens())
                    .build())
            .build();
    server.start();

    // connect
    Map<String, String> subscribeParameters = new HashMap<String, String>();
    subscribeParameters.put("type", "even");

    Observable<Integer> oc = RemoteObservable.connect(new ConnectToObservable.Builder<Integer>()
            .host("localhost")
            .port(serverPort)
            .subscribeParameters(subscribeParameters)
            .decoder(Codecs.integer())
            .build())
            .getObservable();

    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(2550, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #26
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
public void testRoundRobinSlottingServer() {
    // setup
    Observable<Integer> os = Observable.range(1, 100);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();

    RemoteRxServer server = new RemoteRxServer.Builder()
            .port(serverPort)
            .addObservable(new ServeObservable.Builder<Integer>()
                    .encoder(Codecs.integer())
                    .observable(os)
                    .slottingStrategy(new RoundRobin<Integer>())
                    .build())
            .build();
    server.start();

    // connect with 2 remotes
    Observable<Integer> oc1 = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    Observable<Integer> oc2 = RemoteObservable.connect("localhost", serverPort, Codecs.integer());

    // merge results
    Observable<Integer> merged = Observable.merge(oc1, oc2);
    // assert
    MathObservable.sumInteger(merged).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });

}
 
Example #27
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testServedMergedObservablesAddAfterServe() {
    // setup
    Observable<Integer> os1 = Observable.range(0, 100);
    Observable<Integer> os2 = Observable.range(100, 100);
    ReplaySubject<Observable<Integer>> subject = ReplaySubject.create();
    subject.onNext(os1);
    subject.onNext(os2);
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();

    RemoteRxServer server = new RemoteRxServer.Builder()
            .port(serverPort)
            .addObservable(new ServeObservable.Builder<Integer>()
                    .encoder(Codecs.integer())
                    .observable(Observable.merge(subject))
                    .build())
            .build();
    server.start();

    // add after serve
    Observable<Integer> os3 = Observable.range(200, 101);
    subject.onNext(os3);
    subject.onCompleted();

    // connect
    Observable<Integer> oc = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(45150, t1.intValue()); // sum of number 0-200
        }
    });
}
 
Example #28
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testServedMergedObservables() {
    // setup
    Observable<Integer> os1 = Observable.range(0, 101);
    Observable<Integer> os2 = Observable.range(100, 101);
    ReplaySubject<Observable<Integer>> subject = ReplaySubject.create();
    subject.onNext(os1);
    subject.onNext(os2);
    subject.onCompleted();

    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int port = portSelector.acquirePort();

    RemoteRxServer server = new RemoteRxServer.Builder()
            .port(port)
            .addObservable(new ServeObservable.Builder<Integer>()
                    .encoder(Codecs.integer())
                    .observable(Observable.merge(subject))
                    .build())
            .build();

    // serve
    server.start();

    // connect
    Observable<Integer> oc = RemoteObservable.connect("localhost", port, Codecs.integer());
    // assert
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(20200, t1.intValue()); // sum of number 0-200
        }
    });
}
 
Example #29
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testServeNestedObservable() throws InterruptedException {
    // setup
    Observable<Observable<Integer>> os = Observable.just(Observable.range(1, 100),
            Observable.range(1, 100), Observable.range(1, 100));
    // serve
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    int serverPort = portSelector.acquirePort();

    RemoteRxServer server = new RemoteRxServer.Builder()
            .port(serverPort)
            .addObservable(new ServeNestedObservable.Builder<Integer>()
                    .encoder(Codecs.integer())
                    .observable(os)
                    .build())
            .build();

    server.start();
    // connect
    Observable<Integer> oc = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    // assert

    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(15150, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #30
Source File: FixedConnectionSetTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeInConnections() throws InterruptedException {

    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();

    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, Observable.range(51, 50), Codecs.integer());

    server1.start();
    server2.start();

    EndpointInjector staticEndpoints = new EndpointInjector() {
        @Override
        public Observable<EndpointChange> deltas() {
            return Observable.create(new OnSubscribe<EndpointChange>() {
                @Override
                public void call(Subscriber<? super EndpointChange> subscriber) {
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server1Port, "1")));
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server2Port, "2")));
                    subscriber.onCompleted();
                }
            });
        }
    };

    FixedConnectionSet<Integer> cm
            = FixedConnectionSet.create(2, new ConnectToObservable.Builder<Integer>()
                    .decoder(Codecs.integer()),
            staticEndpoints);

    int sum = MathObservable.sumInteger(Observable.merge(cm.getObservables()))
            .toBlocking()
            .last();
    Assert.assertEquals(5050, sum);

}