-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MSHARED-1072] Poll data from input stream
Input stream can be a System.in - all read will be blocked We need read data in no blocking mode
- Loading branch information
1 parent
37fa530
commit 1c264ba
Showing
4 changed files
with
116 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 0 additions & 91 deletions
91
src/test/java/org/apache/maven/shared/utils/cli/StreamFeederTest.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
src/test/java/org/apache/maven/shared/utils/cli/StreamPollFeederTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.maven.shared.utils.cli; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class StreamPollFeederTest { | ||
|
||
@Test | ||
public void waitUntilFeederDoneOnInputStream() throws Exception { | ||
|
||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
StreamPollFeeder streamPollFeeder = new StreamPollFeeder(System.in, outputStream); | ||
|
||
// start thread | ||
streamPollFeeder.start(); | ||
|
||
// wait a moment | ||
Thread.sleep(100); | ||
|
||
// wait until process finish | ||
streamPollFeeder.waitUntilDone(); | ||
assertNull(streamPollFeeder.getException()); | ||
} | ||
|
||
@Test | ||
public void dataShouldBeCopied() throws InterruptedException, IOException { | ||
|
||
StringBuilder TEST_DATA = new StringBuilder(); | ||
for (int i = 0; i < 100; i++) { | ||
TEST_DATA.append("TestData"); | ||
} | ||
|
||
ByteArrayInputStream inputStream = | ||
new ByteArrayInputStream(TEST_DATA.toString().getBytes()); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
StreamPollFeeder streamPollFeeder = new StreamPollFeeder(inputStream, outputStream); | ||
|
||
streamPollFeeder.start(); | ||
|
||
// wait until all data from steam will be read | ||
while (outputStream.size() < TEST_DATA.length()) { | ||
Thread.sleep(100); | ||
} | ||
|
||
// wait until process finish | ||
streamPollFeeder.waitUntilDone(); | ||
assertNull(streamPollFeeder.getException()); | ||
|
||
assertEquals(TEST_DATA.toString(), outputStream.toString()); | ||
} | ||
} |