Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/main/resources/clojuresque/tasks/compile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

(deftask main
[{:keys [compile-mode warn-on-reflection source-files]}]
(let [mode (condp = compile-mode
"compile" clojure.core/compile
"require" clojure.core/require
(throw
(Exception. "You must choose a mode: compile or require.")))
namespaces (namespaces source-files)]
(binding [*warn-on-reflection* warn-on-reflection
*compile-path* (System/getProperty "clojure.compile.path")]
(doseq [nspace namespaces]
(mode nspace))))
true)
;; This entry point runs only in the standalone compiler JVM, not the daemon.
(try
(let [mode (condp = compile-mode
"compile" clojure.core/compile
"require" clojure.core/require
(throw
(Exception. "You must choose a mode: compile or require.")))
namespaces (namespaces source-files)]
(binding [*warn-on-reflection* warn-on-reflection
*compile-path* (System/getProperty "clojure.compile.path")]
(doseq [nspace namespaces]
(mode nspace))))
true
(finally
;; Orderly shutdown lets executor work finish without the idle keepalive.
(shutdown-agents))))
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,72 @@ class NoToolchainNebulaClojurePluginIntegrationSpec extends BaseIntegrationTestK
'''.stripIndent()


def 'compiler worker shuts down agents after #mode with failure=#failure'() {
given:
def sourceDir = new File(projectDir, 'worker-source')
sourceDir.mkdirs()
def source = new File(sourceDir, 'worker.clj')
def completed = new File(projectDir, 'agent-completed')
source.text = '''
(ns worker)
(def work (agent nil))
;; Still running when compilation ends; shutdown-agents must let it finish.
(send-off work (fn [_] (Thread/sleep 500) (spit "COMPLETED" "done") nil))
'''.stripIndent().replace('COMPLETED', completed.absolutePath.replace('\\', '/')) +
(failure ? '\n(throw (ex-info "original compiler failure" {}))\n' : '')
def classes = new File(projectDir, 'worker-classes')
classes.mkdirs()
def output = new File(projectDir, 'worker-output')
def clojureJar = new File(clojure.lang.RT.protectionDomain.codeSource.location.toURI())
// Use the real compiler resources and stdin protocol, but never load them in this JVM.
def process = new ProcessBuilder(
new File(System.getProperty('java.home'), 'bin/java').absolutePath,
"-Dclojure.compile.path=${classes.absolutePath}".toString(),
'-cp', [System.getProperty('java.class.path'), clojureJar, sourceDir].join(File.pathSeparator),
'clojure.main', '-').redirectErrorStream(true).redirectOutput(output).start()
process.outputStream.withWriter('UTF-8') { writer ->
['clojuresque/util.clj', 'clojuresque/tasks/compile.clj'].each {
writer << getClass().classLoader.getResource(it).text << '\n'
}
// Catch outside main so a compiler error cannot hide missing cleanup via clojure.main's exit.
writer << '''
(try
(println "TASK-RESULT" (clojuresque.tasks.compile/main))
(catch Throwable t
(println "COMPILER-ERROR" (.getMessage t))
(loop [cause t]
(if-let [nested (.getCause cause)]
(recur nested)
(println "ROOT-CAUSE" (.getMessage cause))))))
'''.stripIndent()
writer << "\n{:compile-mode \"${mode}\" :source-files [\"${source.absolutePath.replace('\\', '/')}\"]}\n"
}

when:
def exited = process.waitFor(20, java.util.concurrent.TimeUnit.SECONDS)

then:
assert exited: "Compiler worker did not exit promptly: ${output.text}"
process.exitValue() == 0
assert completed.exists(): 'Shutdown interrupted or discarded the running agent action'
completed.text == 'done'
failure ? output.text.contains('ROOT-CAUSE original compiler failure') : output.text.contains('TASK-RESULT true')
!failure || !output.text.contains('TASK-RESULT')

cleanup:
if (process?.isAlive()) {
process.destroyForcibly()
process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)
}

where:
mode | failure
'compile' | false
'require' | false
'compile' | true
'require' | true
}

def 'can compile clojure'() {
buildFile << '''\
plugins {
Expand Down
Loading