Skip to content

Commit

Permalink
Merge pull request #893 from SiaFoundation/pj/upload-regression
Browse files Browse the repository at this point in the history
Add regression test for the upload deadlock
  • Loading branch information
ChrisSchinnerl authored Jan 23, 2024
2 parents 4affd8a + baf644e commit 6e715a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stores/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func (s *SQLStore) findAggregatedContractPeriods(start time.Time, n uint64, inte
SELECT contracts.*, i.Period FROM contracts
INNER JOIN (
SELECT
p.period_start as Period,
p.period_start as Period,
MIN(c.id) AS id
FROM
periods p
Expand Down
7 changes: 6 additions & 1 deletion worker/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ type (
}

mockMemory struct{}
mockMemoryManager struct{}
mockMemoryManager struct {
memBlockChan chan struct{}
}

mockObjectStore struct {
mu sync.Mutex
Expand Down Expand Up @@ -110,6 +112,9 @@ func (mm *mockMemoryManager) Limit(amt uint64) (MemoryManager, error) {
}
func (mm *mockMemoryManager) Status() api.MemoryStatus { return api.MemoryStatus{} }
func (mm *mockMemoryManager) AcquireMemory(ctx context.Context, amt uint64) Memory {
if mm.memBlockChan != nil {
<-mm.memBlockChan
}
return &mockMemory{}
}

Expand Down
55 changes: 55 additions & 0 deletions worker/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,61 @@ func TestMigrateShards(t *testing.T) {
}
}

func TestUploadRegression(t *testing.T) {
// mock worker
w := newMockWorker(testRedundancySettings.TotalShards * 2)

// convenience variables
ul := w.ul
dl := w.dl
mm := w.mm
os := w.os

// create test data
data := make([]byte, 128)
if _, err := frand.Read(data); err != nil {
t.Fatal(err)
}

// create upload params
params := testParameters(t.Name())

// make sure the memory manager blocks
mm.memBlockChan = make(chan struct{})

// upload data
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, _, err := ul.Upload(ctx, bytes.NewReader(data), w.contracts.values(), params, lockingPriorityUpload)
if !errors.Is(err, errUploadInterrupted) {
t.Fatal(err)
}

// unblock the memory manager
close(mm.memBlockChan)

// upload data
_, _, err = ul.Upload(context.Background(), bytes.NewReader(data), w.contracts.values(), params, lockingPriorityUpload)
if err != nil {
t.Fatal(err)
}

// grab the object
o, err := os.Object(context.Background(), testBucket, t.Name(), api.GetObjectOptions{})
if err != nil {
t.Fatal(err)
}

// download data for good measure
var buf bytes.Buffer
err = dl.DownloadObject(context.Background(), &buf, o.Object.Object, 0, uint64(o.Object.Size), w.contracts.values())
if err != nil {
t.Fatal(err)
} else if !bytes.Equal(data, buf.Bytes()) {
t.Fatal("data mismatch", data, buf.Bytes())
}
}

func testParameters(path string) uploadParameters {
return uploadParameters{
bucket: testBucket,
Expand Down

0 comments on commit 6e715a0

Please sign in to comment.