Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
No need for optional function parameters
Browse files Browse the repository at this point in the history
These were only used for testing. We propose using a Stop() method since
it is cleaner.

Signed-off-by: Mike Gehard <mgehard@pivotal.io>
  • Loading branch information
John Tuley authored and Mike Gehard committed Oct 2, 2014
1 parent 8dbdbfd commit ee7e78b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 12 additions & 10 deletions cfcomponent/registrars/collectorregistrar/collector_registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,34 @@ import (

type ClientProvider func(*gosteno.Logger, *cfcomponent.Config) (yagnats.NATSConn, error)

type CollectorRegistrar interface {
Run(stopChan <-chan struct{})
}

type collectorRegistrar struct {
type CollectorRegistrar struct {
clientProvider ClientProvider
interval time.Duration
logger *gosteno.Logger
cfc cfcomponent.Component
client yagnats.NATSConn
config *cfcomponent.Config
stopChan chan struct{}
}

func NewCollectorRegistrar(clientProvider ClientProvider, cfc cfcomponent.Component, interval time.Duration, config *cfcomponent.Config) CollectorRegistrar {
return &collectorRegistrar{
func NewCollectorRegistrar(clientProvider ClientProvider, cfc cfcomponent.Component, interval time.Duration, config *cfcomponent.Config) *CollectorRegistrar {
return &CollectorRegistrar{
clientProvider: clientProvider,
logger: cfc.Logger,
cfc: cfc,
interval: interval,
config: config,
stopChan: make(chan struct{}),
}
}

func (registrar *collectorRegistrar) Run(stopChan <-chan struct{}) {
func (registrar *CollectorRegistrar) Run() {
ticker := time.NewTicker(registrar.interval)
defer ticker.Stop()

for {
select {
case <-stopChan:
case <-registrar.stopChan:
return
case <-ticker.C:
err := registrar.announceMessage()
Expand All @@ -56,7 +54,11 @@ func (registrar *collectorRegistrar) Run(stopChan <-chan struct{}) {
}
}

func (registrar *collectorRegistrar) announceMessage() error {
func (registrar *CollectorRegistrar) Stop() {
close(registrar.stopChan)
}

func (registrar *CollectorRegistrar) announceMessage() error {
if registrar.client == nil {
registrar.logger.Debugf("creating NATS client")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ var _ = Describe("Collectorregistrar", func() {
var (
fakeClient *fakeClient
component cfcomponent.Component
registrar collectorregistrar.CollectorRegistrar
stopChan chan struct{}
registrar *collectorregistrar.CollectorRegistrar
doneChan chan struct{}
errorProvider func() error
fakeClientProviderCallCount int32
Expand All @@ -42,17 +41,16 @@ var _ = Describe("Collectorregistrar", func() {
return fakeClient, errorProvider()
}
registrar = collectorregistrar.NewCollectorRegistrar(fakeClientProvider, component, 10*time.Millisecond, nil)
stopChan = make(chan struct{})
doneChan = make(chan struct{})

go func() {
defer close(doneChan)
registrar.Run(stopChan)
registrar.Run()
}()
})

AfterEach(func() {
close(stopChan)
registrar.Stop()
Eventually(doneChan).Should(BeClosed())
})

Expand Down

0 comments on commit ee7e78b

Please sign in to comment.