"Fossies" - the Fresh Open Source Software Archive

Member "gradle-8.1.1/subprojects/logging/src/integTest/groovy/org/gradle/internal/logging/console/taskgrouping/AbstractBasicGroupedTaskLoggingFunctionalTest.groovy" (20 Apr 2023, 8315 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright 2017 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 package org.gradle.internal.logging.console.taskgrouping
   18 
   19 import org.gradle.integtests.fixtures.console.AbstractConsoleGroupedTaskFunctionalTest
   20 import org.gradle.integtests.fixtures.executer.GradleContextualExecuter
   21 import org.gradle.integtests.fixtures.executer.GradleHandle
   22 import org.gradle.internal.logging.sink.GroupingProgressLogEventGenerator
   23 import org.gradle.test.fixtures.ConcurrentTestUtil
   24 import org.gradle.test.fixtures.server.http.BlockingHttpServer
   25 import org.junit.Rule
   26 import spock.lang.IgnoreIf
   27 
   28 abstract class AbstractBasicGroupedTaskLoggingFunctionalTest extends AbstractConsoleGroupedTaskFunctionalTest {
   29     private static long sleepTimeout = GroupingProgressLogEventGenerator.HIGH_WATERMARK_FLUSH_TIMEOUT / 2 * 3
   30 
   31     @Rule
   32     BlockingHttpServer server = new BlockingHttpServer()
   33 
   34     def "multi-project build tasks logs are grouped"() {
   35         server.start()
   36 
   37         given:
   38         settingsFile << "include '1', '2', '3'"
   39 
   40         buildFile << """
   41             subprojects {
   42                 task log {
   43                     def projectName = project.name
   44                     doFirst {
   45                         logger.error "Error from " + projectName
   46                         logger.quiet "Output from " + projectName
   47                         new java.net.URL("${server.uri}/log" + projectName).openConnection().getContentLength()
   48                         logger.quiet "Done with " + projectName
   49                         logger.error "Done with " + projectName
   50                     }
   51                 }
   52             }
   53         """
   54 
   55         when:
   56         server.expectConcurrent("log1", "log2", "log3")
   57         executer.withArgument("--parallel")
   58         // run build in another process to avoid interference from logging from test fixtures
   59         result = executer.withTasks("log").start().waitForFinish()
   60 
   61         then:
   62         result.groupedOutput.taskCount == 3
   63         if (errorsShouldAppearOnStdout()) {
   64             // both stdout and stderr are attached to the console
   65             assert result.groupedOutput.task(':1:log').output == "Error from 1\nOutput from 1\nDone with 1\nDone with 1"
   66             assert result.groupedOutput.task(':2:log').output == "Error from 2\nOutput from 2\nDone with 2\nDone with 2"
   67             assert result.groupedOutput.task(':3:log').output == "Error from 3\nOutput from 3\nDone with 3\nDone with 3"
   68         } else {
   69             assert result.groupedOutput.task(':1:log').output == "Output from 1\nDone with 1"
   70             assert result.groupedOutput.task(':2:log').output == "Output from 2\nDone with 2"
   71             assert result.groupedOutput.task(':3:log').output == "Output from 3\nDone with 3"
   72 
   73             ['Error from 1', 'Done with 1', 'Error from 2', 'Done with 2', 'Error from 3', 'Done with 3'].each(result.&assertHasErrorOutput)
   74         }
   75     }
   76 
   77     def "logs at execution time are grouped"() {
   78         given:
   79         buildFile << """
   80             task log {
   81                 logger.quiet 'Logged during configuration'
   82                 doFirst {
   83                     logger.quiet 'First line of text'
   84                     logger.quiet 'Second line of text'
   85                 }
   86             }
   87         """
   88 
   89         when:
   90         succeeds('log')
   91 
   92         then:
   93         result.groupedOutput.task(':log').output == "First line of text\nSecond line of text"
   94     }
   95 
   96     def "system out and err gets grouped"() {
   97         given:
   98         buildFile << """
   99             task log {
  100                 logger.quiet 'Logged during configuration'
  101                 doFirst {
  102                     System.out.println("Standard out 1")
  103                     System.err.println("Standard err 1")
  104                     System.out.println("Standard out 2")
  105                     System.err.println("Standard err 2")
  106                 }
  107             }
  108         """
  109 
  110         when:
  111         succeeds('log')
  112 
  113         then:
  114         if (errorsShouldAppearOnStdout()) {
  115             result.groupedOutput.task(':log').output == "Standard out 1\nStandard err 1\nStandard out 2\nStandard err 2"
  116         } else {
  117             result.groupedOutput.task(':log').output == "Standard out 1\nStandard out 2\n"
  118             result.assertHasErrorOutput("Standard err 1\nStandard err 2")
  119         }
  120     }
  121 
  122     def "grouped output is displayed for failed tasks"() {
  123         given:
  124         buildFile << """
  125             task log {
  126                 logger.quiet 'Logged during configuration'
  127                 doFirst {
  128                     logger.quiet 'First line of text'
  129                     logger.quiet ''
  130                     logger.quiet ''
  131                     logger.quiet 'Last line of text'
  132                     throw new GradleException('Forced failure')
  133                 }
  134             }
  135         """
  136         when:
  137         fails('log')
  138 
  139         then:
  140         result.groupedOutput.task(':log').output =~ /First line of text\n{3,}Last line of text/
  141     }
  142 
  143     @IgnoreIf({ GradleContextualExecuter.parallel })
  144     def "long running task output correctly interleave with other tasks in parallel"() {
  145         given:
  146         server.start()
  147 
  148         buildFile << """
  149             project(":a") {
  150                 task log {
  151                     doLast {
  152                         logger.quiet 'Before'
  153                         ${callFromBuild('a-waiting')}
  154                         logger.quiet 'After'
  155                         assert false
  156                     }
  157                 }
  158             }
  159 
  160             project(":b") {
  161                 task log {
  162                     doLast {
  163                         ${callFromBuild('b-waiting')}
  164                         logger.quiet 'Interrupting output'
  165                         ${callFromBuild('b-done')}
  166                     }
  167                 }
  168             }
  169 
  170             task run {
  171                 dependsOn project(':a').log, project(':b').log
  172             }
  173         """
  174 
  175         settingsFile << """
  176             include 'a', 'b'
  177         """
  178 
  179         when:
  180         def waiting = server.expectConcurrentAndBlock("a-waiting", "b-waiting")
  181         def done = server.expectAndBlock("b-done")
  182         def build = executer.withArguments("--parallel").withTasks("run").start()
  183 
  184         waiting.waitForAllPendingCalls()
  185         waiting.release("b-waiting")
  186         done.waitForAllPendingCalls()
  187         done.releaseAll()
  188         waiting.releaseAll()
  189         result = build.waitForFailure()
  190 
  191         then:
  192         result.groupedOutput.task(':a:log').output == 'Before\nAfter'
  193         result.groupedOutput.task(':a:log').outcome == 'FAILED'
  194         result.groupedOutput.task(':b:log').output == 'Interrupting output'
  195         result.groupedOutput.task(':b:log').outcome == null
  196     }
  197 
  198     def "long running task output are flushed after delay"() {
  199         server.start()
  200 
  201         given:
  202         buildFile << """
  203             task log {
  204                 doLast {
  205                     logger.quiet 'Before'
  206                     ${callFromBuild('running')}
  207                     logger.quiet 'After'
  208                 }
  209             }
  210         """
  211 
  212         def handle = server.expectAndBlock(server.get('running'))
  213         def gradle = executer.withTasks('log').start()
  214 
  215         when:
  216         handle.waitForAllPendingCalls()
  217         assertOutputContains(gradle, "Before")
  218         handle.releaseAll()
  219         result = gradle.waitForFinish()
  220 
  221         then:
  222         result.groupedOutput.task(':log').output == 'Before\nAfter'
  223 
  224         cleanup:
  225         gradle?.waitForFinish()
  226     }
  227 
  228     String callFromBuild(String name) {
  229         return "new URL('${server.uri}/${name}').text"
  230     }
  231 
  232     protected static void assertOutputContains(GradleHandle gradle, String str) {
  233         ConcurrentTestUtil.poll(sleepTimeout / 1000 as double) {
  234             assert gradle.standardOutput =~ /(?ms)$str/
  235         }
  236     }
  237 }