okami.mom/albedo

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

Aly Raffaufdocs(test): explain coverage and manual checks2f6697a

main
1.2 KiB28 linesraw
1// Conditional mutations must retain the observed ETag and never retry conflicts.
2// A controlled peer records delivery and returns conflicts deterministically.
3package daemon
4
5import (
6	"io"
7	"net/http"
8	"sync/atomic"
9	"testing"
10)
11
12func TestConditionalMutationUsesObservedValidatorAndNeverRetriesConflict(t *testing.T) {
13	var requests atomic.Int32
14	conn := controlledConnection(t, func(w http.ResponseWriter, r *http.Request) {
15		requests.Add(1)
16		if r.Method != "PATCH" || r.URL.String() != "/sessions/s?view=configuration" || r.Header.Get("If-Match") != "\"seen\"" || r.Header.Get("Content-Type") != "application/merge-patch+json" {
17			t.Errorf("wrong conditional mutation: %s %s %v", r.Method, r.URL, r.Header)
18		}
19		w.WriteHeader(412)
20		_, _ = io.WriteString(w, `{"type":"about:blank","title":"Changed","status":412,"code":"precondition_failed","detail":"Refresh first"}`)
21	})
22	if _, err := RenameSession(t.Context(), conn, "s", "name", ""); err == nil || requests.Load() != 0 {
23		t.Fatal("unobserved mutation dispatched")
24	}
25	if _, err := RenameSession(t.Context(), conn, "s", "name", "\"seen\""); err == nil || requests.Load() != 1 {
26		t.Fatalf("conditional conflict retried: %v count=%d", err, requests.Load())
27	}
28}