<commit_before>package main

import (
	"errors"
	"io"
	"testing"

	"github.com/flynn/lorne/types"
	"github.com/flynn/sampi/types"
	"github.com/technoweenie/grohl"
	"github.com/titanous/go-dockerclient"
)

type nullLogger struct{}

func (nullLogger) Log(grohl.Data) error { return nil }

func init() { grohl.SetLogger(nullLogger{}) }

type dockerClient struct {
	created *docker.Config
	started bool
}

func (c *dockerClient) CreateContainer(config *docker.Config) (*docker.Container, error) {
	c.created = config
	return &docker.Container{ID: "asdf"}, nil
}

func (c *dockerClient) StartContainer(id string, config *docker.HostConfig) error {
	if id != "asdf" {
		return errors.New("Invalid ID")
	}
	c.started = true
	return nil
}

func (c *dockerClient) PullImage(opts docker.PullImageOptions, w io.Writer) error {
	return nil
}

func TestProcessJobs(t *testing.T) {
	jobs := make(chan *sampi.Job)
	done := make(chan struct{})
	client := &dockerClient{}
	state := NewState()
	go func() {
		processJobs(jobs, "", client, state)
		close(done)
	}()
	job := &sampi.Job{ID: "a", Config: &docker.Config{}}
	jobs <- job
	close(jobs)
	<-done

	if client.created != job.Config {
		t.Error("job not created")
	}
	if !client.started {
		t.Error("job not started")
	}
	sjob := state.GetJob("a")
	if sjob == nil || sjob.StartedAt.IsZero() || sjob.Status != lorne.StatusRunning || sjob.ContainerID != "asdf" {
		t.Error("incorrect state")
	}
}<commit_msg>host: Refactor tests to use helper<commit_after>package main

import (
	"errors"
	"io"
	"testing"

	"github.com/flynn/lorne/types"
	"github.com/flynn/sampi/types"
	"github.com/technoweenie/grohl"
	"github.com/titanous/go-dockerclient"
)

type nullLogger struct{}

func (nullLogger) Log(grohl.Data) error { return nil }

func init() { grohl.SetLogger(nullLogger{}) }

type dockerClient struct {
	created *docker.Config
	started bool
}

func (c *dockerClient) CreateContainer(config *docker.Config) (*docker.Container, error) {
	c.created = config
	return &docker.Container{ID: "asdf"}, nil
}

func (c *dockerClient) StartContainer(id string, config *docker.HostConfig) error {
	if id != "asdf" {
		return errors.New("Invalid ID")
	}
	c.started = true
	return nil
}

func (c *dockerClient) PullImage(opts docker.PullImageOptions, w io.Writer) error {
	return nil
}

func testProcessWith(job *sampi.Job, t *testing.T) (*State, *dockerClient) {
	jobs := make(chan *sampi.Job)
	done := make(chan struct{})
	client := &dockerClient{}
	state := NewState()
	go func() {
		processJobs(jobs, "", client, state)
		close(done)
	}()
	jobs <- job
	close(jobs)
	<-done

	if client.created != job.Config {
		t.Error("job not created")
	}
	if !client.started {
		t.Error("job not started")
	}
	sjob := state.GetJob("a")
	if sjob == nil || sjob.StartedAt.IsZero() || sjob.Status != lorne.StatusRunning || sjob.ContainerID != "asdf" {
		t.Error("incorrect state")
	}

	return state, client
}

func TestProcessJob(t *testing.T) {
	testProcessWith(&sampi.Job{ID: "a", Config: &docker.Config{}}, t)
}
{'fraction_non_alphanumeric': 0.1586092715231788, 'fraction_numerical': 0.0, 'mean_word_length': 4.4728260869565215, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4762240', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1109, 'n_words': 257}