feat: API skeleton, SSE, staffing and tests
This commit is contained in:
1 parent
907164f972
commit
f284969a4b
14 files changed
+1355
-54
No files matched your search
@@ -9,13 +9,13 @@ import (
|
||||
|
||||
// Config is the process configuration.
|
||||
type Config struct {
|
||||
Addr string
|
||||
DataDir string
|
||||
FastAPIURL string
|
||||
Addr string
|
||||
DataDir string
|
||||
FastAPIURL string
|
||||
ControlUser string
|
||||
ControlPass string
|
||||
Heartbeat time.Duration
|
||||
MLTimeout time.Duration
|
||||
Heartbeat time.Duration
|
||||
MLTimeout time.Duration
|
||||
}
|
||||
|
||||
// Load reads configuration from env with local-development defaults.
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadDefaults(t *testing.T) {
|
||||
for _, key := range []string{"PORT", "DATA_DIR", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "SSE_HEARTBEAT", "ML_TIMEOUT"} {
|
||||
t.Setenv(key, "")
|
||||
}
|
||||
cfg := Load()
|
||||
if cfg.Addr != ":8080" {
|
||||
t.Errorf("Addr=%q", cfg.Addr)
|
||||
}
|
||||
if cfg.DataDir != "testdata/data" {
|
||||
t.Errorf("DataDir=%q", cfg.DataDir)
|
||||
}
|
||||
if cfg.FastAPIURL != "http://localhost:8000" {
|
||||
t.Errorf("FastAPIURL=%q", cfg.FastAPIURL)
|
||||
}
|
||||
if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond {
|
||||
t.Errorf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
if cfg.ControlAuthConfigured() {
|
||||
t.Error("control auth should be unconfigured by default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromEnv(t *testing.T) {
|
||||
t.Setenv("PORT", "9999")
|
||||
t.Setenv("DATA_DIR", "/data")
|
||||
t.Setenv("FASTAPI_URL", "http://ml:8000")
|
||||
t.Setenv("CONTROL_USER", "user")
|
||||
t.Setenv("CONTROL_PASS", "pass")
|
||||
t.Setenv("SSE_HEARTBEAT", "5s")
|
||||
t.Setenv("ML_TIMEOUT", "2s")
|
||||
|
||||
cfg := Load()
|
||||
if cfg.Addr != ":9999" || cfg.DataDir != "/data" || cfg.FastAPIURL != "http://ml:8000" {
|
||||
t.Fatalf("cfg=%+v", cfg)
|
||||
}
|
||||
if cfg.Heartbeat != 5*time.Second || cfg.MLTimeout != 2*time.Second {
|
||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
if !cfg.ControlAuthConfigured() {
|
||||
t.Fatal("control auth should be configured")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationAcceptsPlainSeconds(t *testing.T) {
|
||||
t.Setenv("SSE_HEARTBEAT", "7")
|
||||
t.Setenv("ML_TIMEOUT", "3")
|
||||
cfg := Load()
|
||||
if cfg.Heartbeat != 7*time.Second || cfg.MLTimeout != 3*time.Second {
|
||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationFallsBackOnInvalidValue(t *testing.T) {
|
||||
t.Setenv("SSE_HEARTBEAT", "soon")
|
||||
t.Setenv("ML_TIMEOUT", "-1s")
|
||||
cfg := Load()
|
||||
if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond {
|
||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user