Skip to content

Commit

Permalink
refactor(server): remove Wait function from Listener interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gsalomao committed Apr 23, 2023
1 parent b66e2b7 commit d242caf
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 57 deletions.
9 changes: 0 additions & 9 deletions internal/cli/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ import (
type listenerMock struct {
mock.Mock
running chan struct{}
stopped chan struct{}
}

func newListenerMock() *listenerMock {
return &listenerMock{
running: make(chan struct{}, 1),
stopped: make(chan struct{}, 1),
}
}

Expand All @@ -48,12 +46,6 @@ func (l *listenerMock) Start() error {

func (l *listenerMock) Stop() {
l.Called()
l.stopped <- struct{}{}
}

func (l *listenerMock) Wait() {
l.Called()
<-l.stopped
}

func TestCLIRunStartLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -88,7 +80,6 @@ func TestCLIRunStartStartStopServer(t *testing.T) {
s.AddListener(l)

l.On("Start").Return(nil)
l.On("Wait")
l.On("Stop")

var wg sync.WaitGroup
Expand Down
4 changes: 0 additions & 4 deletions internal/metrics/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,3 @@ func (l *Listener) Stop() {
l.wg.Wait()
l.log.Debug().Msg("Listener stopped with success")
}

func (l *Listener) Wait() {
l.wg.Wait()
}
12 changes: 0 additions & 12 deletions internal/metrics/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,5 @@ func TestListenerStartAndStop(t *testing.T) {
err = l.Start()
require.Nil(t, err)

var wg sync.WaitGroup
starting := make(chan struct{})

wg.Add(1)
go func() {
defer wg.Done()
close(starting)
l.Wait()
}()

<-starting
l.Stop()
wg.Wait()
}
6 changes: 1 addition & 5 deletions internal/mqtt/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (l *Listener) Start() error {
return nil
}

// Stop stops the MQTT listener. Once called, it unblocks the Run function.
// Stop stops the MQTT listener.
func (l *Listener) Stop() {
l.log.Debug().Msg("Stopping listener")

Expand All @@ -124,7 +124,3 @@ func (l *Listener) Stop() {
l.wg.Wait()
l.log.Debug().Msg("Listener stopped with success")
}

func (l *Listener) Wait() {
l.wg.Wait()
}
13 changes: 0 additions & 13 deletions internal/mqtt/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,7 @@ func TestListenerStartAndStop(t *testing.T) {
err = l.Start()
require.Nil(t, err)

var wg sync.WaitGroup
starting := make(chan struct{})

wg.Add(1)
go func() {
defer wg.Done()
close(starting)
l.Wait()
}()

<-starting
l.Stop()
wg.Wait()
}

func TestListenerHandleConnection(t *testing.T) {
Expand All @@ -123,5 +111,4 @@ func TestListenerHandleConnection(t *testing.T) {

_ = conn.Close()
l.Stop()
l.Wait()
}
9 changes: 2 additions & 7 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ import (

// Listener is an interface for network listeners.
type Listener interface {
// Start starts the listener. If the listener has already started, this function has no effect.
// Start starts the listener.
Start() error

// Stop stops the listener unblocking the Wait function. If the listener has not started, this function has no
// effect.
// Stop stops the listener.
Stop()

// Wait blocks until the listener stops. If the listener has not started, this function returns immediately.
Wait()
}

type serverListener struct {
Expand Down Expand Up @@ -102,7 +98,6 @@ func (s *Server) stopRunningListeners() {
for _, lsn := range s.listeners {
if lsn.running {
lsn.listener.Stop()
lsn.listener.Wait()
lsn.running = false
s.wg.Done()
}
Expand Down
7 changes: 0 additions & 7 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ func (l *listenerMock) Stop() {
l.wg.Done()
}

func (l *listenerMock) Wait() {
l.Called()
l.wg.Wait()
}

func newLogger() *logger.Logger {
out := bytes.NewBufferString("")
return logger.New(out, nil, logger.Json)
Expand Down Expand Up @@ -85,7 +80,6 @@ func TestServerStop(t *testing.T) {
require.Nil(t, err)

l.On("Stop")
l.On("Wait")
s.Stop()
l.AssertExpectations(t)
}
Expand All @@ -109,7 +103,6 @@ func TestServerStartWithListenerErrorMultipleListeners(t *testing.T) {

l1 := &listenerMock{}
l1.On("Start").Return(nil)
l1.On("Wait")
l1.On("Stop")
s.AddListener(l1)

Expand Down

0 comments on commit d242caf

Please sign in to comment.