Paul Martin
2014-09-29 f9c78c0ccc709509cdf7f83c45c898883d329db2
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright 2013 akquinet tech@spree GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.akquinet.devops;
 
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
 
import junit.framework.Assert;
 
import org.junit.Test;
 
import com.gitblit.Constants;
 
/**
 * This test checks if it is possible to run two server instances in the same
 * JVM sequentially
 * 
 * @author saheba
 * 
 */
public class LaunchWithUITestConfig {
 
    @Test
    public void testSequentialLaunchOfSeveralInstances()
            throws InterruptedException {
        // different ports than in testParallelLaunchOfSeveralInstances to
        // ensure that both test cases do not affect each others test results
        int httpPort = 9191, httpsPort = 9292, shutdownPort = 9393;
        String gitblitPropertiesPath = "src/test/config/test-ui-gitblit.properties",
                usersPropertiesPath = "src/test/config/test-ui-users.conf";
 
        GitblitRunnable gitblitRunnable = new GitblitRunnable(httpPort,
                httpsPort, shutdownPort, gitblitPropertiesPath,
                usersPropertiesPath);
        Thread serverThread = new Thread(gitblitRunnable);
        serverThread.start();
        Thread.sleep(2000);
        Assert.assertFalse(gitblitRunnable.isStartFailed());
        LaunchWithUITestConfig.shutdownGitBlitServer(shutdownPort);
 
        Thread.sleep(5000);
 
        GitblitRunnable gitblitRunnable2 = new GitblitRunnable(httpPort,
                httpsPort, shutdownPort, gitblitPropertiesPath,
                usersPropertiesPath);
        Thread serverThread2 = new Thread(gitblitRunnable2);
        serverThread2.start();
        Thread.sleep(2000);
        Assert.assertFalse(gitblitRunnable2.isStartFailed());
        LaunchWithUITestConfig.shutdownGitBlitServer(shutdownPort);
    }
 
    @Test
    public void testParallelLaunchOfSeveralInstances()
            throws InterruptedException {
        // different ports than in testSequentialLaunchOfSeveralInstances to
        // ensure that both test cases do not affect each others test results
        int httpPort = 9797, httpsPort = 9898, shutdownPort = 9999;
        int httpPort2 = 9494, httpsPort2 = 9595, shutdownPort2 = 9696;
        String gitblitPropertiesPath = "src/test/config/test-ui-gitblit.properties",
                usersPropertiesPath = "src/test/config/test-ui-users.conf";
 
        GitblitRunnable gitblitRunnable = new GitblitRunnable(httpPort,
                httpsPort, shutdownPort, gitblitPropertiesPath,
                usersPropertiesPath);
        Thread serverThread = new Thread(gitblitRunnable);
        serverThread.start();
        Thread.sleep(2000);
        Assert.assertFalse(gitblitRunnable.isStartFailed());
 
        GitblitRunnable gitblitRunnable2 = new GitblitRunnable(httpPort2,
                httpsPort2, shutdownPort2, gitblitPropertiesPath,
                usersPropertiesPath);
        Thread serverThread2 = new Thread(gitblitRunnable2);
        serverThread2.start();
        Thread.sleep(2000);
        Assert.assertFalse(gitblitRunnable2.isStartFailed());
 
        LaunchWithUITestConfig.shutdownGitBlitServer(shutdownPort);
        LaunchWithUITestConfig.shutdownGitBlitServer(shutdownPort2);
    }
 
    /**
     * main runs the tests without assert checks. You have to check the console
     * output manually.
     * 
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        new LaunchWithUITestConfig().testSequentialLaunchOfSeveralInstances();
        new LaunchWithUITestConfig().testParallelLaunchOfSeveralInstances();
    }
 
    private static void shutdownGitBlitServer(int shutdownPort) {
        try {
            Socket s = new Socket(InetAddress.getByName("127.0.0.1"),
                    shutdownPort);
            OutputStream out = s.getOutputStream();
            System.out.println("Sending Shutdown Request to " + Constants.NAME);
            out.write("\r\n".getBytes());
            out.flush();
            s.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}