okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufdocs(test): explain coverage and manual checks2f6697a

main
3.0 KiB65 linesraw
1// Polling a completed action must retain the original deadline and cancellation.
2// A controlled peer gates delivery so these boundaries can be exercised reliably.
3package daemon
4
5import (
6	"context"
7	"fmt"
8	"net/http"
9	"testing"
10	"time"
11)
12
13func TestCompletedActionsRetainTheirDeadlineAndCallerCancellation(t *testing.T) {
14	cases := []struct {
15		name, path, body string
16		status           int
17		long             bool
18		invoke           func(context.Context, *Connection) error
19	}{
20		{"reload", "/sessions/s/reload", `{"session":{"state":"applied","loaded_revision":"loaded-current","restart_required":false,"warnings":[],"failure":null},"models":[],"cache_policy":{"state":"refreshed","failure":null}}`, 200, true, func(ctx context.Context, conn *Connection) error {
21			_, err := ReloadSession(ctx, conn, "s", ReloadRequest{Target: "both"})
22			return err
23		}},
24		{"upgrade", "/sessions/s/kernel/upgrade", `{"old_build":null,"new_build":null,"state":"unchanged","stopped_jobs":[],"warnings":[],"failure":null,"old_kernel_id":null,"new_kernel_id":null}`, 200, true, func(ctx context.Context, conn *Connection) error { _, err := UpgradeKernel(ctx, conn, "s"); return err }},
25		{"compaction", "/sessions/s/compaction", `{"selection_applied":false,"effective_strategy":null,"state":"unchanged","observation":null,"failure":null}`, 200, true, func(ctx context.Context, conn *Connection) error {
26			_, err := CompactSession(ctx, conn, "s", "")
27			return err
28		}},
29		{"shutdown", "/server/shutdown", `{"instance_id":"instance","state":"draining"}`, 202, false, StopDaemon},
30	}
31	for _, test := range cases {
32		for _, shorter := range []bool{false, true} {
33			t.Run(fmt.Sprintf("%s/shorter=%t", test.name, shorter), func(t *testing.T) {
34				ctx := t.Context()
35				if shorter {
36					var cancel context.CancelFunc
37					ctx, cancel = context.WithTimeout(ctx, time.Second)
38					defer cancel()
39				}
40				transport := captureTransport{entered: make(chan *http.Request), release: make(chan struct{}), body: test.body, status: test.status}
41				conn := NewConnection(ConnectionSnapshot{Port: 12345, Token: "token", Version: ProtocolVersion, InstanceID: "instance"}, nil)
42				conn.HTTPClient().Transport = transport
43				finished := make(chan error, 1)
44				go func() { finished <- test.invoke(ctx, conn) }()
45				request := <-transport.entered
46				deadline, ok := request.Context().Deadline()
47				close(transport.release)
48				if err := <-finished; err != nil {
49					t.Fatal(err)
50				}
51				if !ok || request.URL.Path != test.path || request.GetBody != nil {
52					t.Fatalf("wrong action transport contract: %s deadline=%t replay=%t", request.URL.Path, ok, request.GetBody != nil)
53				}
54				if shorter {
55					parentDeadline, _ := ctx.Deadline()
56					if !deadline.Equal(parentDeadline) {
57						t.Fatal("action extended the caller deadline")
58					}
59				} else if remaining := time.Until(deadline); test.long && remaining < 185*time.Second || !test.long && remaining > 20*time.Second {
60					t.Fatalf("action used wrong deadline budget: %s", remaining)
61				}
62			})
63		}
64	}
65}