aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/container/handlers/ThrowingInWriteRequestHandler.java
blob: 75ecd11dfa8e59164e0740d740fc13ec77e1601f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.container.handlers;

import com.yahoo.jdisc.handler.AbstractRequestHandler;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author Einar M R Rosenvinge
*/
public class ThrowingInWriteRequestHandler extends AbstractRequestHandler {

    private ExecutorService responseExecutor = Executors.newSingleThreadExecutor();

    @Override
    public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) {
        responseExecutor.execute(new ThrowingInWriteTask(handler));
        return new ThrowingInWriteContentChannel();
    }


    private static class ThrowingInWriteTask implements Runnable {
        private final ResponseHandler handler;

        public ThrowingInWriteTask(ResponseHandler handler) {
            this.handler = handler;
        }

        @Override
        public void run() {
            ContentChannel responseChannel = handler.handleResponse(
                    new com.yahoo.jdisc.Response(com.yahoo.jdisc.Response.Status.OK));
            responseChannel.close(null);
        }
    }


    private static class ThrowingInWriteContentChannel implements ContentChannel {
        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            throw new WriteException();
        }

        @Override
        public void close(CompletionHandler handler) {
            handler.completed();
        }
    }

}