<commit_before>// +build integration

package mtail_test

import (
	"os"
	"path"
	"syscall"
	"testing"
	"time"

	"github.com/golang/glog"
	"github.com/google/mtail/internal/mtail"
)

func TestReadFromPipe(t *testing.T) {
	tmpDir, rmTmpDir := mtail.TestTempDir(t)
	defer rmTmpDir()

	logDir := path.Join(tmpDir, "logs")
	progDir := path.Join(tmpDir, "progs")
	err := os.Mkdir(logDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.Mkdir(progDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	defer mtail.TestChdir(t, logDir)()

	logFile := path.Join(logDir, "logpipe")

	err = syscall.Mkfifo(logFile, 0600)
	if err != nil {
		t.Fatal(err)
	}
	time.Sleep(time.Second)

	f, err := os.OpenFile(logFile, os.O_RDWR|syscall.O_NONBLOCK, 0600)
	if err != nil {
		t.Fatal(err)
	}
	time.Sleep(time.Second)

	m, stopM := mtail.TestStartServer(t, 0, false, mtail.LogPathPatterns(logDir+"/*"), mtail.ProgramPath(progDir))
	defer stopM()

	startLineCount := mtail.TestGetMetric(t, m.Addr(), "line_count")

	time.Sleep(1 * time.Second)

	n, err := f.WriteString("1\n2\n3\n")
	if err != nil {
		t.Fatal(err)
	}
	glog.Infof("Wrote %d bytes", n)
	time.Sleep(1 * time.Second)

	endLineCount := mtail.TestGetMetric(t, m.Addr(), "line_count")

	lineCount := endLineCount.(float64) - startLineCount.(float64)
	if lineCount != 3. {
		t.Errorf("output didn't have expected line count increase: want 3 got %#v", lineCount)
	}

	// TODO(jaq): Use f.SetDeadline once we stop using afero.
	err = f.Close()
	if err != nil {
		t.Fatal(err)
	}
}<commit_msg>Open the named pipe after startserver to avoid a hang waiting for the mtail server.<commit_after>// +build integration

package mtail_test

import (
	"os"
	"path"
	"syscall"
	"testing"
	"time"

	"github.com/golang/glog"
	"github.com/google/mtail/internal/mtail"
)

func TestReadFromPipe(t *testing.T) {
	tmpDir, rmTmpDir := mtail.TestTempDir(t)
	defer rmTmpDir()

	logDir := path.Join(tmpDir, "logs")
	progDir := path.Join(tmpDir, "progs")
	err := os.Mkdir(logDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	err = os.Mkdir(progDir, 0700)
	if err != nil {
		t.Fatal(err)
	}
	defer mtail.TestChdir(t, logDir)()

	logFile := path.Join(logDir, "logpipe")

	err = syscall.Mkfifo(logFile, 0600)
	if err != nil {
		t.Fatal(err)
	}
	time.Sleep(time.Second)

	m, stopM := mtail.TestStartServer(t, 0, false, mtail.LogPathPatterns(logDir+"/*"), mtail.ProgramPath(progDir))

	defer stopM()

	// TODO(jaq): Want to open the file before the startserver to the pipe
	// exists before start, but that hangs the server.
	f, err := os.OpenFile(logFile, os.O_RDWR|syscall.O_NONBLOCK, 0600)
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		// TODO(jaq): Use f.SetDeadline once we stop using afero.
		err = f.Close()
		if err != nil {
			t.Fatal(err)
		}
	}()

	time.Sleep(time.Second)

	startLineCount := mtail.TestGetMetric(t, m.Addr(), "line_count")
	time.Sleep(1 * time.Second)

	n, err := f.WriteString("1\n2\n3\n")
	if err != nil {
		t.Fatal(err)
	}
	glog.Infof("Wrote %d bytes", n)
	time.Sleep(1 * time.Second)

	endLineCount := mtail.TestGetMetric(t, m.Addr(), "line_count")
	lineCount := endLineCount.(float64) - startLineCount.(float64)
	if lineCount != 3. {
		t.Errorf("output didn't have expected line count increase: want 3 got %#v", lineCount)
	}
}
{'fraction_non_alphanumeric': 0.16457632303456715, 'fraction_numerical': 0.017130620985010708, 'mean_word_length': 3.9470499243570347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5441150', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1336, 'n_words': 294}