<commit_before>package natsio
import (

	gDoc "github.com/fsouza/go-dockerclient"
	"os"
	"testing"
	"time"
)

type TestData struct {
	Message string
}

var (
	dockCli *gDoc.Client
	natsContainer *gDoc.Container
)


func setup(dockCli *gDoc.Client) *gDoc.Container {
	err := dockCli.PullImage(gDoc.PullImageOptions{Repository: "nats", OutputStream: os.Stdout}, gDoc.AuthConfiguration{})
	if err != nil {
		panic("Failed to pull nats image:" + err.Error())
	}
	con, err := dockCli.CreateContainer(gDoc.CreateContainerOptions{
		Config: &gDoc.Config{
			Image: "nats",
		},
		HostConfig: &gDoc.HostConfig{
			PortBindings: map[gDoc.Port][]gDoc.PortBinding{
				"4222/tcp" : []gDoc.PortBinding{
					gDoc.PortBinding{
						HostIP: "127.0.0.1",
						HostPort: "4222",
					},
				},
			},
		},
	})
	if err != nil {
		panic("Failed to create nats container:" + err.Error())
	}

	if err := dockCli.StartContainer(con.ID, nil); err != nil {
		panic("Failed to start nats container:" + err.Error())
	}

	return con
}

func teardown() {
	err := dockCli.RemoveContainer(gDoc.RemoveContainerOptions{
		Force: true,
		ID: natsContainer.ID,
	})
	if err != nil {
		panic("Failed to remove nats container:" + err.Error())
	}
}

func TestMain(m *testing.M) {
	// your func

	var err error

	dockCli, err = gDoc.NewClientFromEnv()
	if err != nil {
		panic("Failed to connect to docker:" + err.Error())
	}

	natsContainer = setup(dockCli)

	retCode := m.Run()

	teardown()
	// call with result of m.Run()
	os.Exit(retCode)
}

func TestNewNatsConnect(t *testing.T) {
	natsOpts := NewNats(func(n *Nats) error {
		n.Url = "nats://localhost:4222"
		return nil
	})

	var handler = func(testData *TestData) {
		t.Logf("Got message on nats: %+v", testData)
	}

	natsOpts.HandleFunc("test.a", handler)

	err := natsOpts.ListenAndServe()
	if err != nil {
		teardown()
		t.Error("Failed to connect:" + err.Error())
	}

	response := TestData{}
	err = natsOpts.GetEncCon().Request("test.a", TestData{"Ping"}, response, 2 * time.Second)
	if err != nil {
		teardown()
		t.Error("Failed to get response:" + err.Error())<commit_msg>Starting to write IT test for nats.<commit_after>package natsio
import (

	gDoc "github.com/fsouza/go-dockerclient"
	"os"
	"testing"
	"time"
)

type TestData struct {
	Message string
}

const (
	NatsImage = "nats"
	NatsPort = "4222"
)

var (
	dockCli *gDoc.Client
	natsContainer *gDoc.Container
)


func setup(dockCli *gDoc.Client) *gDoc.Container {
	if err := dockCli.PullImage(gDoc.PullImageOptions{Repository: NatsImage, OutputStream: os.Stdout}, gDoc.AuthConfiguration{}); err != nil {
		panic("Failed to pull nats image:" + err.Error())
	}

	con, err := dockCli.CreateContainer(gDoc.CreateContainerOptions{
		Config: &gDoc.Config{
			Image: NatsImage,
		},
		HostConfig: &gDoc.HostConfig{
			PortBindings: map[gDoc.Port][]gDoc.PortBinding{
				NatsPort + "/tcp" : []gDoc.PortBinding{
					gDoc.PortBinding{HostIP: "127.0.0.1", HostPort: NatsPort},
				},
			},
		},
	})
	if err != nil {
		panic("Failed to create nats container:" + err.Error())
	}

	if err := dockCli.StartContainer(con.ID, nil); err != nil {
		panic("Failed to start nats container:" + err.Error())
	}

	return con
}

func teardown() {
	err := dockCli.RemoveContainer(gDoc.RemoveContainerOptions{
		Force: true,
		ID: natsContainer.ID,
	})
	if err != nil {
		panic("Failed to remove nats container:" + err.Error())
	}
}

func TestMain(m *testing.M) {
	// your func

	var err error

	dockCli, err = gDoc.NewClientFromEnv()
	if err != nil {
		panic("Failed to connect to docker:" + err.Error())
	}

	natsContainer = setup(dockCli)

	retCode := m.Run()

	teardown()
	// call with result of m.Run()
	os.Exit(retCode)
}

func TestNewNatsConnect(t *testing.T) {
	natsOpts := NewNats(func(n *Nats) error {
		n.Url = "nats://localhost:" + NatsPort
		return nil
	})

	var handler = func(testData *TestData) {
		t.Logf("Got message on nats: %+v", testData)
	}

	natsOpts.HandleFunc("test.a", handler)

	err := natsOpts.ListenAndServe()
	if err != nil {
		teardown()
		t.Error("Failed to connect:" + err.Error())
	}

	response := TestData{}
	err = natsOpts.GetEncCon().Request("test.a", TestData{"Ping"}, response, 2 * time.Second)
	if err != nil {
		teardown()
		t.Error("Failed to get response:" + err.Error())
{'fraction_non_alphanumeric': 0.15796864029955535, 'fraction_numerical': 0.007020828457758015, 'mean_word_length': 3.851305334846765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1517969', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1595, 'n_words': 363}