Skip to content

Commit

Permalink
Test at which point output writers are getting closed
Browse files Browse the repository at this point in the history
Tests work as expected, so this is not the bug we're trying to find.
But having these tests in place won’t hurt.

#40
  • Loading branch information
brawer committed May 15, 2024
1 parent 00ad13d commit 0f9789d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
10 changes: 9 additions & 1 deletion cmd/qrank-builder/item_signals_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

func TestItemSignalsWriter(t *testing.T) {
var buf bytes.Buffer
w := NewItemSignalsWriter(NopWriteCloser(&buf))
writer := TestingWriteCloser(&buf)
w := NewItemSignalsWriter(writer)
for _, s := range []ItemSignals{
ItemSignals{72, 1, 2, 3, 4, 5},
ItemSignals{72, 3, 3, 3, 3, 3},
Expand All @@ -22,9 +23,16 @@ func TestItemSignalsWriter(t *testing.T) {
t.Error(err)
}
}
if writer.closed {
t.Error("ItemSignalsWriter has prematurely closed its output writer")
}
if err := w.Close(); err != nil {
t.Error(err)
}
if !writer.closed {
t.Error("ItemSignalsWriter.Close() should close its output writer")
}

got := strings.Split(strings.TrimSuffix(string(buf.Bytes()), "\n"), "\n")
want := []string{
"item,pageviews_52w,wikitext_bytes,claims,identifiers,sitelinks",
Expand Down
9 changes: 8 additions & 1 deletion cmd/qrank-builder/pagesignals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func storeFakePageSignals(id string, content string, s3 *FakeS3, t *testing.T) {
func TestPageSignalMerger(t *testing.T) {
logger = log.New(&bytes.Buffer{}, "", log.Lshortfile)
var buf strings.Builder
m := NewPageSignalMerger(NopWriteCloser(&buf))
writer := TestingWriteCloser(&buf)
m := NewPageSignalMerger(writer)
for _, line := range []string{
"11,s=1111111",
"22,Q72",
Expand All @@ -171,9 +172,15 @@ func TestPageSignalMerger(t *testing.T) {
t.Error(err)
}
}
if writer.closed {
t.Errorf("PageSignalMerger.Close() closed output writer prematurely")
}
if err := m.Close(); err != nil {
t.Error(err)
}
if !writer.closed {
t.Errorf("PageSignalMerger.Close() should close output writer")
}
got := strings.Split(strings.TrimSuffix(buf.String(), "\n"), "\n")
want := []string{
"22,Q72,830167",
Expand Down
21 changes: 21 additions & 0 deletions cmd/qrank-builder/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ func (s3 *FakeS3) FPutObject(ctx context.Context, bucketName, objectName, filePa
return info, nil
}

type testingWriteCloser struct {
writer io.Writer
closed bool
}

func TestingWriteCloser(w io.Writer) *testingWriteCloser {
return &testingWriteCloser{writer: w}
}

func (t *testingWriteCloser) Write(p []byte) (int, error) {
if t.closed {
return 0, fmt.Errorf("already closed")
}
return t.writer.Write(p)
}

func (t *testingWriteCloser) Close() error {
t.closed = true
return nil
}

// NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the
// provided Writer w. Like io.ReadCloser but for writing.
func NopWriteCloser(w io.Writer) io.WriteCloser {
Expand Down

0 comments on commit 0f9789d

Please sign in to comment.