~james/core

core/spindle/nixcache/publishing_test.go -rw-r--r-- 5.7 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package nixcache

import (
	"context"
	"net/http"
	"sync"
	"testing"
	"time"

	"tangled.org/core/spindle/db"
)

type fakeReservations struct {
	mu                  sync.Mutex
	committed, released []string
}

func (f *fakeReservations) Commit(_ context.Context, id string) error {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.committed = append(f.committed, id)
	return nil
}

func (f *fakeReservations) Release(_ context.Context, id string) error {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.released = append(f.released, id)
	return nil
}

func (f *fakeReservations) settled() (committed, released []string) {
	f.mu.Lock()
	defer f.mu.Unlock()
	return append([]string(nil), f.committed...), append([]string(nil), f.released...)
}

func publishingRow(id, repo, hash string, age time.Duration) db.NixPublishing {
	cache, _ := CacheName(repo)
	return db.NixPublishing{ReservationID: id, Cache: cache, StoreHash: hash, CreatedAt: pruneNow.Add(-age)}
}

func (pt *pruneTest) publishing(rows ...db.NixPublishing) *fakeReservations {
	res := &fakeReservations{}
	pt.pruner.Reservations = res
	pt.pruner.Publishing = rows
	return res
}

func wantIDs(t *testing.T, what string, got []string, want ...string) {
	t.Helper()
	if len(got) != len(want) || len(want) > 0 && got[0] != want[0] {
		t.Errorf("%s = %v, want %v", what, got, want)
	}
}

func TestResolvePublishingCommitsPresentReleasesOldMissingAndKeepsYoungMissing(t *testing.T) {
	pt := newPruneTest(t)
	pt.attic.store(repoA, hash('a'))
	res := pt.publishing(
		publishingRow("present", repoA, hash('a'), time.Minute),
		publishingRow("old", repoA, hash('b'), 2*time.Hour),
		publishingRow("young", repoA, hash('c'), time.Minute),
	)

	pt.pruner.resolvePublishing(context.Background())

	committed, released := res.settled()
	wantIDs(t, "committed", committed, "present")
	wantIDs(t, "released", released, "old")
	if len(pt.pruner.Publishing) != 1 || pt.pruner.Publishing[0].ReservationID != "young" {
		t.Errorf("kept = %+v, want only the young one", pt.pruner.Publishing)
	}
	if len(pt.attic.missingCalls) != 1 {
		t.Errorf("attic got %d calls, want one per cache", len(pt.attic.missingCalls))
	}
}

func TestResolvePublishingWaitsOutTheLongestLiveToken(t *testing.T) {
	pt := newPruneTest(t)
	pt.pruner.Grace = 5 * time.Minute
	pt.pruner.Grants = liveGrants(3 * time.Hour)
	res := pt.publishing(publishingRow("missing", repoA, hash('b'), 2*time.Hour))

	pt.pruner.resolvePublishing(context.Background())

	if _, released := res.settled(); len(released) != 0 || len(pt.pruner.Publishing) != 1 {
		t.Fatalf("released %v while a token that could still finish the upload is live, want it kept", released)
	}
}

func TestResolvePublishingKeepsEverythingWhileAtticIsDownThenSettlesIt(t *testing.T) {
	pt := newPruneTest(t)
	cacheA, _ := CacheName(repoA)
	cacheB, _ := CacheName(repoB)
	pt.attic.store(repoA, hash('a'))
	pt.attic.failCache = map[string]int{cacheA: http.StatusBadGateway, cacheB: http.StatusBadGateway}
	res := pt.publishing(
		publishingRow("a", repoA, hash('a'), time.Minute),
		publishingRow("b", repoB, hash('b'), 2*time.Hour),
	)

	pt.pruner.resolvePublishing(context.Background())

	if committed, released := res.settled(); len(committed)+len(released) != 0 || len(pt.pruner.Publishing) != 2 {
		t.Fatalf("settled %v and %v with attic down, kept %d, want nothing settled and both kept", committed, released, len(pt.pruner.Publishing))
	}
	if len(pt.attic.missingCalls) != 1 {
		t.Errorf("attic got %d calls %v, want the pass to end after the first", len(pt.attic.missingCalls), pt.attic.missingCalls)
	}

	pt.attic.failCache = nil
	pt.pruner.resolvePublishing(context.Background())

	committed, released := res.settled()
	wantIDs(t, "committed", committed, "a")
	wantIDs(t, "released", released, "b")
	if len(pt.pruner.Publishing) != 0 {
		t.Errorf("kept = %+v, want none", pt.pruner.Publishing)
	}
}

func TestResolvePublishingKeepsOnlyTheCacheAtticRefuses(t *testing.T) {
	pt := newPruneTest(t)
	cacheA, _ := CacheName(repoA)
	pt.attic.store(repoA, hash('a'))
	pt.attic.store(repoB, hash('b'))
	pt.attic.failCache = map[string]int{cacheA: http.StatusForbidden}
	res := pt.publishing(
		publishingRow("a", repoA, hash('a'), time.Minute),
		publishingRow("b", repoB, hash('b'), time.Minute),
	)

	pt.pruner.resolvePublishing(context.Background())

	committed, _ := res.settled()
	wantIDs(t, "committed", committed, "b")
	if len(pt.pruner.Publishing) != 1 || pt.pruner.Publishing[0].ReservationID != "a" {
		t.Errorf("kept = %+v, want the refused cache's row", pt.pruner.Publishing)
	}
}

func TestStartWithoutAnIntervalSettlesPublishingNowAndAfterGrace(t *testing.T) {
	pt := newPruneTest(t)
	pt.pruner.MaxBytes = 1
	pt.add(repoA, ownerX, hash('z'), 100, 3*day)
	pt.attic.store(repoA, hash('a'))
	pt.pruner.Grace = 300 * time.Millisecond
	pt.pruner.now = time.Now
	cache, _ := CacheName(repoA)
	res := pt.publishing(
		db.NixPublishing{ReservationID: "present", Cache: cache, StoreHash: hash('a'), CreatedAt: time.Now()},
		db.NixPublishing{ReservationID: "missing", Cache: cache, StoreHash: hash('b'), CreatedAt: time.Now()},
	)

	pt.pruner.Start(t.Context(), 0)

	waitFor(t, "the present upload to be committed", func() bool { c, _ := res.settled(); return len(c) == 1 })
	if _, released := res.settled(); len(released) != 0 {
		t.Fatalf("released %v before the grace passed", released)
	}
	waitFor(t, "the missing upload to be released", func() bool { _, r := res.settled(); return len(r) == 1 })
	if len(pt.attic.deletes) != 0 {
		t.Errorf("delete calls = %v, want no pruning with eviction off", pt.attic.deletes)
	}
}

func waitFor(t *testing.T, what string, done func() bool) {
	t.Helper()
	for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
		if done() {
			return
		}
	}
	t.Fatalf("timed out waiting for %s", what)
}