aboutsummaryrefslogtreecommitdiffstats
path: root/jrt_test/src/tests/garbage/Garbage.java
blob: 77a1d8bfd7d134bf3bddf6ec17569a0cde0a8338 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import com.yahoo.jrt.*;
import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;

public class Garbage implements FatalErrorHandler {

    boolean error = false;
    int     port  = 0;

    public void handleFailure(Throwable t, Object o) {
	System.err.println("FATAL ERROR -> " + o);
	t.printStackTrace();
	error = true;
    }

    class Closer implements Runnable {
        private boolean done  = false;
        private Socket socket = null;

        public Closer(Socket s) {
            socket = s;
        }

        public void done() {
            synchronized (this) {
                done = true;
                notify();
            }
        }

        public void run() {
            long t = System.currentTimeMillis();
            long end = t + 60000;
            synchronized (this) {
                while (!done) {
                    if (t >= end) {
                        error = true;
                        try { socket.close(); } catch (IOException e) {
                            System.err.println("AAARGH!!");
                            System.exit(1);
                        }
                        return;
                    }
                    try { wait(end - t); } catch (InterruptedException ignore) {}
                    t = System.currentTimeMillis();
                }
            }
        }
    }

    public void myMain() {
	Supervisor server = new Supervisor(new Transport(this));
	try {
	    port = server.listen(new Spec(0)).port(); // random port
	} catch (ListenFailedException e) {
	    System.err.println("Listen failed");
	    System.exit(1);
	}

        try {
            Socket s = new Socket("localhost", port);
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            byte[] data = {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
                           (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
                           (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff};
            out.write(data);
            out.flush();
            Closer closer = new Closer(s);
            new Thread(closer).start();
            try {
                if (in.read() != -1) {
                    error = true;
                }
            } finally {
                closer.done();
            }
        } catch (IOException e) {
	    System.err.println(e);
            error = true;
        }

	server.transport().shutdown().join();

	if (error) {
	    System.err.println("TEST FAILED!");
	    System.exit(1);
	} else {
	    System.err.println("test passed");
        }
    }

    public static void main(String[] args) {
	new Garbage().myMain();
    }
}