<commit_before>package org.zstack.test.integration.core

import org.zstack.core.asyncbatch.While
import org.zstack.header.core.FutureCompletion
import org.zstack.header.core.NoErrorCompletion
import org.zstack.header.core.workflow.WhileCompletion
import org.zstack.testlib.SubCase
import org.zstack.testlib.util.TimeUnitUtil
import org.zstack.utils.TimeUtils
import org.zstack.utils.Utils
import org.zstack.utils.logging.CLogger

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

/**
 * Created by MaJin on 2017-05-02.
 */
class WhileCase extends SubCase{
    private static final CLogger logger = Utils.getLogger(WhileCase.class)
    static TIME_OUT = TimeUnit.SECONDS.toMillis(1)

    @Override
    void clean() {

    }

    @Override
    void setup() {
        INCLUDE_CORE_SERVICES = false
    }

    @Override
    void environment() {

    }

    @Override
    void test() {
        testRunAllWhenItemsEmpty()
        testRunAllCompletionAllDone()
        testRunStepWhenItemsEmpty()
        testRunStepCompletionDone()
        testRunStepComletionAllDone()
    }

    static void testRunAllWhenItemsEmpty(){
        FutureCompletion future = new FutureCompletion(null)

        new While<>(new ArrayList<String>()).all(new While.Do<String>() {
            @Override
            void accept(String item, WhileCompletion completion) {
                completion.done()
            }
        }).run(new NoErrorCompletion(){

            @Override
            void done() {
                future.success()
            }
        })

        future.await(TIME_OUT)

        assert future.success
    }

    static void testRunAllCompletionAllDone(){
        FutureCompletion future = new FutureCompletion(null)
        AtomicInteger count = new AtomicInteger(0)

        new While<>(["1", "2"]).all({item, completion ->
            logger.debug(String.format("item %s allDone", item))
            completion.allDone()
        }).run(new NoErrorCompletion(){
            @Override
            void done() {
                count.addAndGet(1)
                logger.debug("While is done")
                future.success()
            }
        })

        future.await(TIME_OUT)
        assert future.success
        assert count.get() == 1
    }

    static void testRunStepWhenItemsEmpty(){
        FutureCompletion future = new FutureCompletion(null)

        new While<>(new ArrayList<String>()).step({item, completion ->
            completion.done()
        }, 1).run(new NoErrorCompletion(){
            @Override
            void done() {
                future.success()
            }
        })

        future.await(TIME_OUT)
        assert future.success
    }

    static void testRunStepCompletionDone(){
        FutureCompletion future = new FutureCompletion(null)
        AtomicInteger whileDoneCount = new AtomicInteger(0)
        AtomicInteger itemDoneCount = new AtomicInteger(0)

        new While<>([1, 2, 3]).step({item, completion ->
            Thread.start {
                logger.debug("step " + item)
                TimeUnitUtil.sleepMilliseconds(TimeUnit.SECONDS.toMillis(item as long)/5 as long)
                itemDoneCount.addAndGet(1)
                completion.done()
            }
        }, 2).run(new NoErrorCompletion(){
            @Override
            void done() {
                whileDoneCount.addAndGet(1)
                logger.debug("While is done")
                future.success()
            }
        })

        future.await(TIME_OUT)
        assert future.success
        assert whileDoneCount.get() == 1<commit_msg>try to fix WhileCase Resolve ZSTAC-9695<commit_after>package org.zstack.test.integration.core

import org.zstack.core.asyncbatch.While
import org.zstack.header.core.FutureCompletion
import org.zstack.header.core.NoErrorCompletion
import org.zstack.header.core.workflow.WhileCompletion
import org.zstack.testlib.SubCase
import org.zstack.testlib.util.TimeUnitUtil
import org.zstack.utils.TimeUtils
import org.zstack.utils.Utils
import org.zstack.utils.logging.CLogger

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

/**
 * Created by MaJin on 2017-05-02.
 */
class WhileCase extends SubCase{
    private static final CLogger logger = Utils.getLogger(WhileCase.class)
    static TIME_OUT = TimeUnit.SECONDS.toMillis(15)

    @Override
    void clean() {

    }

    @Override
    void setup() {
        INCLUDE_CORE_SERVICES = false
    }

    @Override
    void environment() {

    }

    @Override
    void test() {
        testRunAllWhenItemsEmpty()
        testRunAllCompletionAllDone()
        testRunStepWhenItemsEmpty()
        testRunStepCompletionDone()
        testRunStepComletionAllDone()
    }

    static void testRunAllWhenItemsEmpty(){
        FutureCompletion future = new FutureCompletion(null)

        new While<>(new ArrayList<String>()).all(new While.Do<String>() {
            @Override
            void accept(String item, WhileCompletion completion) {
                completion.done()
            }
        }).run(new NoErrorCompletion(){

            @Override
            void done() {
                future.success()
            }
        })

        future.await(TIME_OUT)

        assert future.success
    }

    static void testRunAllCompletionAllDone(){
        FutureCompletion future = new FutureCompletion(null)
        AtomicInteger count = new AtomicInteger(0)

        new While<>(["1", "2"]).all({item, completion ->
            logger.debug(String.format("item %s allDone", item))
            completion.allDone()
        }).run(new NoErrorCompletion(){
            @Override
            void done() {
                count.addAndGet(1)
                logger.debug("While is done")
                future.success()
            }
        })

        future.await(TIME_OUT)
        assert future.success
        assert count.get() == 1
    }

    static void testRunStepWhenItemsEmpty(){
        FutureCompletion future = new FutureCompletion(null)

        new While<>(new ArrayList<String>()).step({item, completion ->
            completion.done()
        }, 1).run(new NoErrorCompletion(){
            @Override
            void done() {
                future.success()
            }
        })

        future.await(TIME_OUT)
        assert future.success
    }

    static void testRunStepCompletionDone(){
        FutureCompletion future = new FutureCompletion(null)
        AtomicInteger whileDoneCount = new AtomicInteger(0)
        AtomicInteger itemDoneCount = new AtomicInteger(0)

        new While<>([1, 2, 3]).step({item, completion ->
            Thread.start {
                logger.debug("step " + item)
                sleep(item * 200 as long)
                itemDoneCount.addAndGet(1)
                completion.done()
            }
        }, 2).run(new NoErrorCompletion(){
            @Override
            void done() {
                whileDoneCount.addAndGet(1)
                logger.debug("While is done")
                future.success()
            }
        })

        future.await(TIME_OUT)
        assert future.success
        assert whileDoneCount.get() == 1
{'fraction_non_alphanumeric': 0.09851120077918464, 'fraction_numerical': 0.007930986503408934, 'mean_word_length': 2.057422373458103, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '3229458', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1879, 'n_words': 385}