Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/process/StreamMessageInput.java
41159 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share.process;2324import java.util.List;25import java.util.ArrayList;26import nsk.share.TestBug;2728public class StreamMessageInput implements MessageInput {29private Object sync = new Object();30private List<String> lines = new ArrayList<String>();31private int position = 0;32private volatile boolean active = false;33private volatile Throwable exception;3435public StreamMessageInput() {36}3738public StreamMessageInput(StreamReader sd) {39bind(sd);40}4142public StreamListener createListener() {43return new Listener();44}4546public void bind(StreamReader sd) {47sd.addListener(createListener());48}4950public boolean isActive() {51return active;52}5354public boolean isException() {55return exception != null;56}5758public Throwable getException() {59return exception;60}6162public boolean waitForStart(long timeout) throws InterruptedException {63long startTime = System.currentTimeMillis();64long curTime = startTime;65synchronized (sync) {66while (!active && curTime - startTime < timeout) {67sync.wait(curTime - startTime);68curTime = System.currentTimeMillis();69}70}71return active;72}7374public boolean waitForFinish(long timeout) throws InterruptedException {75long startTime = System.currentTimeMillis();76long curTime = startTime;77synchronized (sync) {78while (active && curTime - startTime < timeout) {79sync.wait(curTime - startTime);80curTime = System.currentTimeMillis();81}82}83return !active;84}8586public boolean waitForMessage(String msg, long timeout) throws InterruptedException {87long startTime = System.currentTimeMillis();88long curTime = startTime;89int n = position;90synchronized (sync) {91while (curTime - startTime < timeout) {92while (n < lines.size()) {93// System.out.println("Check: " + lines.get(n));94if (msg == null || lines.get(n++).contains(msg)) {95return true;96}97}98sync.wait(timeout - (curTime - startTime));99curTime = System.currentTimeMillis();100}101return false;102}103}104105public boolean waitForMessage(long timeout) throws InterruptedException {106return waitForMessage(null, timeout);107}108109public String getMessage() {110if (position < lines.size())111return lines.get(position++);112else113return null;114}115116public String getMessage(int index) {117return lines.get(index);118}119120public int getPosition() {121return position;122}123124public void setPosition(int position) {125this.position = position;126}127128public int getMessageCount() {129return lines.size();130}131132public List<String> getMessages() {133return getMessages(position, lines.size());134}135136public List<String> getMessages(int to) {137return getMessages(position, to);138}139140public List<String> getMessages(int from, int to) {141synchronized (sync) {142if (to < 0)143to = lines.size() + to;144position = Math.max(position, to);145return new ArrayList<String>(lines.subList(from, to));146}147}148149public void reset() {150synchronized (sync) {151position = lines.size();152}153}154155private class Listener implements StreamListener {156@Override157public void onStart() {158synchronized (sync) {159active = true;160sync.notifyAll();161}162}163164@Override165public void onRead(String line) {166//System.out.println("onRead: " + line);167synchronized (sync) {168lines.add(line);169sync.notifyAll();170}171}172173@Override174public void onFinish() {175synchronized (sync) {176active = false;177sync.notifyAll();178}179}180181@Override182public void onException(Throwable e) {183exception = e;184}185}186}187188189