github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/parallel-writer/buffer_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package writer 16 17 import ( 18 "fmt" 19 ) 20 21 type testWriter struct { 22 capacity int 23 } 24 25 func (w *testWriter) Write(data interface{}) error { 26 fmt.Printf("Write data ok") 27 return nil 28 } 29 30 func (w *testWriter) WriteN(data ...interface{}) (int, error) { 31 if w.capacity <= 0 { 32 err := fmt.Errorf("buffer max capacity") 33 return 0, err 34 } 35 w.capacity -= len(data) 36 fmt.Printf("WriteN size: %d, data: %v\n", len(data), data) 37 return len(data), nil 38 } 39 40 func (w *testWriter) Close() error { 41 fmt.Println("Close") 42 return nil 43 } 44 45 func ExampleBuffer() { 46 buf := NewBuffer(&testWriter{2}, 4) 47 n, err := buf.WriteN(1, 2, 3, 4, 5, 6, 7, 8, 9) 48 fmt.Printf("writes: %d, buffered: %v, err: %s\n", n, buf.buf, err) 49 50 err = buf.Close() 51 fmt.Println(err) 52 53 // Output: 54 // WriteN size: 4, data: [1 2 3 4] 55 // writes: 8, buffered: [5 6 7 8], err: buffer max capacity 56 // Close 57 // buffer max capacity 58 } 59 60 func ExampleBuffer_write() { 61 buf := NewBuffer(&testWriter{10}, 3) 62 n, err := buf.WriteN(1, 2, 3, 4, 5) 63 fmt.Printf("writes: %d, buffered: %v, err: %v\n", n, buf.buf, err) 64 65 n, err = buf.WriteN(6) 66 fmt.Printf("writes: %d, buffered: %v, err: %v\n", n, buf.buf, err) 67 68 n, err = buf.WriteN(7, 8, 9) 69 fmt.Printf("writes: %d, buffered: %v, err: %v\n", n, buf.buf, err) 70 71 n, err = buf.WriteN(10, 11) 72 fmt.Printf("writes: %d, buffered: %v, err: %v\n", n, buf.buf, err) 73 74 n, err = buf.WriteN(10, 11) 75 fmt.Printf("writes: %d, buffered: %v, err: %v\n", n, buf.buf, err) 76 77 // Output: 78 // WriteN size: 3, data: [1 2 3] 79 // writes: 5, buffered: [4 5], err: <nil> 80 // WriteN size: 3, data: [4 5 6] 81 // writes: 1, buffered: [], err: <nil> 82 // writes: 3, buffered: [7 8 9], err: <nil> 83 // WriteN size: 3, data: [7 8 9] 84 // writes: 2, buffered: [10 11], err: <nil> 85 // WriteN size: 3, data: [10 11 10] 86 // writes: 2, buffered: [11], err: <nil> 87 } 88 89 func ExampleBuffer_for() { 90 buf := NewBuffer(&testWriter{100}, 3) 91 data := make([]interface{}, 10, 10) 92 for i := range data { 93 data[i] = i 94 } 95 for i := 0; i < 10; i++ { 96 n, err := buf.WriteN(data[0 : 1+i%10]...) 97 if err != nil { 98 fmt.Println(n, buf.buf, err) 99 break 100 } 101 } 102 err := buf.Close() 103 fmt.Println(buf.buf, err) 104 105 // Output: 106 // WriteN size: 3, data: [0 0 1] 107 // WriteN size: 3, data: [0 1 2] 108 // WriteN size: 3, data: [0 1 2] 109 // WriteN size: 3, data: [3 0 1] 110 // WriteN size: 3, data: [2 3 4] 111 // WriteN size: 3, data: [0 1 2] 112 // WriteN size: 3, data: [3 4 5] 113 // WriteN size: 3, data: [0 1 2] 114 // WriteN size: 3, data: [3 4 5] 115 // WriteN size: 3, data: [6 0 1] 116 // WriteN size: 3, data: [2 3 4] 117 // WriteN size: 3, data: [5 6 7] 118 // WriteN size: 3, data: [0 1 2] 119 // WriteN size: 3, data: [3 4 5] 120 // WriteN size: 3, data: [6 7 8] 121 // WriteN size: 3, data: [0 1 2] 122 // WriteN size: 3, data: [3 4 5] 123 // WriteN size: 3, data: [6 7 8] 124 // WriteN size: 1, data: [9] 125 // Close 126 // [] <nil> 127 }