<commit_before>package availability_test

import (
	"net"
	"time"

	"github.com/pivotal-cf/cf-redis-broker/availability"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func listen(address net.TCPAddr, delayBeforeListening time.Duration, terminate, closed chan struct{}) {
	listener, err := net.ListenTCP("tcp", &address)
	Ω(err).ShouldNot(HaveOccurred())
	go func() {
		select {
		case <-terminate:
			listener.Close()
			close(closed)
		}
	}()
	time.Sleep(delayBeforeListening)
	listener.AcceptTCP()
}

var _ = Describe("waiting for a port to become available", func() {

	var address *net.TCPAddr

	BeforeEach(func() {
		var err error
		address, err = net.ResolveTCPAddr("tcp", "localhost:19000")
		Ω(err).ShouldNot(HaveOccurred())
	})

	Context("when nothing is listening at the specified address", func() {
		It("errors", func() {
			err := availability.Check(address, time.Second*1)
			Ω(err).Should(HaveOccurred())
		})
	})

	Context("when something is already listening at the specified address", func() {
		terminate := make(chan struct{})
		closed := make(chan struct{})

		BeforeEach(func() {
			go func() {
				defer GinkgoRecover()
				listen(*address, 0, terminate, closed)
			}()
		})

		AfterEach(func() {
			close(terminate)
			Eventually(closed).Should(BeClosed())
		})

		It("does not error", func() {
			err := availability.Check(address, time.Second*1)
			Ω(err).ShouldNot(HaveOccurred())
		})
	})

	Context("when something begins to listen on address before timeout is up", func() {
		terminate := make(chan struct{})
		closed := make(chan struct{})

		BeforeEach(func() {
			go func() {
				defer GinkgoRecover()

				listen(*address, 500*time.Millisecond, terminate, closed)
			}()
		})

		AfterEach(func() {
			close(terminate)
			Eventually(closed).Should(BeClosed())
		})

		It("does not error", func() {
			err := availability.Check(address, time.Second*1)
			Ω(err).ShouldNot(HaveOccurred())
		})
	})
})<commit_msg>use consistent test language<commit_after>package availability_test

import (
	"net"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/pivotal-cf/cf-redis-broker/availability"
)

func listen(address net.TCPAddr, delayBeforeListening time.Duration, terminate, closed chan struct{}) {
	listener, err := net.ListenTCP("tcp", &address)
	Expect(err).NotTo(HaveOccurred())
	go func() {
		select {
		case <-terminate:
			listener.Close()
			close(closed)
		}
	}()
	time.Sleep(delayBeforeListening)
	listener.AcceptTCP()
}

var _ = Describe("waiting for a port to become available", func() {
	var address *net.TCPAddr

	BeforeEach(func() {
		var err error
		address, err = net.ResolveTCPAddr("tcp", "localhost:19000")
		Expect(err).NotTo(HaveOccurred())
	})

	Context("when nothing is listening at the specified address", func() {
		It("errors", func() {
			err := availability.Check(address, time.Second*1)
			Expect(err).To(HaveOccurred())
		})
	})

	Context("when something is already listening at the specified address", func() {
		terminate := make(chan struct{})
		closed := make(chan struct{})

		BeforeEach(func() {
			go func() {
				defer GinkgoRecover()
				listen(*address, 0, terminate, closed)
			}()
		})

		AfterEach(func() {
			close(terminate)
			Eventually(closed).Should(BeClosed())
		})

		It("does not error", func() {
			err := availability.Check(address, time.Second*1)
			Expect(err).NotTo(HaveOccurred())
		})
	})

	Context("when something begins to listen on address before timeout is up", func() {
		terminate := make(chan struct{})
		closed := make(chan struct{})

		BeforeEach(func() {
			go func() {
				defer GinkgoRecover()

				listen(*address, 500*time.Millisecond, terminate, closed)
			}()
		})

		AfterEach(func() {
			close(terminate)
			Eventually(closed).Should(BeClosed())
		})

		It("does not error", func() {
			err := availability.Check(address, time.Second*1)
			Expect(err).NotTo(HaveOccurred())
		})
	})
})
{'fraction_non_alphanumeric': 0.15822945815314168, 'fraction_numerical': 0.006105316713304503, 'mean_word_length': 4.207947019867549, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6406832', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1296, 'n_words': 285}