test/jdk/java/net/httpclient/websocket/MockListener.java
branchhttp-client-branch
changeset 56688 0b633bdb7010
parent 56451 9585061fdb04
child 56795 03ece2518428
equal deleted inserted replaced
56682:9822bbe48b9b 56688:0b633bdb7010
    37     private final List<Invocation> invocations = new ArrayList<>();
    37     private final List<Invocation> invocations = new ArrayList<>();
    38     private final CompletableFuture<?> lastCall = new CompletableFuture<>();
    38     private final CompletableFuture<?> lastCall = new CompletableFuture<>();
    39     private final Predicate<? super Invocation> collectUntil;
    39     private final Predicate<? super Invocation> collectUntil;
    40 
    40 
    41     public MockListener() {
    41     public MockListener() {
    42         this(i -> i instanceof OnClose || i instanceof OnError);
    42         this(1, MockListener::closeOrError);
       
    43     }
       
    44 
       
    45     public MockListener(long bufferSize) {
       
    46         this(bufferSize, MockListener::closeOrError);
    43     }
    47     }
    44 
    48 
    45     public MockListener(Predicate<? super Invocation> collectUntil) {
    49     public MockListener(Predicate<? super Invocation> collectUntil) {
    46         this(2, collectUntil);
    50         this(1, collectUntil);
    47     }
    51     }
    48 
    52 
    49     /*
    53     /*
    50      * Typical buffer sizes: 1, n, Long.MAX_VALUE
    54      * Typical buffer sizes: 1, n, Long.MAX_VALUE
    51      */
    55      */
    57         Objects.requireNonNull(collectUntil);
    61         Objects.requireNonNull(collectUntil);
    58         this.bufferSize = bufferSize;
    62         this.bufferSize = bufferSize;
    59         this.collectUntil = collectUntil;
    63         this.collectUntil = collectUntil;
    60     }
    64     }
    61 
    65 
       
    66     private static boolean closeOrError(Invocation i) {
       
    67         return i instanceof OnClose || i instanceof OnError;
       
    68     }
       
    69 
    62     @Override
    70     @Override
    63     public void onOpen(WebSocket webSocket) {
    71     public void onOpen(WebSocket webSocket) {
    64         System.out.printf("onOpen(%s)%n", webSocket);
    72         System.out.printf("onOpen(%s)%n", webSocket);
    65         OnOpen inv = new OnOpen(webSocket);
    73         OnOpen inv = new OnOpen(webSocket);
    66         synchronized (invocations) {
    74         synchronized (invocations) {
    71         }
    79         }
    72         onOpen0(webSocket);
    80         onOpen0(webSocket);
    73     }
    81     }
    74 
    82 
    75     protected void onOpen0(WebSocket webSocket) {
    83     protected void onOpen0(WebSocket webSocket) {
    76         replenish(webSocket);
    84         count = bufferSize - bufferSize / 2;
       
    85         System.out.printf("request(%d)%n", bufferSize);
       
    86         webSocket.request(bufferSize);
    77     }
    87     }
    78 
    88 
    79     @Override
    89     @Override
    80     public CompletionStage<?> onText(WebSocket webSocket,
    90     public CompletionStage<?> onText(WebSocket webSocket,
    81                                      CharSequence message,
    91                                      CharSequence message,
   207     }
   217     }
   208 
   218 
   209     protected void replenish(WebSocket webSocket) {
   219     protected void replenish(WebSocket webSocket) {
   210         if (--count <= 0) {
   220         if (--count <= 0) {
   211             count = bufferSize - bufferSize / 2;
   221             count = bufferSize - bufferSize / 2;
   212         }
   222             webSocket.request(count);
   213         webSocket.request(count);
   223             System.out.printf("request(%d)%n", count);
       
   224         }
   214     }
   225     }
   215 
   226 
   216     public abstract static class Invocation {
   227     public abstract static class Invocation {
   217 
   228 
   218         public static OnOpen onOpen(WebSocket webSocket) {
   229         public static OnOpen onOpen(WebSocket webSocket) {