| ServerSpawningTest.java |
1 package com.palantir.blog.processspawner;
2 /*
3 * All source code and information in this file is made
4 * available under the following licensing terms:
5 *
6 * Copyright (c) 2009, Palantir Technologies, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 *
21 * * Neither the name of Palantir Technologies, Inc. nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 *
38 */
39 import java.net.ConnectException;
40 import java.net.InetAddress;
41 import java.net.UnknownHostException;
42
43 import junit.framework.TestCase;
44
45 /**
46 * A simple ACK server functional test that will showcase a simple example of
47 * spawning external VMs during a JUnit {@link TestCase}.
48 *
49 * @author regs
50 *
51 */
52 public class ServerSpawningTest extends TestCase {
53
54 /**
55 * Our vmspawner that knows how to spin off
56 * new server VMs for every test.
57 */
58 JavaInvoke vmspawner;
59 /**
60 * The server process.
61 *
62 * This field is managed by setup/teardown for each test case.
63 */
64 Process p = null;
65
66 /**
67 * The ACK {@link Client} object.
68 *
69 * This field is managed by setup/teardown for each test case.
70 */
71 Client c = null;
72
73 /**
74 * Starts our server VM and waits to make sure that it's accepting connections.
75 *
76 */
77 @Override
78 protected void setUp() throws Exception {
79 System.out.println("-----------------------------------------------------");
80 System.out.println("Starting test " + getName());
81 System.out.flush();
82 super.setUp();
83 vmspawner = new JavaInvoke(Server.class.getCanonicalName(),null,null,null,null,null);
84 p = vmspawner.startStdinStderrInstance("server");
85 boolean serverIsUp = Server.checkServerIsUp(10000, 100, getServerAddress(), getServerPort());
86 assertTrue("Server did not become available",serverIsUp);
87 c = new Client(getServerAddress(),getServerPort());
88 }
89
90 /**
91 * Makes sure to shutdown external VM. Careful error handling is needed to make
92 * sure that an exception doesn't stop the VM from being destroyed.
93 */
94 @Override
95 protected void tearDown() throws Exception {
96 try {
97 super.tearDown();
98 } finally {
99 try {
100 try {
101 Client.sendShutdown(getServerAddress(), getServerPort());
102 } catch(ConnectException e) {
103 // this is expected
104 }
105 catch(Exception e) {
106 e.printStackTrace();
107 }
108 p.destroy();
109 p.waitFor();
110 c.close();
111 } catch (Exception e) {
112 e.printStackTrace();
113 }
114 p = null;
115 }
116 System.out.println("Finished test " + getName());
117 System.out.println("-----------------------------------------------------");
118 System.out.flush();
119 }
120
121 /**
122 * Test the ACK function of the protocol
123 * @throws Exception
124 */
125 public void testAck() throws Exception {
126 String response = c.sendMessage("some message");
127 assertEquals("Server sent us an unexpected response!",Server.ACK,response);
128 }
129
130 /**
131 * Test that the SHUTDOWN primitive is implemented correctly.
132 *
133 * @throws Exception
134 */
135 public void testShutdown() throws Exception {
136 final long preShutdown = System.currentTimeMillis();
137 Client.sendShutdown(getServerAddress(), getServerPort());
138 final long shutdownSent = System.currentTimeMillis();
139 p.waitFor();
140 final long processDead = System.currentTimeMillis();
141 System.out.println("Took " + (shutdownSent - preShutdown) + " ms to send shutdown.");
142 System.out.println("Took " + (processDead - preShutdown) + " ms for process to die.");
143 assertEquals("Expected process to exit with 0 exit code",0,p.exitValue());
144 }
145
146 InetAddress getServerAddress() throws UnknownHostException {
147 return InetAddress.getLocalHost();
148 }
149
150 int getServerPort() {
151 return Server.SERVERPORT;
152 }
153}
154