<commit_before>package com.github.platan.varnishexec

import static java.util.concurrent.TimeUnit.MILLISECONDS

import com.github.platan.varnishexec.net.SocketPortChecker
import spock.lang.Requires
import spock.lang.Specification
import spock.lang.Timeout

class VarnishExecSpec extends Specification {

    def 'throw UncheckedIOException when varnishd command is not found'() {
        given:
        def varnishExec = VarnishExecs.newVarnishExec()

        when:
        varnishExec.start(VarnishCommand.newBuilder().withVarnishdCommand('unknownVarnishdCommand').build())

        then:
        thrown(UncheckedIOException)
    }

    @Requires({ os.linux })
    @Timeout(value = 200, unit = MILLISECONDS)
    def 'throw exception when varnishd return error code'() {
        given:
        varnishdInstalled()
        !varnishRunning()
        def command = VarnishCommand.newBuilder().withConfigFile('nonexistent_config_file').build()
        def exec = VarnishExecs.newVarnishExec()

        when:
        exec.start(command)

        then:
        thrown IllegalStateException
    }

    @Requires({ os.linux })
    def 'wait until varnish start listening'() {
        given:
        varnishdInstalled()
        !varnishRunning()
        def command = VarnishCommand.newBuilder().withConfigFile('./src/test/resources/simple.vcl').build()
        def exec = VarnishExecs.newVarnishExec()

        when:
        exec.start(command)

        then:
        !new SocketPortChecker().isFree(command.address)

        cleanup:
        killVarnishd()
    }

    @Requires({ os.linux })
    def 'close varnish process'() {
        given:
        varnishdInstalled()
        !varnishRunning()
        def command = VarnishCommand.newBuilder().withConfigFile('./src/test/resources/simple.vcl').build()
        def exec = VarnishExecs.newVarnishExec()
        def process = exec.start(command)

        when:
        process.kill()

        then:
        !varnishRunning()

        cleanup:
        killVarnishd()
    }

    private static boolean varnishRunning() {
        'pidof varnishd'.execute().waitFor() == 0
    }

    static varnishdInstalled() {
        try {
            'varnishd -V'.execute().waitFor() == 0
        } catch (IOException exception) {
            false<commit_msg>Ignore varnishd processes with ppid=1<commit_after>package com.github.platan.varnishexec

import static java.util.concurrent.TimeUnit.MILLISECONDS

import com.github.platan.varnishexec.net.SocketPortChecker
import spock.lang.Requires
import spock.lang.Specification
import spock.lang.Timeout

class VarnishExecSpec extends Specification {

    def 'throw UncheckedIOException when varnishd command is not found'() {
        given:
        def varnishExec = VarnishExecs.newVarnishExec()

        when:
        varnishExec.start(VarnishCommand.newBuilder().withVarnishdCommand('unknownVarnishdCommand').build())

        then:
        thrown(UncheckedIOException)
    }

    @Requires({ os.linux })
    @Timeout(value = 200, unit = MILLISECONDS)
    def 'throw exception when varnishd return error code'() {
        given:
        varnishdInstalled()
        !varnishRunning()
        def command = VarnishCommand.newBuilder().withConfigFile('nonexistent_config_file').build()
        def exec = VarnishExecs.newVarnishExec()

        when:
        exec.start(command)

        then:
        thrown IllegalStateException
    }

    @Requires({ os.linux })
    def 'wait until varnish start listening'() {
        given:
        varnishdInstalled()
        !varnishRunning()
        def command = VarnishCommand.newBuilder().withConfigFile('./src/test/resources/simple.vcl').build()
        def exec = VarnishExecs.newVarnishExec()

        when:
        exec.start(command)

        then:
        !new SocketPortChecker().isFree(command.address)

        cleanup:
        killVarnishd()
    }

    @Requires({ os.linux })
    def 'close varnish process'() {
        given:
        varnishdInstalled()
        !varnishRunning()
        def command = VarnishCommand.newBuilder().withConfigFile('./src/test/resources/simple.vcl').build()
        def exec = VarnishExecs.newVarnishExec()
        def process = exec.start(command)

        when:
        process.kill()

        then:
        !varnishRunning()

        cleanup:
        killVarnishd()
    }

    private static boolean varnishRunning() {
        'ps o cmd= --ppid 1 --deselect | grep "[v]arnish"'.execute().waitFor() == 0
    }

    static varnishdInstalled() {
        try {
            'varnishd -V'.execute().waitFor() == 0
        } catch (IOException exception) {
            false
{'fraction_non_alphanumeric': 0.10552438498057834, 'fraction_numerical': 0.002589555459646094, 'mean_word_length': 2.5327743902439024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264863', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1373, 'n_words': 263}