FeedHandlerV3 extends LoggingRequestHandler { @Override public HttpResponse handle(HttpRequest request) { String clientId = clientId(request); ClientFeederV3 clientFeederV3; synchronized (monitor) { if (! clientFeederByClientId.containsKey(clientId)) { SourceSessionParams sourceSessionParams = sourceSessionParams(request); clientFeederByClientId.put(clientId, new ClientFeederV3(retainSource(sessionCache, sourceSessionParams), new FeedReaderFactory(true), docTypeManager, clientId, metric, feedReplyHandler)); } clientFeederV3 = clientFeederByClientId.get(clientId); } try { return clientFeederV3.handleRequest(request); } catch (UnknownClientException uce) { String msg = Exceptions.toMessageString(uce); log.log(Level.WARNING, msg); return new ErrorHttpResponse(com.yahoo.jdisc.http.HttpResponse.Status.BAD_REQUEST, msg); } catch (Exception e) { String msg = "Could not initialize document parsing: " + Exceptions.toMessageString(e); log.log(Level.WARNING, msg); return new ErrorHttpResponse(com.yahoo.jdisc.http.HttpResponse.Status.INTERNAL_SERVER_ERROR, msg); } } FeedHandlerV3(Executor executor,
                         Metric metric,
                         AccessLog accessLog,
                         DocumentmanagerConfig documentManagerConfig,
                         SessionCache sessionCache,
                         DocumentApiMetrics metricsHelper); void injectDocumentManangerForTests(DocumentTypeManager docTypeManager); @Override HttpResponse handle(HttpRequest request);  }
@Test public void feedOneDocument() throws Exception { final FeedHandlerV3 feedHandlerV3 = setupFeederHandler(simpleThreadpool); HttpResponse httpResponse = feedHandlerV3.handle(createRequest(1)); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); httpResponse.render(outStream); assertThat(httpResponse.getContentType(), is("text/plain")); assertThat(Utf8.toString(outStream.toByteArray()), is("1230 OK message trace\n")); }
@Test public void feedOneBrokenDocument() throws Exception { final FeedHandlerV3 feedHandlerV3 = setupFeederHandler(simpleThreadpool); HttpResponse httpResponse = feedHandlerV3.handle(createBrokenRequest()); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); httpResponse.render(outStream); assertThat(httpResponse.getContentType(), is("text/plain")); assertThat(Utf8.toString(outStream.toByteArray()), startsWith("1230 ERROR ")); assertThat(metric.get(MetricNames.PARSE_ERROR), is(1L)); }
@Test public void feedManyDocument() throws Exception { final FeedHandlerV3 feedHandlerV3 = setupFeederHandler(simpleThreadpool); HttpResponse httpResponse = feedHandlerV3.handle(createRequest(100)); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); httpResponse.render(outStream); assertThat(httpResponse.getContentType(), is("text/plain")); String result = Utf8.toString(outStream.toByteArray()); assertThat(Splitter.on("\n").splitToList(result).size(), is(101)); }