Skip to content

Commit 3929405

Browse files
authored
test(storage): update tests for Writer.ProgressFunc (#12784)
* fix(storage): call progress func only on flush Writer.ProgressFunc should be called only on the flush of a chunk buffer, not on takeover or finalize/close. Fixes a behavior change introduced by the gRPC writer refactor. * update tests * update integration test
1 parent f35f697 commit 3929405

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

storage/client_test.go

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -956,35 +956,62 @@ func TestOpenWriterEmulated(t *testing.T) {
956956
})
957957
}
958958

959-
func TestWriterOneshotNoProgressReportEmulated(t *testing.T) {
959+
// Test that Writer.ProgressFunc is called at the expected intervals, showing
960+
// consistency across HTTP and gRPC.
961+
func TestWriterProgressFuncEmulated(t *testing.T) {
960962
transportClientTest(context.Background(), t, func(t *testing.T, ctx context.Context, project, bucket string, client storageClient) {
961963
_, err := client.CreateBucket(ctx, project, bucket, &BucketAttrs{
962964
Name: bucket,
963965
}, nil)
964966
if err != nil {
965967
t.Fatalf("client.CreateBucket: %v", err)
966968
}
967-
// Oneshot uploads can be forced with either a chunksize of 0, or a
968-
// chunksize larger than the data size.
969-
for _, chunksize := range []int{0, 16 * MiB} {
970-
t.Run(fmt.Sprintf("data size %d chunksize %d", 3*MiB, chunksize), func(t *testing.T) {
971-
prefix := time.Now().Nanosecond()
972-
objName := fmt.Sprintf("%d-object-%d", prefix, time.Now().Nanosecond())
973-
969+
cases := []struct {
970+
name string
971+
chunkSize int
972+
expectedCalls []int64
973+
}{
974+
{
975+
name: "one shot, zero chunkSize",
976+
chunkSize: 0,
977+
expectedCalls: []int64{},
978+
},
979+
{
980+
name: "one shot, chunk larger than data",
981+
chunkSize: 16 * MiB,
982+
expectedCalls: []int64{},
983+
},
984+
{
985+
name: "resumable, obj size evenly divisible by chunkSize",
986+
chunkSize: 1 * MiB,
987+
expectedCalls: []int64{MiB, 2 * MiB, 3 * MiB},
988+
},
989+
{
990+
name: "resumable, obj size not divisible by chunkSize",
991+
chunkSize: 2 * MiB,
992+
expectedCalls: []int64{2 * MiB, 3 * MiB},
993+
},
994+
}
995+
for _, c := range cases {
996+
t.Run(c.name, func(t *testing.T) {
974997
vc := &Client{tc: client}
975-
obj := vc.Bucket(bucket).Object(objName)
998+
obj := vc.Bucket(bucket).Object(c.name)
999+
9761000
w := obj.NewWriter(ctx)
977-
w.ChunkSize = chunksize
978-
progressCalls := 0
979-
w.ProgressFunc = func(int64) { progressCalls++ }
1001+
w.ChunkSize = c.chunkSize
1002+
var gotCalls []int64
1003+
w.ProgressFunc = func(off int64) {
1004+
gotCalls = append(gotCalls, off)
1005+
}
1006+
9801007
if _, err := w.Write(randomBytes3MiB); err != nil {
981-
t.Fatalf("writer.Write: %v", err)
1008+
t.Fatalf("writing data: %v", err)
9821009
}
9831010
if err := w.Close(); err != nil {
984-
t.Fatalf("writer.Close: %v", err)
1011+
t.Fatalf("closing writer: %v", err)
9851012
}
986-
if progressCalls != 0 {
987-
t.Errorf("ProgressFunc was called %d times, expected 0", progressCalls)
1013+
if slices.Compare(gotCalls, c.expectedCalls) != 0 {
1014+
t.Errorf("progressFunc: got calls at %v, want %v", gotCalls, c.expectedCalls)
9881015
}
9891016
attrs, err := obj.Attrs(ctx)
9901017
if err != nil {
@@ -996,6 +1023,7 @@ func TestWriterOneshotNoProgressReportEmulated(t *testing.T) {
9961023
})
9971024
}
9981025
})
1026+
9991027
}
10001028

10011029
func TestOpenAppendableWriterEmulated(t *testing.T) {

storage/integration_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,7 +3414,7 @@ func TestIntegration_WriterAppendTakeover(t *testing.T) {
34143414
opts: &AppendableWriterOpts{
34153415
ChunkSize: 4 * MiB,
34163416
},
3417-
checkProgressOffsets: []int64{7 * MiB, 8 * MiB},
3417+
checkProgressOffsets: []int64{3 * MiB, 7 * MiB, 8 * MiB, 9 * MiB},
34183418
},
34193419
{
34203420
name: "middle chunk takeover, small flush",
@@ -3456,7 +3456,7 @@ func TestIntegration_WriterAppendTakeover(t *testing.T) {
34563456
opts: &AppendableWriterOpts{
34573457
ChunkSize: 4 * MiB,
34583458
},
3459-
checkProgressOffsets: []int64{4 * MiB, 8 * MiB},
3459+
checkProgressOffsets: []int64{0, 4 * MiB, 8 * MiB, 9 * MiB},
34603460
},
34613461
{
34623462
name: "last byte takeover",
@@ -3574,12 +3574,11 @@ func TestIntegration_WriterAppendTakeover(t *testing.T) {
35743574
t.Errorf("got object finalized at %v, want unfinalized", attrs.Finalized)
35753575
}
35763576
// Check ProgressFunc was called if applicable
3577-
// Skip this until progressFunc bug is fixed.
3578-
// if tc.checkProgressOffsets != nil {
3579-
// if !slices.Equal(gotOffsets, tc.checkProgressOffsets) {
3580-
// t.Errorf("progressFunc calls: got %v, want %v", gotOffsets, tc.checkProgressOffsets)
3581-
// }
3582-
// }
3577+
if tc.checkProgressOffsets != nil {
3578+
if !slices.Equal(gotOffsets, tc.checkProgressOffsets) {
3579+
t.Errorf("progressFunc calls: got %v, want %v", gotOffsets, tc.checkProgressOffsets)
3580+
}
3581+
}
35833582
if w2.Attrs() == nil {
35843583
t.Fatalf("takeover writer attrs: expected attrs, got nil")
35853584
}

0 commit comments

Comments
 (0)