<commit_before>package main_test

import (
	"encoding/json"
	"fmt"
	"net"
	"net/http"
	"os"
	"os/exec"
	"testing"
	"time"

	"github.com/apcera/nats"
	"github.com/cloudfoundry-incubator/metricz/collector_registrar"
	"github.com/cloudfoundry/gunk/natsrunner"
	"github.com/cloudfoundry/storeadapter/storerunner/etcdstorerunner"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/ginkgomon"
)

func TestRuntimeMetricsServer(t *testing.T) {
	RegisterFailHandler(Fail)
	SetDefaultConsistentlyDuration(time.Second)
	SetDefaultConsistentlyPollingInterval(100 * time.Millisecond)
	SetDefaultEventuallyPollingInterval(100 * time.Millisecond)
	RunSpecs(t, "RuntimeMetricsServer Suite")
}

func NewMetricServer(binPath string, metricsPort, etcdPort, natsPort int) *ginkgomon.Runner {
	return ginkgomon.New(ginkgomon.Config{
		Name:          "metrics",
		AnsiColorCode: "61m",
		Command: exec.Command(
			binPath,
			"-port", fmt.Sprintf("%d", metricsPort),
			"-etcdCluster", fmt.Sprintf("http://127.0.0.1:%d", etcdPort),
			"-natsAddresses", fmt.Sprintf("127.0.0.1:%d", natsPort),
			"-index", "5",
			"-username", "the-username",
			"-password", "<PASSWORD>",
		),
	})
}

var _ = Describe("Main", func() {
	var metricsPort int
	var natsPort int
	var etcdPort int
	var natsRunner *natsrunner.NATSRunner
	var etcdRunner *etcdstorerunner.ETCDClusterRunner
	var metricsServerPath string
	var metricsServer *ginkgomon.Runner
	var metricsProcess ifrit.Process

	BeforeSuite(func() {
		var err error
		metricsServerPath, err = gexec.Build("github.com/cloudfoundry-incubator/runtime-metrics-server")
		Ω(err).ShouldNot(HaveOccurred())
		metricsPort = 5678 + GinkgoParallelNode()
		etcdPort = 5001 + GinkgoParallelNode()
		natsPort = 4228 + GinkgoParallelNode()
	})

	AfterSuite(func() {
		gexec.CleanupBuildArtifacts()
	})

	BeforeEach(func() {
		etcdRunner = etcdstorerunner.NewETCDClusterRunner(etcdPort, 1)
		natsRunner = natsrunner.NewNATSRunner(natsPort)
		metricsServer = NewMetricServer(metricsServerPath, metricsPort, etcdPort, natsPort)
	})

	JustBeforeEach(func() {
		metricsProcess = ifrit.Envoke(metricsServer)
	})

	AfterEach(func() {
		defer func() {
			metricsProcess.Signal(os.Kill)
			Eventually(metricsProcess.Wait(), time.Second).Should(Receive())
		}()

		metricsProcess.Signal(os.Interrupt)
		Eventually(metricsProcess.Wait(), 5*time.Second).Should(Receive(BeNil()))
	})

	Context("When nats is avaialble", func() {
		BeforeEach(func() {
			natsRunner.Start()
			etcdRunner.Start()
		})

		AfterEach(func() {
			natsRunner.Stop()
			etcdRunner.Stop()
		})

		It("reports started", func() {
			Eventually(metricsServer).Should(gbytes.Say("started"))
		})

		Context("and we are subsribed to component announcements", func() {
			var reg collector_registrar.AnnounceComponentMessage
			var receivedAnnounce chan bool

			BeforeEach(func() {
				receivedAnnounce = make(chan bool)
				natsRunner.MessageBus.Subscribe("vcap.component.announce", func(message *nats.Msg) {
					err := json.Unmarshal(message.Data, &reg)
					Ω(err).ShouldNot(HaveOccurred())

					receivedAnnounce <- true
				})
			})

			JustBeforeEach(func() {
				Eventually(receivedAnnounce).Should(Receive())
			})

			It("reports the correct index", func() {<commit_msg>fix data race<commit_after>package main_test

import (
	"encoding/json"
	"fmt"
	"net"
	"net/http"
	"os"
	"os/exec"
	"testing"
	"time"

	"github.com/apcera/nats"
	"github.com/cloudfoundry-incubator/metricz/collector_registrar"
	"github.com/cloudfoundry/gunk/natsrunner"
	"github.com/cloudfoundry/storeadapter/storerunner/etcdstorerunner"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/ginkgomon"
)

func TestRuntimeMetricsServer(t *testing.T) {
	RegisterFailHandler(Fail)
	SetDefaultConsistentlyDuration(time.Second)
	SetDefaultConsistentlyPollingInterval(100 * time.Millisecond)
	SetDefaultEventuallyPollingInterval(100 * time.Millisecond)
	RunSpecs(t, "RuntimeMetricsServer Suite")
}

func NewMetricServer(binPath string, metricsPort, etcdPort, natsPort int) *ginkgomon.Runner {
	return ginkgomon.New(ginkgomon.Config{
		Name:          "metrics",
		AnsiColorCode: "61m",
		Command: exec.Command(
			binPath,
			"-port", fmt.Sprintf("%d", metricsPort),
			"-etcdCluster", fmt.Sprintf("http://127.0.0.1:%d", etcdPort),
			"-natsAddresses", fmt.Sprintf("127.0.0.1:%d", natsPort),
			"-index", "5",
			"-username", "the-username",
			"-password", "<PASSWORD>",
		),
	})
}

var _ = Describe("Main", func() {
	var metricsPort int
	var natsPort int
	var etcdPort int
	var natsRunner *natsrunner.NATSRunner
	var etcdRunner *etcdstorerunner.ETCDClusterRunner
	var metricsServerPath string
	var metricsServer *ginkgomon.Runner
	var metricsProcess ifrit.Process

	BeforeSuite(func() {
		var err error
		metricsServerPath, err = gexec.Build("github.com/cloudfoundry-incubator/runtime-metrics-server")
		Ω(err).ShouldNot(HaveOccurred())
		metricsPort = 5678 + GinkgoParallelNode()
		etcdPort = 5001 + GinkgoParallelNode()
		natsPort = 4228 + GinkgoParallelNode()
	})

	AfterSuite(func() {
		gexec.CleanupBuildArtifacts()
	})

	BeforeEach(func() {
		etcdRunner = etcdstorerunner.NewETCDClusterRunner(etcdPort, 1)
		natsRunner = natsrunner.NewNATSRunner(natsPort)
		metricsServer = NewMetricServer(metricsServerPath, metricsPort, etcdPort, natsPort)
	})

	JustBeforeEach(func() {
		metricsProcess = ifrit.Envoke(metricsServer)
	})

	AfterEach(func() {
		defer func() {
			metricsProcess.Signal(os.Kill)
			Eventually(metricsProcess.Wait(), time.Second).Should(Receive())
		}()

		metricsProcess.Signal(os.Interrupt)
		Eventually(metricsProcess.Wait(), 5*time.Second).Should(Receive(BeNil()))
	})

	Context("When nats is avaialble", func() {
		BeforeEach(func() {
			natsRunner.Start()
			etcdRunner.Start()
		})

		AfterEach(func() {
			natsRunner.Stop()
			etcdRunner.Stop()
		})

		It("reports started", func() {
			Eventually(metricsServer).Should(gbytes.Say("started"))
		})

		Context("and we are subsribed to component announcements", func() {
			var reg collector_registrar.AnnounceComponentMessage
			var announcements <-chan bool

			BeforeEach(func() {
				receivedAnnouncements := make(chan bool)

				announcements = receivedAnnouncements

				natsRunner.MessageBus.Subscribe("vcap.component.announce", func(message *nats.Msg) {
					err := json.Unmarshal(message.Data, &reg)
					Ω(err).ShouldNot(HaveOccurred())

					receivedAnnouncements <- true
				})
			})

			JustBeforeEach(func() {
				Eventually(announcements).Should(Receive())
			})

			It("reports the correct index", func() {
{'fraction_non_alphanumeric': 0.13836849092786546, 'fraction_numerical': 0.010326006785661602, 'mean_word_length': 4.8803122289679095, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '2166240', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2369, 'n_words': 372}