MCPcopy
hub / github.com/Terry-Mao/goim / TestWriterReadFromCounts

Function TestWriterReadFromCounts

pkg/bufio/bufio_test.go:587–630  ·  view source on GitHub ↗

TestWriterReadFromCounts tests that using io.Copy to copy into a bufio.Writer does not prematurely flush the buffer. For example, when buffering writes to a network socket, excessive network writes should be avoided.

(t *testing.T)

Source from the content-addressed store, hash-verified

585// buffering writes to a network socket, excessive network writes should be
586// avoided.
587func TestWriterReadFromCounts(t *testing.T) {
588 var w0 writeCountingDiscard
589 b0 := NewWriterSize(&w0, 1234)
590 _, _ = b0.WriteString(strings.Repeat("x", 1000))
591 if w0 != 0 {
592 t.Fatalf("write 1000 'x's: got %d writes, want 0", w0)
593 }
594 _, _ = b0.WriteString(strings.Repeat("x", 200))
595 if w0 != 0 {
596 t.Fatalf("write 1200 'x's: got %d writes, want 0", w0)
597 }
598 _, _ = io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 30))})
599 if w0 != 0 {
600 t.Fatalf("write 1230 'x's: got %d writes, want 0", w0)
601 }
602 _, _ = io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 9))})
603 if w0 != 1 {
604 t.Fatalf("write 1239 'x's: got %d writes, want 1", w0)
605 }
606
607 var w1 writeCountingDiscard
608 b1 := NewWriterSize(&w1, 1234)
609 _, _ = b1.WriteString(strings.Repeat("x", 1200))
610 _ = b1.Flush()
611 if w1 != 1 {
612 t.Fatalf("flush 1200 'x's: got %d writes, want 1", w1)
613 }
614 _, _ = b1.WriteString(strings.Repeat("x", 89))
615 if w1 != 1 {
616 t.Fatalf("write 1200 + 89 'x's: got %d writes, want 1", w1)
617 }
618 _, _ = io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 700))})
619 if w1 != 1 {
620 t.Fatalf("write 1200 + 789 'x's: got %d writes, want 1", w1)
621 }
622 _, _ = io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))})
623 if w1 != 2 {
624 t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1)
625 }
626 _ = b1.Flush()
627 if w1 != 3 {
628 t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1)
629 }
630}
631
632// A writeCountingDiscard is like ioutil.Discard and counts the number of times
633// Write is called on it.

Callers

nothing calls this directly

Calls 3

WriteStringMethod · 0.80
NewWriterSizeFunction · 0.70
FlushMethod · 0.45

Tested by

no test coverage detected