### Question:
ProcessStartingDaemonSummoner implements DaemonSummoner { @Override public void connectToDaemon(SuiteConfiguration suite, DaemonConfiguration daemon, ActorRef<DaemonListener> listener) { int port = daemonConnector.listenOnAnyPort( new OneTimeDaemonListenerFactory( withInitialMessageTimeout(listener.tell(), daemon.getStartupTimeout()))); daemon = daemon.melt() .setDaemonDir(steward.createDaemonDir(daemon.getJumiHome())) .setLauncherPort(port) .freeze(); try { JvmArgs jvmArgs = new JvmArgsBuilder() .setExecutableJar(steward.getDaemonJar(daemon.getJumiHome())) .setWorkingDir(Paths.get(suite.getWorkingDirectory())) .setJvmOptions(suite.getJvmOptions()) .setSystemProperties(daemon.toSystemProperties()) .setProgramArgs(daemon.toProgramArgs()) .freeze(); Process process = processStarter.startJavaProcess(jvmArgs); copyInBackground(process.getInputStream(), outputListener); } catch (Exception e) { throw Boilerplate.rethrow(e); } } ProcessStartingDaemonSummoner(Steward steward,
                                         ProcessStarter processStarter,
                                         NetworkServer daemonConnector,
                                         @WillClose OutputStream outputListener); @Override void connectToDaemon(SuiteConfiguration suite,
                                DaemonConfiguration daemon,
                                ActorRef<DaemonListener> listener);  }### Answer:
@Test public void tells_to_daemon_the_daemon_directory_to_use() { daemonSummoner.connectToDaemon(dummySuiteConfig, dummyDaemonConfig, ActorRef.wrap(daemonListener)); DaemonConfiguration daemonConfig = parseDaemonArguments(processStarter.lastArgs); assertThat(daemonConfig.getDaemonDir(), is(dummyDaemonDir)); }
@Test public void tells_to_daemon_the_socket_to_contact() { daemonConnector.portToReturn = 123; daemonSummoner.connectToDaemon(dummySuiteConfig, dummyDaemonConfig, ActorRef.wrap(daemonListener)); DaemonConfiguration daemonConfig = parseDaemonArguments(processStarter.lastArgs); assertThat(daemonConfig.getLauncherPort(), is(123)); }
@Test public void tells_to_output_listener_what_the_daemon_prints() { processStarter.processToReturn.inputStream = new ByteArrayInputStream("hello".getBytes()); daemonSummoner.connectToDaemon(dummySuiteConfig, dummyDaemonConfig, ActorRef.wrap(daemonListener)); assertEventually(outputListener, hasToString("hello"), TIMEOUT); }
@Test public void tells_to_daemon_listener_what_events_the_daemon_sends() { daemonSummoner.connectToDaemon(dummySuiteConfig, dummyDaemonConfig, ActorRef.wrap(daemonListener)); NetworkEndpoint<Event<SuiteListener>, Event<RequestListener>> endpoint = daemonConnector.lastEndpointFactory.createEndpoint(); Event<SuiteListener> anyMessage = new OnSuiteStartedEvent(); endpoint.onMessage(anyMessage); verify((NetworkEndpoint<Event<SuiteListener>, Event<RequestListener>>) daemonListener).onMessage(anyMessage); }
@Test public void reports_an_internal_error_if_the_daemon_fails_to_connect_within_a_timeout() throws InterruptedException { SpyListener<SuiteListener> spy = new SpyListener<>(SuiteListener.class); SuiteListener expect = spy.getListener(); CountDownLatch expectedMessagesArrived = new CountDownLatch(3); expect.onSuiteStarted(); expect.onInternalError("Failed to start the test runner daemon process", StackTrace.from(new RuntimeException("Could not connect to the daemon: timed out after 0 ms"))); expect.onSuiteFinished(); spy.replay(); daemonSummoner.connectToDaemon( dummySuiteConfig, new DaemonConfigurationBuilder() .setStartupTimeout(0) .freeze(), actorRef(new FakeDaemonListener(countMessages(expect, expectedMessagesArrived)))); expectedMessagesArrived.await(TIMEOUT / 2, TimeUnit.MILLISECONDS); spy.verify(); }