Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev' into feat/mdt
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyweight87 committed Oct 27, 2024
2 parents 8fac6e3 + b0c682b commit 8ac366d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type ServiceInterface interface {
// shutdown the service
Shutdown()

// return if the service is running
IsRunning() bool

// add a use case to the service
AddUseCase(useCase UseCaseInterface)

Expand Down
45 changes: 45 additions & 0 deletions mocks/ServiceInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 33 additions & 5 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ type Service struct {
// defines wether a user interaction to accept pairing is possible
isPairingPossible bool

startOnce sync.Once
// return if the service is running
isRunning bool

mux sync.Mutex
mux sync.Mutex
muxRunning sync.Mutex
}

// creates a new EEBUS service
Expand Down Expand Up @@ -153,15 +155,41 @@ func (s *Service) Setup() error {

// Starts the service
func (s *Service) Start() {
s.startOnce.Do(func() {
s.connectionsHub.Start()
})
s.muxRunning.Lock()
defer s.muxRunning.Unlock()

// make sure we do not start twice while the service is already running
if s.isRunning {
return
}

s.connectionsHub.Start()

s.isRunning = true
}

// Shutdown all services and stop the server.
func (s *Service) Shutdown() {
s.muxRunning.Lock()
defer s.muxRunning.Unlock()

// if the service is not running, we do not need to shut it down
if !s.isRunning {
return
}

// Shut down all running connections
s.connectionsHub.Shutdown()

s.isRunning = false
}

// return if the service is running
func (s *Service) IsRunning() bool {
s.muxRunning.Lock()
defer s.muxRunning.Unlock()

return s.isRunning
}

// add a use case to the service
Expand Down
13 changes: 11 additions & 2 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,21 @@ func (s *ServiceSuite) Test_Setup() {
assert.Equal(s.T(), "d:_n:vendor_model-serial", string(*address))

s.sut.connectionsHub = s.conHub
s.conHub.EXPECT().Start()
s.conHub.EXPECT().Start().Once()
s.sut.Start()

time.Sleep(time.Millisecond * 200)

s.conHub.EXPECT().Shutdown()
isRunning := s.sut.IsRunning()
assert.True(s.T(), isRunning)

// nothing should happen
s.sut.Start()

s.conHub.EXPECT().Shutdown().Once()
s.sut.Shutdown()

// nothing should happen
s.sut.Shutdown()

device := s.sut.LocalDevice()
Expand Down

0 comments on commit 8ac366d

Please sign in to comment.