<commit_before>package testutil

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"sync"
	"time"

	"github.com/flynn/flynn/discoverd/client"
	"github.com/flynn/flynn/pkg/attempt"
)

func RunDiscoverdServer(t TestingT, raftPort, httpPort string) (string, func()) {
	killCh := make(chan struct{})
	doneCh := make(chan struct{})

	if raftPort == "" {
		port, err := RandomPort()
		if err != nil {
			t.Fatal("error getting random discoverd raft port: ", err)
		}
		raftPort = port
	}

	if httpPort == "" {
		port, err := RandomPort()
		if err != nil {
			t.Fatal("error getting random discoverd http port: ", err)
		}
		httpPort = port
	}

	// Generate a data directory.
	dataDir, _ := ioutil.TempDir("", "testutil-")

	go func() {
		cmd := exec.Command("discoverd",
			"-host", "127.0.0.1",
			"-raft-addr", "127.0.0.1:"+raftPort,
			"-http-addr", "127.0.0.1:"+httpPort,
			"-dns-addr", "127.0.0.1:0",
			"-data-dir", dataDir,
		)

		var stderr, stdout io.Reader
		if os.Getenv("DEBUG") != "" {
			stderr, _ = cmd.StderrPipe()
			stdout, _ = cmd.StdoutPipe()
		}
		if err := cmd.Start(); err != nil {
			t.Fatal("discoverd start failed: ", err)
			return
		}
		cmdDone := make(chan error)

		go func() {
			if stdout != nil {
				LogOutput("discoverd", stderr, stdout)
			}
			cmdDone <- cmd.Wait()
		}()
		defer close(doneCh)

		select {
		case <-killCh:
			if err := cmd.Process.Kill(); err != nil {
				t.Fatal("failed to kill discoverd:", err)
				return
			}
			err := <-cmdDone
			t.Log("discoverd process exited: ", err)
		case err := <-cmdDone:
			t.Log("discoverd process exited unexpectedly: ", err)
		}
	}()

	// Ping server and wait for leadership.
	if err := waitForLeader(t, "127.0.0.1:"+httpPort, 5*time.Second); err != nil {
		t.Fatal("waiting for leader: %s", err)
	}

	return "127.0.0.1:" + httpPort, func() {
		close(killCh)
		os.RemoveAll(dataDir)
		<-doneCh
	}
}
<commit_msg>discoverd: Fix test log message<commit_after>package testutil

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"sync"
	"time"

	"github.com/flynn/flynn/discoverd/client"
	"github.com/flynn/flynn/pkg/attempt"
)

func RunDiscoverdServer(t TestingT, raftPort, httpPort string) (string, func()) {
	killCh := make(chan struct{})
	doneCh := make(chan struct{})

	if raftPort == "" {
		port, err := RandomPort()
		if err != nil {
			t.Fatal("error getting random discoverd raft port: ", err)
		}
		raftPort = port
	}

	if httpPort == "" {
		port, err := RandomPort()
		if err != nil {
			t.Fatal("error getting random discoverd http port: ", err)
		}
		httpPort = port
	}

	// Generate a data directory.
	dataDir, _ := ioutil.TempDir("", "testutil-")

	go func() {
		cmd := exec.Command("discoverd",
			"-host", "127.0.0.1",
			"-raft-addr", "127.0.0.1:"+raftPort,
			"-http-addr", "127.0.0.1:"+httpPort,
			"-dns-addr", "127.0.0.1:0",
			"-data-dir", dataDir,
		)

		var stderr, stdout io.Reader
		if os.Getenv("DEBUG") != "" {
			stderr, _ = cmd.StderrPipe()
			stdout, _ = cmd.StdoutPipe()
		}
		if err := cmd.Start(); err != nil {
			t.Fatal("discoverd start failed: ", err)
			return
		}
		cmdDone := make(chan error)

		go func() {
			if stdout != nil {
				LogOutput("discoverd", stderr, stdout)
			}
			cmdDone <- cmd.Wait()
		}()
		defer close(doneCh)

		select {
		case <-killCh:
			if err := cmd.Process.Kill(); err != nil {
				t.Fatal("failed to kill discoverd:", err)
				return
			}
			err := <-cmdDone
			t.Log("discoverd process exited: ", err)
		case err := <-cmdDone:
			t.Log("discoverd process exited unexpectedly: ", err)
		}
	}()

	// Ping server and wait for leadership.
	if err := waitForLeader(t, "127.0.0.1:"+httpPort, 5*time.Second); err != nil {
		t.Fatal("waiting for leader: ", err)
	}

	return "127.0.0.1:" + httpPort, func() {
		close(killCh)
		os.RemoveAll(dataDir)
		<-doneCh
	}
}

{'fraction_non_alphanumeric': 0.18503331624807792, 'fraction_numerical': 0.019477191184008202, 'mean_word_length': 3.044559585492228, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, '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': '7295205', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1541, 'n_words': 351}