108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
package hub
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/shoemoney/devbox/internal/chunk"
|
|
"github.com/shoemoney/devbox/internal/hub/blobstore"
|
|
"github.com/shoemoney/devbox/internal/hub/meta"
|
|
"github.com/shoemoney/devbox/internal/manifest"
|
|
"github.com/shoemoney/devbox/pkg/proto"
|
|
)
|
|
|
|
// Pause each manifest read after the handler's preliminary head check. Channels
|
|
// let both requests observe the same parent, then commit A before releasing B.
|
|
// There is no scheduler timing or repeated-attempt assumption in this proof.
|
|
type pushBarrierStore struct {
|
|
blobstore.Store
|
|
gates map[string]chan struct{}
|
|
arrived chan string
|
|
}
|
|
|
|
func (s *pushBarrierStore) Get(hash string) ([]byte, error) {
|
|
if gate, ok := s.gates[hash]; ok {
|
|
s.arrived <- hash
|
|
<-gate
|
|
}
|
|
return s.Store.Get(hash)
|
|
}
|
|
|
|
func TestConcurrentPushRejectsStaleParent(t *testing.T) {
|
|
db, err := meta.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if err := db.AddDevice("device", "test", []byte("pub"), 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.CreateShare("proj", "device", 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
disk, err := blobstore.NewDisk(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := &pushBarrierStore{Store: disk, gates: make(map[string]chan struct{}), arrived: make(chan string, 2)}
|
|
var hashes, chunks [2]string
|
|
for i, name := range []string{"a", "b"} {
|
|
data := []byte(name)
|
|
chunks[i] = chunk.Hash(data)
|
|
if err := disk.Put(chunks[i], data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mf := manifest.Manifest{Entries: []manifest.Entry{{Path: name, Mode: 0644, Size: 1, Chunks: []string{chunks[i]}}}}
|
|
body, err := mf.Marshal()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hashes[i] = chunk.Hash(body)
|
|
if err := disk.Put(hashes[i], body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store.gates[hashes[i]] = make(chan struct{})
|
|
}
|
|
srv := NewServer(db, store)
|
|
done := [2]chan *httptest.ResponseRecorder{make(chan *httptest.ResponseRecorder, 1), make(chan *httptest.ResponseRecorder, 1)}
|
|
for i := range hashes {
|
|
body := mustJSON(t, proto.PushRequest{Share: "proj", ManifestHash: hashes[i]})
|
|
go func(i int) {
|
|
w := httptest.NewRecorder()
|
|
srv.handlePush(w, httptest.NewRequest("POST", proto.PathPush, bytes.NewReader(body)), "device")
|
|
done[i] <- w
|
|
}(i)
|
|
}
|
|
<-store.arrived
|
|
<-store.arrived
|
|
close(store.gates[hashes[0]])
|
|
first := <-done[0]
|
|
close(store.gates[hashes[1]])
|
|
second := <-done[1]
|
|
for i, w := range []*httptest.ResponseRecorder{first, second} {
|
|
var got proto.PushResponse
|
|
if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &got) != nil {
|
|
t.Fatalf("push %d: %d %s", i, w.Code, w.Body)
|
|
}
|
|
if got.Conflict != (i == 1) || got.Head != hashes[0] {
|
|
t.Errorf("push %d: %+v, want conflict=%v head=%s", i, got, i == 1, hashes[0])
|
|
}
|
|
}
|
|
head, _, err := db.ShareHead("proj")
|
|
if err != nil || head != hashes[0] {
|
|
t.Errorf("head=%s err=%v, want %s", head, err, hashes[0])
|
|
}
|
|
snaps, err := db.SnapshotIDs("proj")
|
|
if err != nil || len(snaps) != 1 || snaps[0] != hashes[0] {
|
|
t.Errorf("snapshots=%v err=%v, want only winner", snaps, err)
|
|
}
|
|
for i, hash := range chunks {
|
|
want := 1 - i
|
|
if rc, err := db.ChunkRefcount(hash); err != nil || rc != want {
|
|
t.Errorf("chunk %d refcount=%d err=%v, want %d", i, rc, err, want)
|
|
}
|
|
}
|
|
}
|