Package com.softwaremill.common.test.util

Source Code of com.softwaremill.common.test.util.FileMessageWaiter

package com.softwaremill.common.test.util;

import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

public class FileMessageWaiter {
    private final File file;

    public FileMessageWaiter(File file) {
        this.file = file;
    }
   
    public void waitFor(final String message, long waitForMillis) {
        final AtomicBoolean found = new AtomicBoolean();
       
        Tailer tailer = new Tailer(file, new TailerListenerAdapter() {
            private Tailer tailer;

            @Override
            public void init(Tailer tailer) {
                this.tailer = tailer;
                System.out.println("Waiting for message: [" + message + "]");
            }

            @Override
            public void handle(String line) {
                if (line.contains(message)) {
                    found.set(true);
                    tailer.stop();
                }
            }
        }, 500);

        Thread tailerThread = new Thread(tailer);
        tailerThread.start();
        try {
            tailerThread.join(waitForMillis);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (!found.get()) {
            throw new RuntimeException("Message: "+message+" not found in file: "+file.getAbsolutePath()+" during "+
                    waitForMillis+"ms");
        }
    }
}
TOP

Related Classes of com.softwaremill.common.test.util.FileMessageWaiter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.