View Javadoc
1   /*
2    * Copyright (C) 2017 Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.transport;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.net.InetSocketAddress;
17  
18  import org.junit.Test;
19  
20  /**
21   * Daemon tests.
22   */
23  public class DaemonTest {
24  
25  	@Test
26  	public void testDaemonStop() throws Exception {
27  		Daemon d = new Daemon();
28  		d.start();
29  		InetSocketAddress address = d.getAddress();
30  		assertTrue("Port should be allocated", address.getPort() > 0);
31  		assertTrue("Daemon should be running", d.isRunning());
32  		Thread.sleep(1000); // Give it time to enter accept()
33  		d.stopAndWait();
34  		// Try to start a new Daemon again on the same port
35  		d = new Daemon(address);
36  		d.start();
37  		InetSocketAddress newAddress = d.getAddress();
38  		assertEquals("New daemon should run on the same port", address,
39  				newAddress);
40  		assertTrue("Daemon should be running", d.isRunning());
41  		Thread.sleep(1000);
42  		d.stopAndWait();
43  	}
44  
45  	@Test
46  	public void testDaemonRestart() throws Exception {
47  		Daemon d = new Daemon();
48  		d.start();
49  		InetSocketAddress address = d.getAddress();
50  		assertTrue("Port should be allocated", address.getPort() > 0);
51  		assertTrue("Daemon should be running", d.isRunning());
52  		Thread.sleep(1000);
53  		d.stopAndWait();
54  		// Re-start the same daemon
55  		d.start();
56  		InetSocketAddress newAddress = d.getAddress();
57  		assertEquals("Daemon should again run on the same port", address,
58  				newAddress);
59  		assertTrue("Daemon should be running", d.isRunning());
60  		Thread.sleep(1000);
61  		d.stopAndWait();
62  	}
63  }