Skip to content

Commit

Permalink
fix extra empty file bug (#63)
Browse files Browse the repository at this point in the history
* fix extra empty file bug

* tests
  • Loading branch information
maha-hajja authored Jun 17, 2022
1 parent 380dcd2 commit f985ad3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions destination/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (d *Destination) Teardown(ctx context.Context) error {
}

func (d *Destination) Flush(ctx context.Context) error {
if len(d.Buffer) == 0 {
return nil
}

bufferedRecords := d.Buffer
d.Buffer = d.Buffer[:0]

Expand Down
31 changes: 31 additions & 0 deletions destination/destination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ func TestLocalParquet(t *testing.T) {
}
}

err = destination.Flush(ctx)
if err != nil {
t.Fatalf("failed to Flush: %v", err)
}

err = destination.Teardown(ctx)
if err != nil {
t.Fatalf("failed to Teardown: %v", err)
}

// The code above should produce two files in the fixtures directory:
// - local-0001.parquet
// - local-0002.parquet
Expand Down Expand Up @@ -115,6 +125,16 @@ func TestLocalJSON(t *testing.T) {
}
}

err = destination.Flush(ctx)
if err != nil {
t.Fatalf("failed to Flush: %v", err)
}

err = destination.Teardown(ctx)
if err != nil {
t.Fatalf("failed to Teardown: %v", err)
}

// The code above should produce two files in the fixtures directory:
// - local-0001.json
// - local-0002.json
Expand Down Expand Up @@ -198,6 +218,17 @@ func TestS3Parquet(t *testing.T) {
t.Fatalf("Destination writer expected to be writer.S3, but is actually %+v", writer)
}

err = destination.Flush(ctx)
if err != nil {
t.Fatalf("failed to Flush: %v", err)
}

err = destination.Teardown(ctx)
if err != nil {
t.Fatalf("failed to Teardown: %v", err)
}

// check if only two files are written
if len(writer.FilesWritten) != 2 {
t.Fatalf("Expected writer to have written 2 files, got %d", len(writer.FilesWritten))
}
Expand Down

0 comments on commit f985ad3

Please sign in to comment.