J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Tests that a thread blocked in DatagramSocket.receive |
| 26 | * throws a SocketException if the socket is asynchronously closed. |
| 27 | */ |
| 28 | import java.net.*; |
| 29 | |
| 30 | public class DatagramSocket_receive extends AsyncCloseTest implements Runnable { |
| 31 | DatagramSocket s; |
| 32 | int timeout = 0; |
| 33 | |
| 34 | public DatagramSocket_receive() { |
| 35 | } |
| 36 | |
| 37 | public DatagramSocket_receive(int timeout) { |
| 38 | this.timeout = timeout; |
| 39 | } |
| 40 | |
| 41 | public String description() { |
| 42 | String s = "DatagramSocket.receive"; |
| 43 | if (timeout > 0) { |
| 44 | s += " (timeout specified)"; |
| 45 | } |
| 46 | return s; |
| 47 | } |
| 48 | |
| 49 | public void run() { |
| 50 | DatagramPacket p; |
| 51 | try { |
| 52 | |
| 53 | byte b[] = new byte[1024]; |
| 54 | p = new DatagramPacket(b, b.length); |
| 55 | |
| 56 | if (timeout > 0) { |
| 57 | s.setSoTimeout(timeout); |
| 58 | } |
| 59 | } catch (Exception e) { |
| 60 | failed(e.getMessage()); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | try { |
| 65 | s.receive(p); |
| 66 | } catch (SocketException se) { |
| 67 | closed(); |
| 68 | } catch (Exception e) { |
| 69 | failed(e.getMessage()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public boolean go() throws Exception { |
| 74 | s = new DatagramSocket(); |
| 75 | |
| 76 | Thread thr = new Thread(this); |
| 77 | thr.start(); |
| 78 | |
| 79 | Thread.currentThread().sleep(1000); |
| 80 | |
| 81 | s.close(); |
| 82 | |
| 83 | Thread.currentThread().sleep(1000); |
| 84 | |
| 85 | if (isClosed()) { |
| 86 | return true; |
| 87 | } else { |
| 88 | failed("DatagramSocket.receive wasn't preempted"); |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | } |