Skip to content

Commit 391cfd4

Browse files
committed
simplify httpserver tests
1 parent 5db9867 commit 391cfd4

File tree

2 files changed

+89
-418
lines changed

2 files changed

+89
-418
lines changed

runnables/httpserver/mocks_test.go

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package httpserver
22

33
import (
44
"context"
5-
"errors"
6-
"fmt"
75

8-
"github.com/robbyt/go-supervisor/internal/finitestate"
96
"github.com/stretchr/testify/mock"
107
)
118

@@ -67,126 +64,6 @@ func (m *MockStateMachine) GetStateChanWithTimeout(ctx context.Context) <-chan s
6764
// GetStateChanBuffer mocks the GetStateChanBuffer method of the stateMachine interface.
6865
// It returns a channel with a configurable buffer size that emits the state machine's state whenever it changes.
6966

70-
// MockRunner is a special version of Runner that allows direct manipulation
71-
// of config storage for testing purposes
72-
type MockRunner struct {
73-
storedConfig *Config
74-
mockFSM fsm
75-
callback func() (*Config, error)
76-
ctx context.Context
77-
// Custom error responses for testing
78-
stopServerErr error
79-
setStateErrorCalled bool
80-
}
81-
82-
// Creates a new MockRunner with mocked config storage
83-
func NewMockRunner(configCallback func() (*Config, error), f fsm) *MockRunner {
84-
return &MockRunner{
85-
callback: configCallback,
86-
mockFSM: f,
87-
ctx: context.Background(),
88-
}
89-
}
90-
91-
// getConfig implementation for MockRunner
92-
func (r *MockRunner) getConfig() *Config {
93-
return r.storedConfig
94-
}
95-
96-
// setConfig implementation for MockRunner
97-
func (r *MockRunner) setConfig(config *Config) {
98-
r.storedConfig = config
99-
}
100-
101-
// configCallback returns the MockRunner's callback
102-
func (r *MockRunner) configCallback() (*Config, error) {
103-
return r.callback()
104-
}
105-
106-
// String returns a string representation of the MockRunner
107-
func (r *MockRunner) String() string {
108-
config := r.getConfig()
109-
if config == nil {
110-
return "MockRunner<nil>"
111-
}
112-
return fmt.Sprintf("MockRunner{listening: %s}", config.ListenAddr)
113-
}
114-
115-
// reloadConfig implementation similar to Runner's reloadConfig
116-
func (r *MockRunner) reloadConfig() error {
117-
newConfig, err := r.configCallback()
118-
if err != nil {
119-
return fmt.Errorf("failed to reload config: %w", err)
120-
}
121-
122-
if newConfig == nil {
123-
return errors.New("config callback returned nil")
124-
}
125-
126-
oldConfig := r.getConfig()
127-
if oldConfig == nil {
128-
r.setConfig(newConfig)
129-
return nil
130-
}
131-
132-
if newConfig.Equal(oldConfig) {
133-
// Config unchanged, skip reload and return early
134-
return ErrOldConfig
135-
}
136-
137-
r.setConfig(newConfig)
138-
return nil
139-
}
140-
141-
// stopServer is a mock implementation of Runner.stopServer
142-
func (r *MockRunner) stopServer(ctx context.Context) error {
143-
return r.stopServerErr
144-
}
145-
146-
// setStateError is a mock implementation of Runner.setStateError
147-
func (r *MockRunner) setStateError() {
148-
r.setStateErrorCalled = true
149-
if err := r.mockFSM.SetState(finitestate.StatusError); err != nil {
150-
// In test code, we just panic on state machine errors to make failures obvious
151-
panic(fmt.Sprintf("Failed to set error state: %v", err))
152-
}
153-
}
154-
155-
// Reload is a simplified implementation of the Runner.Reload method for testing
156-
func (r *MockRunner) Reload() {
157-
// Attempt to transition to reloading state
158-
if err := r.mockFSM.Transition(finitestate.StatusReloading); err != nil {
159-
return
160-
}
161-
162-
// Try to reload config
163-
err := r.reloadConfig()
164-
if err != nil {
165-
if errors.Is(err, ErrOldConfig) {
166-
// Config unchanged, go back to running
167-
if stateErr := r.mockFSM.Transition(finitestate.StatusRunning); stateErr != nil {
168-
r.setStateError()
169-
}
170-
return
171-
}
172-
r.setStateError()
173-
return
174-
}
175-
176-
// Try to stop server
177-
if err := r.stopServer(r.ctx); err != nil {
178-
r.setStateError()
179-
return
180-
}
181-
182-
// For testing, we'll skip the actual boot() step
183-
// just transition to running state
184-
if err := r.mockFSM.Transition(finitestate.StatusRunning); err != nil {
185-
r.setStateError()
186-
return
187-
}
188-
}
189-
19067
// MockHttpServer is a mock implementation of the HttpServer interface
19168
type MockHttpServer struct {
19269
mock.Mock

0 commit comments

Comments
 (0)