Take the chat client's port from Run's caller (closes #4)
check / check (push) Successful in 1m8s
check / check (push) Successful in 1m8s
The test that runs the whole bot started its stand-in chat client on 5225, the port a real simplex-chat uses, so on a machine where one listens there the test would have sent it the bot's set-up commands. Run now takes the chat client's port: the run command passes bot.ChatPort (5225), and the test a port it found free. Model: opus-5-5
This commit is contained in:
+17
-13
@@ -28,11 +28,11 @@ const DisplayName = "calc"
|
|||||||
const Welcome = "Send me arithmetic, such as 2 + 2 or 5 * 5/2, " +
|
const Welcome = "Send me arithmetic, such as 2 + 2 or 5 * 5/2, " +
|
||||||
"and I will reply with the result."
|
"and I will reply with the result."
|
||||||
|
|
||||||
const (
|
// ChatPort is where the chat client serves its API, on localhost inside
|
||||||
// chatPort is where the chat client serves its API, on localhost
|
// the bot's own container.
|
||||||
// inside the bot's own container.
|
const ChatPort = 5225
|
||||||
chatPort = 5225
|
|
||||||
|
|
||||||
|
const (
|
||||||
// connectTimeout bounds the wait for a freshly started chat client
|
// connectTimeout bounds the wait for a freshly started chat client
|
||||||
// to open its API, which includes creating or migrating the
|
// to open its API, which includes creating or migrating the
|
||||||
// database.
|
// database.
|
||||||
@@ -54,12 +54,15 @@ const (
|
|||||||
|
|
||||||
var errExited = errors.New("simplex-chat exited")
|
var errExited = errors.New("simplex-chat exited")
|
||||||
|
|
||||||
// Run starts the chat client with its database in cfg.DataDir, connects
|
// Run starts the chat client with its database in cfg.DataDir, serving
|
||||||
// to it, sets up the bot's address, then answers messages and serves the
|
// its API on localhost at chatPort, connects to it, sets up the bot's
|
||||||
// API until ctx is cancelled — which is a clean stop and returns nil —
|
// address, then answers messages and serves the bot's API until ctx is
|
||||||
// or until the chat client, the connection to it or the API's listener
|
// cancelled — which is a clean stop and returns nil — or until the chat
|
||||||
// fails, which returns the error.
|
// client, the connection to it or the API's listener fails, which
|
||||||
func Run(ctx context.Context, log *slog.Logger, cfg *config.Config) error {
|
// returns the error.
|
||||||
|
func Run(
|
||||||
|
ctx context.Context, log *slog.Logger, cfg *config.Config, chatPort int,
|
||||||
|
) error {
|
||||||
err := os.MkdirAll(cfg.DataDir, dataDirMode)
|
err := os.MkdirAll(cfg.DataDir, dataDirMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("creating data directory: %w", err)
|
return fmt.Errorf("creating data directory: %w", err)
|
||||||
@@ -84,7 +87,7 @@ func Run(ctx context.Context, log *slog.Logger, cfg *config.Config) error {
|
|||||||
<-cli.Done()
|
<-cli.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
client, err := connect(ctx, log, cli)
|
client, err := connect(ctx, log, cli, chatPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -143,9 +146,10 @@ func stopAPI(ctx context.Context, log *slog.Logger, srv *http.Server) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// connect waits for the chat client to open its API and connects to it.
|
// connect waits for the chat client to open its API on chatPort and
|
||||||
|
// connects to it.
|
||||||
func connect(
|
func connect(
|
||||||
ctx context.Context, log *slog.Logger, cli *simplex.CLI,
|
ctx context.Context, log *slog.Logger, cli *simplex.CLI, chatPort int,
|
||||||
) (*simplex.Client, error) {
|
) (*simplex.Client, error) {
|
||||||
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
|
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|||||||
@@ -52,9 +52,7 @@ func TestMain(m *testing.M) {
|
|||||||
// port it is given, on localhost, and answers the commands the bot
|
// port it is given, on localhost, and answers the commands the bot
|
||||||
// sends. SIGTERM ends it, as it ends the real client. Unlike the real
|
// sends. SIGTERM ends it, as it ends the real client. Unlike the real
|
||||||
// client, it also exits when the bot hangs up, so that it never
|
// client, it also exits when the bot hangs up, so that it never
|
||||||
// outlives a test run that was cut short. It takes port 5225, the chat
|
// outlives a test run that was cut short.
|
||||||
// client's fixed port, so this test cannot run twice at once on one
|
|
||||||
// machine.
|
|
||||||
func standIn() {
|
func standIn() {
|
||||||
arg := func(name string) string {
|
arg := func(name string) string {
|
||||||
return os.Args[slices.Index(os.Args, name)+1]
|
return os.Args[slices.Index(os.Args, name)+1]
|
||||||
@@ -142,6 +140,8 @@ func TestStopDuringRequest(t *testing.T) {
|
|||||||
t.Setenv("PATH", bin)
|
t.Setenv("PATH", bin)
|
||||||
|
|
||||||
cfg := &config.Config{DataDir: t.TempDir(), Port: freePort(t), APIToken: credential}
|
cfg := &config.Config{DataDir: t.TempDir(), Port: freePort(t), APIToken: credential}
|
||||||
|
// Never bot.ChatPort: a real chat client may be listening there.
|
||||||
|
chatPort := freePort(t)
|
||||||
ctx, stop := context.WithCancel(t.Context())
|
ctx, stop := context.WithCancel(t.Context())
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
@@ -150,7 +150,7 @@ func TestStopDuringRequest(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
runErr = bot.Run(ctx, slog.New(slog.DiscardHandler), cfg)
|
runErr = bot.Run(ctx, slog.New(slog.DiscardHandler), cfg, chatPort)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
|||||||
+1
-1
@@ -45,7 +45,7 @@ func run(ctx context.Context, version string) error {
|
|||||||
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
err = bot.Run(ctx, log, cfg)
|
err = bot.Run(ctx, log, cfg, bot.ChatPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("stopped", "error", err)
|
log.Error("stopped", "error", err)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user