<commit_before>package db

import (
	"fmt"
	"net"
	"os"

	"go.skia.org/infra/go/trace/service"
	"go.skia.org/infra/go/util"
	"go.skia.org/infra/perf/go/types"
	"google.golang.org/grpc"
)

const (
	FILENAME = "/tmp/tracedb_test.db"
)

type fatalf func(format string, args ...interface{})
type cleanup func()

// setupClientServerForTesting is a utility func used in tests.
//
// It starts a server running in a Go routine and then connects
// a client to that server, which is returned as DB. In addition it
// returns a func that should be called to clean up both the client
// and the server after they are done being used.
func setupClientServerForTesting(f fatalf) (DB, cleanup) {
	// First spin up a traceservice server that we will talk to.
	server, err := traceservice.NewTraceServiceServer(FILENAME)
	if err != nil {
		f("Failed to initialize the tracestore server: %s", err)
	}

	// Start the server on an open port.
	lis, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		f("failed to listen: %v", err)
	}
	port := lis.Addr().String()
	s := grpc.NewServer()
	traceservice.RegisterTraceServiceServer(s, server)
	go func() {
		f("Failed while serving: %s", s.Serve(lis))
	}()

	// Set up a connection to the server.
	conn, err := grpc.Dial(port, grpc.WithInsecure())
	if err != nil {
		f("did not connect: %v", err)
	}
	ts, err := NewTraceServiceDB(conn, types.PerfTraceBuilder)
	if err != nil {
		f("Failed to create tracedb.DB: %s", err)
	}
	cl := func() {
		util.Close(conn)
		util.Close(ts)
		if err := os.Remove(FILENAME); err != nil {
			fmt.Printf("Failed to clean up %s: %s", FILENAME, err)
		}
	}
	return ts, cl
}<commit_msg>Fix file contention in TestTileFromCommits<commit_after>package db

import (
	"fmt"
	"io/ioutil"
	"net"
	"os"

	"go.skia.org/infra/go/trace/service"
	"go.skia.org/infra/go/util"
	"go.skia.org/infra/perf/go/types"
	"google.golang.org/grpc"
)

const (
	TMP_PREFIX = "tracedb_test"
)

type fatalf func(format string, args ...interface{})
type cleanup func()

// setupClientServerForTesting is a utility func used in tests.
//
// It starts a server running in a Go routine and then connects
// a client to that server, which is returned as DB. In addition it
// returns a func that should be called to clean up both the client
// and the server after they are done being used.
func setupClientServerForTesting(f fatalf) (DB, cleanup) {
	file, err := ioutil.TempFile("", TMP_PREFIX)
	if err != nil {
		f("Could not create temporary file: %s", err)
	}
	// First spin up a traceservice server that we will talk to.
	server, err := traceservice.NewTraceServiceServer(file.Name())
	if err != nil {
		f("Failed to initialize the tracestore server: %s", err)
	}

	// Start the server on an open port.
	lis, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		f("failed to listen: %v", err)
	}
	port := lis.Addr().String()
	s := grpc.NewServer()
	traceservice.RegisterTraceServiceServer(s, server)
	go func() {
		f("Failed while serving: %s", s.Serve(lis))
	}()

	// Set up a connection to the server.
	conn, err := grpc.Dial(port, grpc.WithInsecure())
	if err != nil {
		f("did not connect: %v", err)
	}
	ts, err := NewTraceServiceDB(conn, types.PerfTraceBuilder)
	if err != nil {
		f("Failed to create tracedb.DB: %s", err)
	}
	cl := func() {
		util.Close(conn)
		util.Close(ts)
		if err := os.Remove(file.Name()); err != nil {
			fmt.Printf("Failed to clean up %s: %s", file.Name(), err)
		}
	}
	return ts, cl
}
{'fraction_non_alphanumeric': 0.14426134721017636, 'fraction_numerical': 0.0005782017924255566, 'mean_word_length': 3.6380697050938338, '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': '2181599', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1225, 'n_words': 401}