Implement instance tests for timeout

This commit is contained in:
2025-08-17 21:50:16 +02:00
parent 41eaebc927
commit d70bb634cd
4 changed files with 334 additions and 9 deletions

View File

@@ -17,6 +17,18 @@ import (
"time"
)
// TimeProvider interface allows for testing with mock time
type TimeProvider interface {
Now() time.Time
}
// realTimeProvider implements TimeProvider using the actual time
type realTimeProvider struct{}
func (realTimeProvider) Now() time.Time {
return time.Now()
}
type CreateInstanceOptions struct {
// Auto restart
AutoRestart *bool `json:"auto_restart,omitempty"`
@@ -90,6 +102,7 @@ type Process struct {
// Timeout management
lastRequestTime atomic.Int64 // Unix timestamp of last request
timeProvider TimeProvider `json:"-"` // Time provider for testing
}
// validateAndCopyOptions validates and creates a deep copy of the provided options
@@ -179,10 +192,8 @@ func NewInstance(name string, globalSettings *config.InstancesConfig, options *C
options: optionsCopy,
globalSettings: globalSettings,
logger: logger,
Running: false,
Created: time.Now().Unix(),
timeProvider: realTimeProvider{},
Created: time.Now().Unix(),
}
}
@@ -210,6 +221,11 @@ func (i *Process) SetOptions(options *CreateInstanceOptions) {
i.proxy = nil
}
// SetTimeProvider sets a custom time provider for testing
func (i *Process) SetTimeProvider(tp TimeProvider) {
i.timeProvider = tp
}
// GetProxy returns the reverse proxy for this instance, creating it if needed
func (i *Process) GetProxy() (*httputil.ReverseProxy, error) {
i.mu.Lock()