View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.internal.builder.multithreaded;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.PrintStream;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.concurrent.Callable;
28  import java.util.concurrent.CompletionService;
29  import java.util.concurrent.ExecutorCompletionService;
30  import java.util.concurrent.ExecutorService;
31  import java.util.concurrent.Executors;
32  import java.util.concurrent.Future;
33  
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.lifecycle.LifecycleNotFoundException;
36  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
37  import org.apache.maven.lifecycle.internal.ProjectBuildList;
38  import org.apache.maven.lifecycle.internal.ProjectSegment;
39  import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
40  import org.apache.maven.plugin.InvalidPluginDescriptorException;
41  import org.apache.maven.plugin.MojoNotFoundException;
42  import org.apache.maven.plugin.PluginDescriptorParsingException;
43  import org.apache.maven.plugin.PluginNotFoundException;
44  import org.apache.maven.plugin.PluginResolutionException;
45  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
46  import org.apache.maven.plugin.version.PluginVersionResolutionException;
47  import org.junit.jupiter.api.Test;
48  
49  import static org.junit.jupiter.api.Assertions.assertEquals;
50  
51  /**
52   */
53  class ThreadOutputMuxerTest {
54  
55      final String paid = "Paid";
56  
57      final String in = "In";
58  
59      final String full = "Full";
60  
61      @Test
62      void testSingleThreaded() throws Exception {
63          ProjectBuildList src = getProjectBuildList();
64          ProjectBuildList projectBuildList = new ProjectBuildList(Arrays.asList(src.get(0), src.get(1), src.get(2)));
65  
66          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
67          PrintStream systemOut = new PrintStream(byteArrayOutputStream);
68          ThreadOutputMuxer threadOutputMuxer = new ThreadOutputMuxer(projectBuildList, systemOut);
69  
70          threadOutputMuxer.associateThreadWithProjectSegment(projectBuildList.get(0));
71          System.out.print(paid); // No, this does not print to system.out. It's part of the test
72          assertEquals(paid.length(), byteArrayOutputStream.size());
73          threadOutputMuxer.associateThreadWithProjectSegment(projectBuildList.get(1));
74          System.out.print(in); // No, this does not print to system.out. It's part of the test
75          assertEquals(paid.length(), byteArrayOutputStream.size());
76          threadOutputMuxer.associateThreadWithProjectSegment(projectBuildList.get(2));
77          System.out.print(full); // No, this does not print to system.out. It's part of the test
78          assertEquals(paid.length(), byteArrayOutputStream.size());
79  
80          threadOutputMuxer.setThisModuleComplete(projectBuildList.get(0));
81          threadOutputMuxer.setThisModuleComplete(projectBuildList.get(1));
82          threadOutputMuxer.setThisModuleComplete(projectBuildList.get(2));
83          threadOutputMuxer.close();
84          assertEquals((paid + in + full).length(), byteArrayOutputStream.size());
85      }
86  
87      @Test
88      void testMultiThreaded() throws Exception {
89          ProjectBuildList projectBuildList = getProjectBuildList();
90  
91          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
92          PrintStream systemOut = new PrintStream(byteArrayOutputStream);
93          final ThreadOutputMuxer threadOutputMuxer = new ThreadOutputMuxer(projectBuildList, systemOut);
94  
95          final List<String> stringList = Arrays.asList(
96                  "Thinkin", "of", "a", "master", "plan", "Cuz", "ain’t", "nuthin", "but", "sweat", "inside", "my",
97                  "hand");
98          Iterator<String> lyrics = stringList.iterator();
99  
100         ExecutorService executor = Executors.newFixedThreadPool(10);
101         CompletionService<ProjectSegment> service = new ExecutorCompletionService<>(executor);
102 
103         List<Future<ProjectSegment>> futures = new ArrayList<>();
104         for (ProjectSegment projectBuild : projectBuildList) {
105             final Future<ProjectSegment> buildFuture =
106                     service.submit(new Outputter(threadOutputMuxer, projectBuild, lyrics.next()));
107             futures.add(buildFuture);
108         }
109 
110         for (Future<ProjectSegment> future : futures) {
111             future.get();
112         }
113         int expectedLength = 0;
114         for (int i = 0; i < projectBuildList.size(); i++) {
115             expectedLength += stringList.get(i).length();
116         }
117 
118         threadOutputMuxer.close();
119         final byte[] bytes = byteArrayOutputStream.toByteArray();
120         String result = new String(bytes);
121         assertEquals(expectedLength, bytes.length, result);
122     }
123 
124     class Outputter implements Callable<ProjectSegment> {
125         private final ThreadOutputMuxer threadOutputMuxer;
126 
127         private final ProjectSegment item;
128 
129         private final String response;
130 
131         Outputter(ThreadOutputMuxer threadOutputMuxer, ProjectSegment item, String response) {
132             this.threadOutputMuxer = threadOutputMuxer;
133             this.item = item;
134             this.response = response;
135         }
136 
137         public ProjectSegment call() throws Exception {
138             threadOutputMuxer.associateThreadWithProjectSegment(item);
139             System.out.print(response);
140             threadOutputMuxer.setThisModuleComplete(item);
141             return item;
142         }
143     }
144 
145     private ProjectBuildList getProjectBuildList()
146             throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
147                     NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException,
148                     PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
149         final MavenSession session = ProjectDependencyGraphStub.getMavenSession();
150         return ProjectDependencyGraphStub.getProjectBuildList(session);
151     }
152 }