@Test public void mustBeAbleToRecover() throws Exception { final ManualProbe<Integer> publisherProbe = TestPublisher.manualProbe(true, system); final JavaTestKit probe = new JavaTestKit(system); final Source<Integer, NotUsed> source = Source.fromPublisher(publisherProbe) .map( elem -> { if (elem == 1) throw new RuntimeException("ex"); else return elem; }) .recover(new PFBuilder<Throwable, Integer>().matchAny(ex -> 0).build()); final CompletionStage<Done> future = source.runWith( Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); final PublisherProbeSubscription<Integer> s = publisherProbe.expectSubscription(); s.sendNext(0); probe.expectMsgEquals(0); s.sendNext(1); probe.expectMsgEquals(0); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); }
@Test public void testReturnNullWhenNoProperty() throws Exception { sm.register(SOURCE, testFile); Source source = sm.getSource(SOURCE); assertNull(source.getFileProperty("skjbnskb")); assertNull(source.getProperty("skjbnskb")); }
@Test public void testOverrideFileProperty() throws Exception { sm.register(SOURCE, testFile); Source source = sm.getSource(SOURCE); String fileProp = "testFileProp"; associateFile(source, fileProp); File file = source.createFileProperty(fileProp); FileOutputStream fis = new FileOutputStream(file); fis.write("newcontent".getBytes()); fis.close(); source.deleteProperty(fileProp); File dir = sm.getSourceInfoDirectory(); File[] content = dir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return !name.startsWith("."); } }); assertEquals(2, content.length); String[] res = new String[] {content[0].getName(), content[1].getName()}; Arrays.sort(res); String[] comp = new String[] {"directory.xml", "spatial_ref_sys_extended.gdms"}; assertArrayEquals(res, comp); }
@Test public void testAssociateFile() throws Exception { sm.register(SOURCE, testFile); String statistics = "statistics"; Source source = sm.getSource(SOURCE); String rcStr = associateFile(source, statistics); assertEquals(sm.getSource(SOURCE).getFilePropertyNames().length, 1); sm.saveStatus(); sm.shutdown(); sm.init(); assertEquals(sm.getSource(SOURCE).getFilePropertyNames().length, 1); String statsContent = source.getFilePropertyContentsAsString(statistics); assertEquals(statsContent, rcStr); File f = source.getFileProperty(statistics); DataInputStream dis = new DataInputStream(new FileInputStream(f)); byte[] content = new byte[dis.available()]; dis.readFully(content); assertEquals(new String(content), rcStr); }
@Test public void mustBeAbleToUseZipWith() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable<String> input1 = Arrays.asList("A", "B", "C"); final Iterable<String> input2 = Arrays.asList("D", "E", "F"); Source.from(input1) .zipWith( Source.from(input2), new Function2<String, String, String>() { public String apply(String s1, String s2) { return s1 + "-" + s2; } }) .runForeach( new Procedure<String>() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals("A-D"); probe.expectMsgEquals("B-E"); probe.expectMsgEquals("C-F"); }
@Test public void mustProduceTicks() throws Exception { final JavaTestKit probe = new JavaTestKit(system); Source<String, Cancellable> tickSource = Source.tick( FiniteDuration.create(1, TimeUnit.SECONDS), FiniteDuration.create(500, TimeUnit.MILLISECONDS), "tick"); @SuppressWarnings("unused") Cancellable cancellable = tickSource .to( Sink.foreach( new Procedure<String>() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } })) .run(materializer); probe.expectNoMsg(FiniteDuration.create(600, TimeUnit.MILLISECONDS)); probe.expectMsgEquals("tick"); probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS)); probe.expectMsgEquals("tick"); probe.expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS)); }
@Test public void mustBeAbleToUseFlatMapMerge() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable<Integer> input1 = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); final Iterable<Integer> input2 = Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19); final Iterable<Integer> input3 = Arrays.asList(20, 21, 22, 23, 24, 25, 26, 27, 28, 29); final Iterable<Integer> input4 = Arrays.asList(30, 31, 32, 33, 34, 35, 36, 37, 38, 39); final List<Source<Integer, NotUsed>> mainInputs = new ArrayList<Source<Integer, NotUsed>>(); mainInputs.add(Source.from(input1)); mainInputs.add(Source.from(input2)); mainInputs.add(Source.from(input3)); mainInputs.add(Source.from(input4)); CompletionStage<List<Integer>> future = Source.from(mainInputs) .flatMapMerge(3, ConstantFun.<Source<Integer, NotUsed>>javaIdentityFunction()) .grouped(60) .runWith(Sink.<List<Integer>>head(), materializer); List<Integer> result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); final Set<Integer> set = new HashSet<Integer>(); for (Integer i : result) { set.add(i); } final Set<Integer> expected = new HashSet<Integer>(); for (int i = 0; i < 40; ++i) { expected.add(i); } assertEquals(expected, set); }
@Test public void mustWorkFromFuture() throws Exception { final Iterable<String> input = Arrays.asList("A", "B", "C"); CompletionStage<String> future1 = Source.from(input).runWith(Sink.<String>head(), materializer); CompletionStage<String> future2 = Source.fromCompletionStage(future1).runWith(Sink.<String>head(), materializer); String result = future2.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("A", result); }
@Test public void testRemoveStringProperty() throws Exception { sm.register(SOURCE, testFile); Source source = sm.getSource(SOURCE); String stringProp = "testFileProp"; associateString(source, stringProp); source.deleteProperty(stringProp); assertEquals(source.getStringPropertyNames().length, 0); assertEquals(source.getFilePropertyNames().length, 0); }
@Test public void testRemoveAll() throws Exception { sm.register(SOURCE, testFile); Source src = sm.getSource(SOURCE); associateFile(src, "statisticsFile"); associateString(src, "statistics"); sm.removeAll(); sm.register(SOURCE, testFile); src = sm.getSource(SOURCE); assertEquals(src.getStringPropertyNames().length, 0); assertEquals(src.getFilePropertyNames().length, 0); }
@Test public void nameAndPathFromLoad() { XMLDocument doc = db.createFolder("/top").documents().load(Name.create(db, "foo"), Source.xml("<root/>")); assertEquals("foo", doc.name()); assertEquals("/top/foo", doc.path()); }
@Test public void mustBeAbleToUseVoidTypeInForeach() { final JavaTestKit probe = new JavaTestKit(system); final java.lang.Iterable<String> input = Arrays.asList("a", "b", "c"); Source<String, NotUsed> ints = Source.from(input); final CompletionStage<Done> completion = ints.runForeach(elem -> probe.getRef().tell(elem, ActorRef.noSender()), materializer); completion.thenAccept(elem -> probe.getRef().tell(String.valueOf(elem), ActorRef.noSender())); probe.expectMsgEquals("a"); probe.expectMsgEquals("b"); probe.expectMsgEquals("c"); probe.expectMsgEquals("Done"); }
@Test public void resolve() throws RuleBaseException, TransformException { final Node m1 = content.query().single("/id('m1')").node(); final Node m2 = content.query().single("/id('m2')").node(); final SortBlock mockBlock = mockery.mock(SortBlock.class); SortBlock block = new SortBlock( db.getFolder("/") .documents() .load( Name.create("rule"), Source.xml("<rule><sort by='ascending'>@foo</sort></rule>")) .root() .query() .single("*") .node()) { @Override void resolveOrder(Builder aModBuilder, Node node) throws TransformException { mockBlock.resolveOrder(aModBuilder, node); } @Override public Seg createSeg(Mod unused) { fail(); return null; } }; modBuilder = mockery.mock(Mod.Builder.class); mockery.checking( new Expectations() { { one(modBuilder).dependOn(m1.document()); one(modBuilder).dependOn(m2.document()); Sequence seq1 = mockery.sequence("pre-commit resolveOrder 1"); Sequence seq2 = mockery.sequence("pre-commit resolveOrder 2"); one(mockBlock).resolveOrder(modBuilder, m1); inSequence(seq1); one(mockBlock).resolveOrder(modBuilder, m2); inSequence(seq2); modBuilderPriors.add(seq1); modBuilderPriors.add(seq2); } }); block.requiredVariables = Arrays.asList(new QName[] {new QName(null, "a", null), new QName(null, "b", null)}); dependOnNearest( NodeTarget.class, false, new NodeTarget() { public ItemList targets() throws TransformException { return content.query().all("/id('m1 m2')"); } }); order("m1"); order("m2"); dependOnVariables(new QName(null, "a", null), new QName(null, "b", null)); thenCommit(); block.resolve(modBuilder); }
@Test public void testObjectDriverType() throws Exception { MemoryDataSetDriver driver = new MemoryDataSetDriver( new String[] {"pk", "geom"}, new Type[] {TypeFactory.createType(Type.INT), TypeFactory.createType(Type.GEOMETRY)}); sm.register("spatial", driver); Source src = sm.getSource("spatial"); assertEquals((src.getType() & SourceManager.MEMORY), SourceManager.MEMORY); assertEquals((src.getType() & SourceManager.VECTORIAL), SourceManager.VECTORIAL); driver = new MemoryDataSetDriver(new String[] {"pk"}, new Type[] {TypeFactory.createType(Type.INT)}); sm.register("alpha", driver); src = sm.getSource("alpha"); assertEquals((src.getType() & SourceManager.MEMORY), SourceManager.MEMORY); assertEquals((src.getType() & SourceManager.VECTORIAL), 0); }
private String associateFile(Source source, String propertyName) throws Exception { if (source.hasProperty(propertyName)) { source.deleteProperty(propertyName); } File stats = source.createFileProperty(propertyName); DataSource ds = dsf.getDataSource(source.getName()); ds.open(); long rc = ds.getRowCount(); ds.close(); FileOutputStream fis = new FileOutputStream(stats); String rcStr = Long.toString(rc); fis.write(rcStr.getBytes()); fis.close(); return rcStr; }
public void mustSuitablyOverrideAttributeHandlingMethods() { @SuppressWarnings("unused") final Source<Integer, NotUsed> f = Source.single(42) .withAttributes(Attributes.name("")) .addAttributes(Attributes.asyncBoundary()) .named(""); }
@Test public void mustBeAbleToUseToFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable<String> input = Arrays.asList("A", "B", "C"); CompletionStage<String> future = Source.from(input).runWith(Sink.<String>head(), materializer); String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("A", result); }
@Test public void mustRepeat() throws Exception { final CompletionStage<List<Integer>> f = Source.repeat(42).grouped(10000).runWith(Sink.<List<Integer>>head(), materializer); final List<Integer> result = f.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(result.size(), 10000); for (Integer i : result) assertEquals(i, (Integer) 42); }
@Ignore("StatefulStage to be converted to GraphStage when Java Api is available (#18817)") @Test public void mustBeAbleToUseTransform() { final JavaTestKit probe = new JavaTestKit(system); final Iterable<Integer> input = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7); // duplicate each element, stop after 4 elements, and emit sum to the end Source.from(input) .transform( new Creator<Stage<Integer, Integer>>() { @Override public PushPullStage<Integer, Integer> create() throws Exception { return new StatefulStage<Integer, Integer>() { int sum = 0; int count = 0; @Override public StageState<Integer, Integer> initial() { return new StageState<Integer, Integer>() { @Override public SyncDirective onPush(Integer element, Context<Integer> ctx) { sum += element; count += 1; if (count == 4) { return emitAndFinish( Arrays.asList(element, element, sum).iterator(), ctx); } else { return emit(Arrays.asList(element, element).iterator(), ctx); } } }; } @Override public TerminationDirective onUpstreamFinish(Context<Integer> ctx) { return terminationEmit(Collections.singletonList(sum).iterator(), ctx); } }; } }) .runForeach( new Procedure<Integer>() { public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals(0); probe.expectMsgEquals(0); probe.expectMsgEquals(1); probe.expectMsgEquals(1); probe.expectMsgEquals(2); probe.expectMsgEquals(2); probe.expectMsgEquals(3); probe.expectMsgEquals(3); probe.expectMsgEquals(6); }
@Test public void mustWorkFromRange() throws Exception { CompletionStage<List<Integer>> f = Source.range(0, 10).grouped(20).runWith(Sink.<List<Integer>>head(), materializer); final List<Integer> result = f.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals(11, result.size()); Integer counter = 0; for (Integer i : result) assertEquals(i, counter++); }
@Test public void writeToOutputStream() throws IOException { XMLDocument doc = db.createFolder("/top").documents().load(Name.create(db, "foo"), Source.xml("<root/>")); ByteArrayOutputStream out = new ByteArrayOutputStream(); doc.write(out); out.close(); assertEquals("<root/>", out.toString()); }
@Test public void mustBeAbleToUseMerge2() { final JavaTestKit probe = new JavaTestKit(system); final Iterable<String> input1 = Arrays.asList("A", "B", "C"); final Iterable<String> input2 = Arrays.asList("D", "E", "F"); Source.from(input1) .merge(Source.from(input2)) .runForeach( new Procedure<String>() { public void apply(String elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgAllOf("A", "B", "C", "D", "E", "F"); }
@Test public void mustBeAbleToUseActorRefSource() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source<Integer, ActorRef> actorRefSource = Source.actorRef(10, OverflowStrategy.fail()); final ActorRef ref = actorRefSource .to( Sink.foreach( new Procedure<Integer>() { public void apply(Integer elem) { probe.getRef().tell(elem, ActorRef.noSender()); } })) .run(materializer); ref.tell(1, ActorRef.noSender()); probe.expectMsgEquals(1); ref.tell(2, ActorRef.noSender()); probe.expectMsgEquals(2); }
private String associateString(Source source, String propertyName) throws Exception { DataSource ds = dsf.getDataSource(SOURCE); ds.open(); long rc = ds.getRowCount(); ds.close(); String rcStr = Long.toString(rc); source.putProperty(propertyName, rcStr); return rcStr; }
@Test public void testAssociateStringProperty() throws Exception { sm.register(SOURCE, testFile); Source source = sm.getSource(SOURCE); String statistics = "statistics"; String rcStr = associateString(source, statistics); assertEquals(sm.getSource(SOURCE).getStringPropertyNames().length, 1); sm.saveStatus(); sm.shutdown(); sm.init(); assertEquals(sm.getSource(SOURCE).getStringPropertyNames().length, 1); String statsContent = source.getProperty(statistics); assertEquals(statsContent, rcStr); }
@Test public void mustBeAbleToUseConflate() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final List<String> input = Arrays.asList("A", "B", "C"); CompletionStage<String> future = Source.from(input) .conflateWithSeed(s -> s, (aggr, in) -> aggr + in) .runFold("", (aggr, in) -> aggr + in, materializer); String result = future.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("ABC", result); final Flow<String, String, NotUsed> flow2 = Flow.of(String.class).conflate((a, b) -> a + b); CompletionStage<String> future2 = Source.from(input) .conflate((String a, String b) -> a + b) .runFold("", (a, b) -> a + b, materializer); String result2 = future2.toCompletableFuture().get(3, TimeUnit.SECONDS); assertEquals("ABC", result2); }
@Test public void mustBeAbleToUseMapFuture() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable<String> input = Arrays.asList("a", "b", "c"); Source.from(input) .mapAsync(4, elem -> CompletableFuture.completedFuture(elem.toUpperCase())) .runForeach(elem -> probe.getRef().tell(elem, ActorRef.noSender()), materializer); probe.expectMsgEquals("A"); probe.expectMsgEquals("B"); probe.expectMsgEquals("C"); }
@Test public void mustBeAbleToUseDropWhile() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Source<Integer, NotUsed> source = Source.from(Arrays.asList(0, 1, 2, 3)) .dropWhile( new Predicate<Integer>() { public boolean test(Integer elem) { return elem < 2; } }); final CompletionStage<Done> future = source.runWith( Sink.foreach(elem -> probe.getRef().tell(elem, ActorRef.noSender())), materializer); probe.expectMsgEquals(2); probe.expectMsgEquals(3); future.toCompletableFuture().get(200, TimeUnit.MILLISECONDS); }
@Test public void mustBeAbleToUseZip() throws Exception { final JavaTestKit probe = new JavaTestKit(system); final Iterable<String> input1 = Arrays.asList("A", "B", "C"); final Iterable<String> input2 = Arrays.asList("D", "E", "F"); Source.from(input1) .zip(Source.from(input2)) .runForeach( new Procedure<Pair<String, String>>() { public void apply(Pair<String, String> elem) { probe.getRef().tell(elem, ActorRef.noSender()); } }, materializer); probe.expectMsgEquals(new Pair<String, String>("A", "D")); probe.expectMsgEquals(new Pair<String, String>("B", "E")); probe.expectMsgEquals(new Pair<String, String>("C", "F")); }
@Test public void mustBeAbleToUseThrottle() throws Exception { Integer result = Source.from(Arrays.asList(0, 1, 2)) .throttle(10, FiniteDuration.create(1, TimeUnit.SECONDS), 10, ThrottleMode.shaping()) .throttle(10, FiniteDuration.create(1, TimeUnit.SECONDS), 10, ThrottleMode.enforcing()) .runWith(Sink.head(), materializer) .toCompletableFuture() .get(3, TimeUnit.SECONDS); assertEquals((Object) 0, result); }