File: Util.java

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 158,500 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (109 lines) | stat: -rw-r--r-- 3,410 bytes parent folder | download | duplicates (8)
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
100
101
102
103
104
105
106
107
108
109
/*-
 * See the file LICENSE for redistribution information.
 * 
 * Copyright (c) 2010, 2013 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package repmgrtests;

import java.io.File;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.FileOutputStream;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * Miscellaneous utilities used by repmgr tests.
 */
public class Util {
    public static Process startFiddler(PortsConfig p, String testName, int mgrPort)
        throws Exception
    {
        ProcessBuilder pb =
            new ProcessBuilder("erl", "-noshell",
                               "-s", "fiddler1", "startn",
                               Integer.toString(mgrPort),
                               p.getFiddlerConfig());
        pb.directory(new File("fiddler")).redirectErrorStream(true);
        Process fiddler = pb.start();

        String fileName = testName + ".fiddler.out";
        final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
        final BufferedInputStream bis = new BufferedInputStream(fiddler.getInputStream());

        // Spawn a thread to capture the fiddler's output and save it
        // into a file.
        // 
        Runnable copier = new Runnable() {
                public void run() {
                    try {
                        byte[] buf = new byte[1000];
                        int len;

                        for (;;) {
                            len = bis.read(buf, 0, buf.length);
                            if (len == -1) { break; }
                            bos.write(buf, 0, len);
                        }
                        bos.close();
                    } catch (IOException x) {
                        x.printStackTrace();
                    }
                }
            };
        Thread t = new Thread(copier);
        t.start();

        // wait til fiddler is ready, which we'll infer when it is
        // listening on its mgmt port (Hmm, is that really late
        // enough?  Well, it's certainly better'n nuttin'.)
        // 
        Socket s = null;
        long deadline = System.currentTimeMillis() + 5000; // allow 5 seconds, max
        do {
            try {
                s = new Socket("localhost", mgrPort);
            } catch (IOException x) {
                if (System.currentTimeMillis() > deadline) { throw x; }
                Thread.sleep(200);
            }
        } while (s == null);
        s.close();

        return fiddler;
    }

    public static File mkdir(String dname) {
        File d = new File("TESTDIR");
        d.mkdir();
        File f = new File(d, dname);
        rm_rf(f);
        f.mkdir();
        return f;
    }
    
    public static void rm_rf(File f) {
        if (f.isDirectory())
            for (File f2 : f.listFiles())
                rm_rf(f2);
        f.delete();
    }
    
    public static int[] findAvailablePorts(int n) throws IOException {
        int ports[] = new int[n];
        ServerSocket[] sockets = new ServerSocket[n];
        for (int i=0; i<n; i++) {
            ServerSocket s = new ServerSocket(0);
            s.setReuseAddress(true);
            ports[i] = s.getLocalPort();
            sockets[i] = s;
        }
        for (int i=0; i<n; i++)
            sockets[i].close();
        return ports;
    }
}