<commit_before>/*
 *    Copyright 2017 by <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package io.github.ddimitrov.nuggets

import spock.lang.Specification
import spock.lang.Subject
import spock.lang.Timeout
import spock.lang.Title

import java.util.concurrent.TimeoutException


@Title("Functions :: retry")
@Subject(Functions)
@Timeout(10)
class RetrySpec extends Specification {
    def "retry calls the closure until it passes"() {
        int i = 1

        when:
        def result = Functions.retry("incrementing", 1000) {
            assert ++i>=5
            return i
        }

        then:
        result == 5

    }

    def "but it may timeout"() {
        def testThread = Thread.currentThread()
        def interrupter = Thread.start { while (!Thread.interrupted()) testThread.interrupt() } // increase coverage, force spinning

        when:
        Functions.retry("failing", 100) {
            assert false
        }

        then:
        def e = thrown(TimeoutException)
        and: 'the exception message contains the description and extra details'
        e.message =~ 'failing'

        cleanup:
        interrupter.interrupt()
        while (testThread.interrupted) testThread.interrupted() // clear the interruptions of the current thread before calling join()
        interrupter.join()
    }

    def "one can listen for retries - useful for test reports"() {
        given:
        def results = [:]
        assert !Functions.snoopRetries("test-listener") { description, result, error ->
            results[description] = error ?: result
        }

        when:
        int i = 1
        def result = Functions.retry("incrementing", 1000) {
            assert ++i >= 5
            return i
        }
        result == 5

        and:
        Functions.retry("failing", 100) { assert false }

        then:
        thrown(TimeoutException)
        and:
        results['incrementing'] == 5
        results['failing'] instanceof TimeoutException

        cleanup:
        Functions.snoopRetries("test-listener", null)
    }
}<commit_msg>Composable retry - fixing race condition in test nuggets#61<commit_after>/*
 *    Copyright 2017 by <NAME>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package io.github.ddimitrov.nuggets

import spock.lang.Specification
import spock.lang.Subject
import spock.lang.Timeout
import spock.lang.Title

import java.util.concurrent.TimeoutException


@Title("Functions :: retry")
@Subject(Functions)
@Timeout(10)
class RetrySpec extends Specification {
    def "retry calls the closure until it passes"() {
        int i = 1

        when:
        def result = Functions.retry("incrementing", 1000) {
            assert ++i>=5
            return i
        }

        then:
        result == 5

    }

    def "but it may timeout"() {
        def testThread = Thread.currentThread()
        def interrupter = Thread.start { while (!Thread.interrupted()) testThread.interrupt() } // increase coverage, force spinning

        when:
        Functions.retry("failing", 100) {
            assert false
        }

        then:
        def e = thrown(TimeoutException)
        and: 'the exception message contains the description and extra details'
        e.message =~ 'failing'

        cleanup:
        interrupter.interrupt()
        Functions.retry("race between the interrupter interrupting us and us interrupting it", 10_000) {
            interrupter.join()
        }
    }

    def "one can listen for retries - useful for test reports"() {
        given:
        def results = [:]
        assert !Functions.snoopRetries("test-listener") { description, result, error ->
            results[description] = error ?: result
        }

        when:
        int i = 1
        def result = Functions.retry("incrementing", 1000) {
            assert ++i >= 5
            return i
        }
        result == 5

        and:
        Functions.retry("failing", 100) { assert false }

        then:
        thrown(TimeoutException)
        and:
        results['incrementing'] == 5
        results['failing'] instanceof TimeoutException

        cleanup:
        Functions.snoopRetries("test-listener", null)
    }
}
{'fraction_non_alphanumeric': 0.0914958238420653, 'fraction_numerical': 0.013097949886104784, 'mean_word_length': 2.2767412935323383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1691758', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1373, 'n_words': 471}