58 lines
1.3 KiB
Go
58 lines
1.3 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")
|
|
}
|
|
}
|