blob: 52cbf56474a4b1b648548094d5cd3298a7eb0871 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 2003 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 * @test
26 * @bug 4796166
27 * @summary Linger interval delays usage of released file descriptor
28 */
29
30import java.net.*;
31import java.io.*;
32
33public class LingerTest {
34
35 static class Sender implements Runnable {
36 Socket s;
37
38 public Sender(Socket s) {
39 this.s = s;
40 }
41
42 public void run() {
43 System.out.println ("Sender starts");
44 try {
45 s.getOutputStream().write(new byte[128*1024]);
46 }
47 catch (IOException ioe) {
48 }
49 System.out.println ("Sender ends");
50 }
51 }
52
53 static class Closer implements Runnable {
54 Socket s;
55
56 public Closer(Socket s) {
57 this.s = s;
58 }
59
60 public void run() {
61 System.out.println ("Closer starts");
62 try {
63 s.close();
64 }
65 catch (IOException ioe) {
66 }
67 System.out.println ("Closer ends");
68 }
69 }
70
71 static class Another implements Runnable {
72 int port;
73 long delay;
74 boolean connected = false;
75
76 public Another(int port, long delay) {
77 this.port = port;
78 this.delay = delay;
79 }
80
81 public void run() {
82 System.out.println ("Another starts");
83 try {
84 Thread.currentThread().sleep(delay);
85 Socket s = new Socket("localhost", port);
86 synchronized (this) {
87 connected = true;
88 }
89 s.close();
90 }
91 catch (Exception ioe) {
92 ioe.printStackTrace();
93 }
94 System.out.println ("Another ends");
95 }
96
97 public synchronized boolean connected() {
98 return connected;
99 }
100 }
101
102 public static void main(String args[]) throws Exception {
103 ServerSocket ss = new ServerSocket(0);
104
105 Socket s1 = new Socket("localhost", ss.getLocalPort());
106 Socket s2 = ss.accept();
107
108
109 // setup conditions for untransmitted data and lengthy
110 // linger interval
111 s1.setSendBufferSize(128*1024);
112 s1.setSoLinger(true, 30);
113 s2.setReceiveBufferSize(1*1024);
114
115 // start sender
116 Thread thr = new Thread(new Sender(s1));
117 thr.start();
118
119 // another thread that will connect after 5 seconds.
120 Another another = new Another(ss.getLocalPort(), 5000);
121 thr = new Thread(another);
122 thr.start();
123
124 // give sender time to queue the data
125 Thread.currentThread().sleep(1000);
126
127 // close the socket asynchronously
128 (new Thread(new Closer(s1))).start();
129
130 // give another time to run
131 Thread.currentThread().sleep(10000);
132
133 // check that another is done
134 if (!another.connected()) {
135 throw new RuntimeException("Another thread is blocked");
136 }
137 System.out.println ("Main ends");
138
139 }
140
141}