79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRunStopsMonitorWhenServerFails(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
file := filepath.Join(t.TempDir(), "local.yaml")
|
|
if err := os.WriteFile(file, []byte("server:\n addr: "+listener.Addr().String()+"\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("APP_STORAGE_MYSQL_ENABLED", "false")
|
|
t.Setenv("APP_STORAGE_REDIS_ENABLED", "false")
|
|
t.Setenv("APP_JETSTREAM_ENABLED", "false")
|
|
configPath = file
|
|
result := make(chan error, 1)
|
|
go func() { result <- run() }()
|
|
select {
|
|
case err := <-result:
|
|
if err == nil || !strings.Contains(err.Error(), "listen:") {
|
|
t.Fatalf("server failure was not returned: %v", err)
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("run did not stop its monitor after server startup failed")
|
|
}
|
|
}
|
|
|
|
func TestRunMonitorStopsOnCancellation(t *testing.T) {
|
|
wg = &sync.WaitGroup{}
|
|
wg.Add(1)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
result := make(chan error, 1)
|
|
go func() { result <- runMonitor(ctx) }()
|
|
cancel()
|
|
select {
|
|
case err := <-result:
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wg.Wait()
|
|
case <-time.After(time.Second):
|
|
t.Fatal("monitor did not stop after cancellation")
|
|
}
|
|
}
|
|
|
|
func TestHealthAddressUsesConfiguredPort(t *testing.T) {
|
|
file := filepath.Join(t.TempDir(), "rpc.yaml")
|
|
if err := os.WriteFile(file, []byte("server:\n addr: \":50002\"\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("CONFIG_FILE", file)
|
|
if value, exists := os.LookupEnv("APP_SERVER_ADDR"); exists {
|
|
t.Setenv("APP_SERVER_ADDR", value)
|
|
if err := os.Unsetenv("APP_SERVER_ADDR"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
address, err := healthAddress("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if address != "127.0.0.1:50002" {
|
|
t.Fatalf("probe address = %q, want configured address 127.0.0.1:50002", address)
|
|
}
|
|
}
|