Commit 5d74dc75 authored by Martti Käärik's avatar Martti Käärik
Browse files

Tear down test scope at behaviour completion

parent 8973584a
Loading
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -453,7 +453,16 @@ public class JUnitTestGenerator extends Renderer {
			line("executor.addTest(a" + tClass + ".getTestControl(), () -> a" + tClass + ".test_" + tdName + "());");

		newLine();
		append("try");
		blockOpen();
		line("executor.execute();");
		blockClose();
		append("finally");
		blockOpen();
		lineComment("End the test scope for every component.");
		for (String tClass : testerClasses)
			line("a" + tClass + ".getTestControl().shutdown();");
		blockClose();

		blockClose();
		newLine();
@@ -499,6 +508,7 @@ public class JUnitTestGenerator extends Renderer {
		newLine();

		writeTestConfiguration(td, tester);
		writeTestShutdown(td);

		newLine();
		writeTypes(td);
@@ -601,6 +611,17 @@ public class JUnitTestGenerator extends Renderer {
		newLine();
	}

	private void writeTestShutdown(TestDescription td) {
		line("@AfterAll");
		append("public void shutdown_" + getElementName(td) + "()");
		blockOpen();
		lineComment("End the test scope: release ports/clients and stop processing");
		lineComment("any messages arriving after the test behaviour has completed.");
		line(COMPONENT_FIELD + ".shutdown();");
		blockClose();
		newLine();
	}

	private void writeGateReference(GateReference e) {
		append("new GateReferenceImpl(");
		writeElement(e.getGate());
+25 −2
Original line number Diff line number Diff line
@@ -77,6 +77,10 @@ public class HttpSystemAdapter implements SystemAdapter {
	private Queue<HttpInput> unhandledInputs = new ConcurrentLinkedQueue<>();
	private Map<Connection, Queue<HttpExchange>> unhandledRequests = new Hashtable<Connection, Queue<HttpExchange>>();

	// Set once the test scope has ended (see stop()): after this, late inbound
	// requests/responses are dropped rather than buffered, decoded or logged.
	private volatile boolean stopped = false;

	public HttpSystemAdapter(Validator validator, Reporter reporter) {
		this.validator = validator;
		this.reporter = reporter;
@@ -161,12 +165,28 @@ public class HttpSystemAdapter implements SystemAdapter {

	}
	
	@Override
	public void stop() {
		if (stopped)
			return;
		stopped = true;
		for (HttpServer server : this.servers.values())
			server.stop(5);
			server.stop(0);
		this.servers.clear();
		synchronized (unhandledInputs) {
			unhandledInputs.clear();
			unhandledInputs.notifyAll();
		}
		synchronized (unhandledRequests) {
			unhandledRequests.clear();
		}
	}

	private void handleRequest(HttpExchange request, HttpEndpoint target, Connection connection) throws IOException {
		if (stopped) {
			request.close();
			return;
		}
		synchronized (unhandledInputs) {
			unhandledInputs.add(new HttpInput(null, request, target, connection));
			unhandledInputs.notifyAll();
@@ -175,6 +195,9 @@ public class HttpSystemAdapter implements SystemAdapter {

	private void handleResponse(HttpResponse<String> response, Throwable t, HttpEndpoint target,
			Connection connection) {
		// Drop late responses that complete after the test scope has ended.
		if (stopped)
			return;
		if (t != null)
			handleError(t);
		else {
+15 −0
Original line number Diff line number Diff line
@@ -333,6 +333,21 @@ public class TestControl {
		receiverHubs.values().forEach(hub -> hub.resume());
	}

	/**
	 * Ends the test scope once the behaviour has completed: stops the receiver
	 * hubs, releases the system adapter (open ports, clients, buffered inbound
	 * data) and shuts down the executor. After this no further incoming data is
	 * accepted, decoded or logged, so messages arriving once the test is over do
	 * not appear as stray log entries. Idempotent.
	 */
	public void shutdown() {
		receiverHubs.values().forEach(ReceiverHub::stop);
		receiverHubs.clear();
		if (systemAdapter != null)
			systemAdapter.stop();
		executor.shutdownNow();
	}

	/**
	 * Throws a {@link StopException} to terminate the test execution. Wrapping
	 * the throw in a method call lets the Java compiler treat the call site as
+8 −0
Original line number Diff line number Diff line
@@ -23,6 +23,14 @@ public interface SystemAdapter {
	 */
	void configure(Connection[] connections, NamedElement component);

	/**
	 * Release all resources acquired for the test execution (open server sockets,
	 * clients, buffered inbound data). Called once when the test behaviour has
	 * completed so that no further incoming data is accepted, decoded or logged
	 * after the test is over — the test scope has a definite end.
	 */
	void stop();

	/**
	 * Encode and send the data to system under test using protocol or adapter
	 * specific means.