Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot compile valid Groovy code due to final variable #1015

Open
renatoathaydes opened this issue Aug 27, 2019 · 1 comment

Comments

@renatoathaydes
Copy link

commented Aug 27, 2019

Issue description

Valid Groovy class cannot be compiled (worked fine before upgrade).

How to reproduce

Attempt to compile this spec (sorry it's a bit long, I've tried to remove stuff from it but then the problem would go away):

import groovy.transform.TupleConstructor
import spock.lang.Specification

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

class BrokenSpec extends Specification {

    def "Can throttle multiple run requests together with a initial delay"() {
        given: 'A normal Runnable with latches to keep track of invocations'
        final long minimumTimeBetweenRuns = 150L
        final CountDownLatch firstCallLatch = new CountDownLatch(1)
        final CountDownLatch secondCallLatch = new CountDownLatch(2)
        final AtomicInteger counter = new AtomicInteger(0)

        Runnable countingRunnable = {
            println "Running the runnable for the ${counter.incrementAndGet()} time"
            [firstCallLatch, secondCallLatch]*.countDown()
        }

        and: 'A test job scheduler'
        def jobScheduler = new CoreJobScheduler(false)

        and: 'The Runnable is wrapped within an immediate ThrottledRunnable'
        def throttledRunnable = new ThrottledRunnable(countingRunnable, jobScheduler)

        when: 'The ThrottledRunnable is requested run once'
        throttledRunnable.run()

        then: 'It should NOT have immediately run'
        !firstCallLatch.await(1, TimeUnit.MILLISECONDS)

        when: 'The ThrottledRunnable is requested to run again'
        throttledRunnable.run()

        then: 'Nothing should happen immediately as the request is batched to run later'
        !firstCallLatch.await((minimumTimeBetweenRuns / 2).toLong(), TimeUnit.MILLISECONDS)

        and: 'The underlying runnable should run first time within the minimum time between executions (+ small delay)'
        firstCallLatch.await(minimumTimeBetweenRuns + 10, TimeUnit.MILLISECONDS)

        and: 'No other calls are expected'
        !secondCallLatch.await(minimumTimeBetweenRuns + 10, TimeUnit.MILLISECONDS)

        when: 'The ThrottledRunnable is requested to run 3 times'
        3.times { throttledRunnable.run() }

        then: 'The underlying runnable should run only once within the minimum time between executions (+ small delay)'
        secondCallLatch.await(minimumTimeBetweenRuns + 10, TimeUnit.MILLISECONDS)

        and: 'we expect only 2 total calls'
        counter.get() == 2

        cleanup:
        jobScheduler?.close()
    }
}

class CoreJobScheduler {
    CoreJobScheduler(boolean x) {}

    void close() {}
}

@TupleConstructor
class ThrottledRunnable {
    Runnable runnable
    CoreJobScheduler jobScheduler

    void run() {
        Thread.start(runnable)
    }
}

Run:

./gradlew compileTestGroovy

Error:

> Task :compileTestGroovy FAILED
startup failed:
/Users/renato/programming/projects/spock-tests/src/test/groovy/BrokenSpec.groovy: -1: The variable [firstCallLatch] is declared final but is reassigned
. At [-1:-1]  @ line -1, column -1.
1 error

Expected outcome

Should compile ok.

Additional Environment information

From ./gradlew --version:

------------------------------------------------------------
Gradle 5.2.1
------------------------------------------------------------

Build time:   2019-02-08 19:00:10 UTC
Revision:     f02764e074c32ee8851a4e1877dd1fea8ffb7183

Kotlin DSL:   1.1.3
Kotlin:       1.3.20
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_181 (Azul Systems, Inc. 25.181-b02)
OS:           Mac OS X 10.14.6 x86_64

Also attempted with Oracle's JVM and the result is the same:

JVM:          1.8.0_171 (Oracle Corporation 25.171-b11)

Groovy/Spock version

dependencies {
    compile 'org.spockframework:spock-core:1.3-groovy-2.5'
}
@renatoathaydes

This comment has been minimized.

Copy link
Author

commented Aug 27, 2019

Note: removing extends Specification from the class fixes the issue, so the Groovy compiler seems to be doing the right thing... so the problem must be in the AST transformation done by Spock?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.