Debian Codegraph @

pkg/util/flushwriter/writer_test.go

raw
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package flushwriter
18
19 import (
20 "fmt"
21 "testing"
22 )
23
24 type writerWithFlush struct {
25 writeCount, flushCount int
26 err error
27 }
28
29 func (w *writerWithFlush) Flush() {
30 w.flushCount++
31 }
32
33 func (w *writerWithFlush) Write(p []byte) (n int, err error) {
34 w.writeCount++
35 return len(p), w.err
36 }
37
38 type writerWithNoFlush struct {
39 writeCount int
40 }
41
42 func (w *writerWithNoFlush) Write(p []byte) (n int, err error) {
43 w.writeCount++
44 return len(p), nil
45 }
46
47 func TestWriteWithFlush(t *testing.T) {
48 w := &writerWithFlush{}
49 fw := Wrap(w)
50 for i := 0; i < 10; i++ {
51 _, err := fw.Write([]byte("Test write"))
52 if err != nil {
53 t.Errorf("Unexpected error while writing with flush writer: %v", err)
54 }
55 }
56 if w.flushCount != 10 {
57 t.Errorf("Flush not called the expected number of times. Actual: %d", w.flushCount)
58 }
59 if w.writeCount != 10 {
60 t.Errorf("Write not called the expected number of times. Actual: %d", w.writeCount)
61 }
62 }
63
64 func TestWriteWithoutFlush(t *testing.T) {
65 w := &writerWithNoFlush{}
66 fw := Wrap(w)
67 for i := 0; i < 10; i++ {
68 _, err := fw.Write([]byte("Test write"))
69 if err != nil {
70 t.Errorf("Unexpected error while writing with flush writer: %v", err)
71 }
72 }
73 if w.writeCount != 10 {
74 t.Errorf("Write not called the expected number of times. Actual: %d", w.writeCount)
75 }
76 }
77
78 func TestWriteError(t *testing.T) {
79 e := fmt.Errorf("Error")
80 w := &writerWithFlush{err: e}
81 fw := Wrap(w)
82 _, err := fw.Write([]byte("Test write"))
83 if err != e {
84 t.Errorf("Did not get expected error. Got: %#v", err)
85 }
86 }