SimpleFeeder implements ReplyHandler { @Override public void handleReply(Reply reply) { if (failure.get() != null) { return; } if (reply.hasErrors()) { failure.compareAndSet(null, new IOException(formatErrors(reply))); return; } long now = System.currentTimeMillis(); long latency = now - (long) reply.getContext(); numReplies.incrementAndGet(); accumulateReplies(now, latency); } SimpleFeeder(FeederParams params); static void main(String[] args); @Override void handleReply(Reply reply);  }
@Test public void requireThatXMLFeederWorks() throws Throwable { assertFeed("<vespafeed>" + " <document documenttype='simple' documentid='id:scheme:simple::0'>" + " <my_str>foo</my_str>" + " </document>" + " <update documenttype='simple' documentid='id:scheme:simple::1'>" + " <assign field='my_str'>bar</assign>" + " </update>" + " <remove documenttype='simple' documentid='id:scheme:simple::2'/>" + "</vespafeed>", new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*3,.+\n"); }
@Test public void requireThatXML2JsonFeederWorks() throws Throwable { ByteArrayOutputStream dump = new ByteArrayOutputStream(); assertFeed(new FeederParams().setDumpStream(dump), "<vespafeed>" + " <document documenttype='simple' documentid='id:simple:simple::0'>" + " <my_str>foo</my_str>" + " </document>" + " <update documenttype='simple' documentid='id:simple:simple::1'>" + " <assign field='my_str'>bar</assign>" + " </update>" + " <remove documenttype='simple' documentid='id:simple:simple::2'/>" + "</vespafeed>", new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*3,.+\n"); assertEquals(58, dump.size()); assertEquals("[\n{\"id\":\"id:simple:simple::0\",\"fields\":{\"my_str\":\"foo\"}}\n]", dump.toString()); }
@Test public void requireThatDualPutXML2JsonFeederWorks() throws Throwable { ByteArrayOutputStream dump = new ByteArrayOutputStream(); assertFeed(new FeederParams().setDumpStream(dump), "<vespafeed>" + " <document documenttype='simple' documentid='id:simple:simple::0'>" + " <my_str>foo</my_str>" + " </document>" + " <document documenttype='simple' documentid='id:simple:simple::1'>" + " <my_str>bar</my_str>" + " </document>" + " <remove documenttype='simple' documentid='id:simple:simple::2'/>" + "</vespafeed>", new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*3,.+\n"); assertEquals(115, dump.size()); assertEquals("[\n{\"id\":\"id:simple:simple::0\",\"fields\":{\"my_str\":\"foo\"}},\n {\"id\":\"id:simple:simple::1\",\"fields\":{\"my_str\":\"bar\"}}\n]", dump.toString()); assertFeed(dump.toString(), new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*2,.+\n"); }
@Test public void requireThatJson2VespaFeederWorks() throws Throwable { ByteArrayOutputStream dump = new ByteArrayOutputStream(); assertFeed(new FeederParams().setDumpStream(dump).setDumpFormat(FeederParams.DumpFormat.VESPA), "[" + " { \"put\": \"id:simple:simple::0\", \"fields\": { \"my_str\":\"foo\"}}," + " { \"update\": \"id:simple:simple::1\", \"fields\": { \"my_str\": { \"assign\":\"bar\"}}}," + " { \"remove\": \"id:simple:simple::2\", \"condition\":\"true\"}" + "]", new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*3,.+\n"); assertEquals(187, dump.size()); assertFeed(new ByteArrayInputStream(dump.toByteArray()), new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*3,.+\n"); }
@Test public void requireThatJsonFeederWorks() throws Throwable { assertFeed("[" + " { \"put\": \"id:simple:simple::0\", \"fields\": { \"my_str\":\"foo\"}}," + " { \"update\": \"id:simple:simple::1\", \"fields\": { \"my_str\": { \"assign\":\"bar\"}}}," + " { \"remove\": \"id:simple:simple::2\"}" + "]", new MessageHandler() { @Override public void handleMessage(Message msg) { Reply reply = ((DocumentMessage)msg).createReply(); reply.swapState(msg); reply.popHandler().handleReply(reply); } }, "", "(.+\n)+" + "\\s*\\d+,\\s*3,.+\n"); }