< prev index next >

test/jdk/java/net/httpclient/websocket/Support.java

Print this page


   1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  62 
  63     public static void assertDoesNotCompleteWithin(long timeout,
  64                                                    TimeUnit unit,
  65                                                    CompletionStage<?> stage) {
  66         CompletableFuture<?> cf =
  67                 CompletableFuture.completedFuture(null).thenCompose(x -> stage);
  68         assertThrows(TimeoutException.class, () -> cf.get(timeout, unit));
  69     }
  70 
  71     public static ByteBuffer fullCopy(ByteBuffer src) {
  72         ByteBuffer copy = ByteBuffer.allocate(src.capacity());
  73         int p = src.position();
  74         int l = src.limit();
  75         src.clear();
  76         copy.put(src).position(p).limit(l);
  77         src.position(p).limit(l);
  78         return copy;
  79     }
  80 
  81     public static DummyWebSocketServer serverWithCannedData(int... data) {








  82         byte[] copy = new byte[data.length];
  83         for (int i = 0; i < data.length; i++) {
  84             copy[i] = (byte) data[i];
  85         }
  86         return serverWithCannedData(copy);
  87     }
  88 
  89     public static DummyWebSocketServer serverWithCannedData(byte... data) {








  90         byte[] copy = Arrays.copyOf(data, data.length);
  91         return new DummyWebSocketServer() {
  92             @Override
  93             protected void write(SocketChannel ch) throws IOException {
  94                 int off = 0; int n = 1; // 1 byte at a time
  95                 while (off + n < copy.length + n) {
  96 //                    try {
  97 //                        TimeUnit.MICROSECONDS.sleep(500);
  98 //                    } catch (InterruptedException e) {
  99 //                        return;
 100 //                    }
 101                     int len = Math.min(copy.length - off, n);
 102                     ByteBuffer bytes = ByteBuffer.wrap(copy, off, len);
 103                     off += len;
 104                     ch.write(bytes);
 105                 }
 106                 super.write(ch);
 107             }
 108         };
 109     }
 110 
 111     /*


   1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  62 
  63     public static void assertDoesNotCompleteWithin(long timeout,
  64                                                    TimeUnit unit,
  65                                                    CompletionStage<?> stage) {
  66         CompletableFuture<?> cf =
  67                 CompletableFuture.completedFuture(null).thenCompose(x -> stage);
  68         assertThrows(TimeoutException.class, () -> cf.get(timeout, unit));
  69     }
  70 
  71     public static ByteBuffer fullCopy(ByteBuffer src) {
  72         ByteBuffer copy = ByteBuffer.allocate(src.capacity());
  73         int p = src.position();
  74         int l = src.limit();
  75         src.clear();
  76         copy.put(src).position(p).limit(l);
  77         src.position(p).limit(l);
  78         return copy;
  79     }
  80 
  81     public static DummyWebSocketServer serverWithCannedData(int... data) {
  82         return serverWithCannedDataAndAuthentication(null, null, data);
  83     }
  84 
  85     public static DummyWebSocketServer serverWithCannedDataAndAuthentication(
  86             String username,
  87             String password,
  88             int... data)
  89     {
  90         byte[] copy = new byte[data.length];
  91         for (int i = 0; i < data.length; i++) {
  92             copy[i] = (byte) data[i];
  93         }
  94         return serverWithCannedDataAndAuthentication(username, password, copy);
  95     }
  96 
  97     public static DummyWebSocketServer serverWithCannedData(byte... data) {
  98        return serverWithCannedDataAndAuthentication(null, null, data);
  99     }
 100 
 101     public static DummyWebSocketServer serverWithCannedDataAndAuthentication(
 102             String username,
 103             String password,
 104             byte... data)
 105     {
 106         byte[] copy = Arrays.copyOf(data, data.length);
 107         return new DummyWebSocketServer(username, password) {
 108             @Override
 109             protected void write(SocketChannel ch) throws IOException {
 110                 int off = 0; int n = 1; // 1 byte at a time
 111                 while (off + n < copy.length + n) {
 112 //                    try {
 113 //                        TimeUnit.MICROSECONDS.sleep(500);
 114 //                    } catch (InterruptedException e) {
 115 //                        return;
 116 //                    }
 117                     int len = Math.min(copy.length - off, n);
 118                     ByteBuffer bytes = ByteBuffer.wrap(copy, off, len);
 119                     off += len;
 120                     ch.write(bytes);
 121                 }
 122                 super.write(ch);
 123             }
 124         };
 125     }
 126 
 127     /*


< prev index next >