<commit_before>package main

import (
	"testing"
	"time"

	"github.com/newrelic/bosun/services_state"
	. "github.com/smartystreets/goconvey/convey"
)

func Test_GetBroadcasts(t *testing.T) {
	Convey("When handing back broadcast messages", t, func() {
		state    := services_state.NewServicesState()
		delegate := NewServicesDelegate(state)
		bCast    := [][]byte{
			[]byte(`{"ID":"d419fa7ad1a7","Name":"/dockercon-6adfe629eebc91","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
			[]byte(`{"ID":"deadbeefabba","Name":"/dockercon-6c01869525db08","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
		}
		bCast2   := [][]byte{
			[]byte(`{"ID":"1b3295bf300f","Name":"/romantic_brown","Image":"0415448f2cc2","Created":"2014-10-02T23:58:48Z","Hostname":"docker1","Ports":[{"Type":"tcp","Port":9494}],"Updated":"2015-03-04T01:12:32.630357657Z","Status":0}`),
			[]byte(`{"ID":"deadbeefabba","Name":"/dockercon-6c01869525db08","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
		}

		Convey("GetBroadcasts()", func() {
			Convey("Returns nil when there is nothing to send", func() {
				So(delegate.GetBroadcasts(3, 1398), ShouldBeNil)
			})

			Convey("Returns from the pending list when nothing in the channel", func() {
				data := []byte("data")
				delegate.pendingBroadcasts = [][]byte{data}

				result := delegate.GetBroadcasts(3, 1398)
				So(string(result[0]), ShouldEqual, string(data)) 
				So(len(result), ShouldEqual, 1)
			})

			Convey("Returns what's in the channel", func() {
				go func() {
					state.Broadcasts <-bCast
				}()
				// There has got to be a better way to do this than to sleep here
				// TODO figure that out
				time.Sleep(2 * time.Millisecond)

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 2)
				So(string(result[0]), ShouldEqual, string(bCast[0]))
				So(string(result[1]), ShouldEqual, string(bCast[1]))
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})

			Convey("Returns what's left when nothing is new", func() {
				delegate.pendingBroadcasts = bCast

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 2)
				So(string(result[0]), ShouldEqual, string(bCast[0]))
				So(string(result[1]), ShouldEqual, string(bCast[1]))
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})

			Convey("Returns what's left and what's new when it fits", func() {
				delegate.pendingBroadcasts = bCast
				go func() {
					state.Broadcasts <-bCast2
				}()
				// There has got to be a better way to do this than to sleep here
				// TODO figure that out
				time.Sleep(2 * time.Millisecond)

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 4)
				for i, entry := range(append(bCast2, bCast...)) {
					So(string(result[i]), ShouldEqual, string(entry))
				}
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})

			Convey("Many runs with leftovers don't leave junk or bad buffers", func() {
				delegate.pendingBroadcasts = bCast
				go func() {
					state.Broadcasts <-append(bCast2, bCast...)
				}()
				// There has got to be a better way to do this than to sleep here
				// TODO figure that out
				time.Sleep(2 * time.Millisecond)

				delegate.GetBroadcasts(3, 100)
				delegate.GetBroadcasts(3, 300) // 1 message fits here
				delegate.GetBroadcasts(3, 100)

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 5)
				for i, entry := range(append(bCast2[1:], bCast...)) {
					So(string(result[i]), ShouldEqual, string(entry))
				}
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})
		})
	})
}<commit_msg>Use a buffered channel in the tests rather than crummy Sleep.<commit_after>package main

import (
	"testing"

	"github.com/newrelic/bosun/services_state"
	. "github.com/smartystreets/goconvey/convey"
)

func Test_GetBroadcasts(t *testing.T) {
	Convey("When handing back broadcast messages", t, func() {
		state    := services_state.NewServicesState()
		delegate := NewServicesDelegate(state)
		bCast    := [][]byte{
			[]byte(`{"ID":"d419fa7ad1a7","Name":"/dockercon-6adfe629eebc91","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
			[]byte(`{"ID":"deadbeefabba","Name":"/dockercon-6c01869525db08","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
		}
		bCast2   := [][]byte{
			[]byte(`{"ID":"1b3295bf300f","Name":"/romantic_brown","Image":"0415448f2cc2","Created":"2014-10-02T23:58:48Z","Hostname":"docker1","Ports":[{"Type":"tcp","Port":9494}],"Updated":"2015-03-04T01:12:32.630357657Z","Status":0}`),
			[]byte(`{"ID":"deadbeefabba","Name":"/dockercon-6c01869525db08","Image":"nginx:latest","Created":"2015-02-25T19:04:46Z","Hostname":"docker2","Ports":[{"Type":"tcp","Port":10234}],"Updated":"2015-03-04T01:12:46.669648453Z","Status":0}`),
		}

		Convey("GetBroadcasts()", func() {
			Convey("Returns nil when there is nothing to send", func() {
				So(delegate.GetBroadcasts(3, 1398), ShouldBeNil)
			})

			Convey("Returns from the pending list when nothing in the channel", func() {
				data := []byte("data")
				delegate.pendingBroadcasts = [][]byte{data}

				result := delegate.GetBroadcasts(3, 1398)
				So(string(result[0]), ShouldEqual, string(data)) 
				So(len(result), ShouldEqual, 1)
			})

			Convey("Returns what's in the channel", func() {
				state.Broadcasts = make(chan [][]byte, 1)
				state.Broadcasts <-bCast
				result := delegate.GetBroadcasts(3, 1398)

				So(len(result), ShouldEqual, 2)
				So(string(result[0]), ShouldEqual, string(bCast[0]))
				So(string(result[1]), ShouldEqual, string(bCast[1]))
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})

			Convey("Returns what's left when nothing is new", func() {
				delegate.pendingBroadcasts = bCast

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 2)
				So(string(result[0]), ShouldEqual, string(bCast[0]))
				So(string(result[1]), ShouldEqual, string(bCast[1]))
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})

			Convey("Returns what's left and what's new when it fits", func() {
				state.Broadcasts = make(chan [][]byte, 1)
				delegate.pendingBroadcasts = bCast
				state.Broadcasts <-bCast2

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 4)
				for i, entry := range(append(bCast2, bCast...)) {
					So(string(result[i]), ShouldEqual, string(entry))
				}
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})

			Convey("Many runs with leftovers don't leave junk or bad buffers", func() {
				state.Broadcasts = make(chan [][]byte, 1)
				delegate.pendingBroadcasts = bCast
				state.Broadcasts <-append(bCast2, bCast...)

				delegate.GetBroadcasts(3, 100)
				delegate.GetBroadcasts(3, 300) // 1 message fits here
				delegate.GetBroadcasts(3, 100)

				result := delegate.GetBroadcasts(3, 1398)
				So(len(result), ShouldEqual, 5)
				for i, entry := range(append(bCast2[1:], bCast...)) {
					So(string(result[i]), ShouldEqual, string(entry))
				}
				So(len(delegate.pendingBroadcasts), ShouldEqual, 0)
			})
		})
	})
}
{'fraction_non_alphanumeric': 0.19054478784704035, 'fraction_numerical': 0.07752750130958617, 'mean_word_length': 4.870099923136049, 'pattern_counts': {'":': 80, '<': 9, '<?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': '3796651', 'n_tokens_mistral': 3432, 'n_tokens_neox': 2631, 'n_words': 468}