(t *testing.T)
| 127 | } |
| 128 | |
| 129 | func TestStreamSectionReader(t *testing.T) { |
| 130 | buf := make([]byte, 8<<10) |
| 131 | for i := range len(buf) { |
| 132 | buf[i] = byte(i % 256) |
| 133 | } |
| 134 | f := &stream.FileStream{ |
| 135 | Obj: &model.Object{ |
| 136 | Size: int64(len(buf)), |
| 137 | }, |
| 138 | Reader: io.NopCloser(bytes.NewReader(buf)), |
| 139 | } |
| 140 | prevAutoMemoryLimit := conf.AutoMemoryLimit |
| 141 | prevMaxBlockLimit := conf.MaxBlockLimit |
| 142 | prevConf := conf.Conf |
| 143 | t.Cleanup(func() { |
| 144 | conf.AutoMemoryLimit = prevAutoMemoryLimit |
| 145 | conf.MaxBlockLimit = prevMaxBlockLimit |
| 146 | conf.Conf = prevConf |
| 147 | }) |
| 148 | conf.AutoMemoryLimit = 0 |
| 149 | conf.MaxBlockLimit = 2 << 10 |
| 150 | partSize := 3 << 10 |
| 151 | conf.Conf = &conf.Config{} |
| 152 | ss, err := stream.NewStreamSectionReader(f, partSize, nil) |
| 153 | if err != nil { |
| 154 | t.Errorf("NewStreamSectionReader() error = %v", err) |
| 155 | } |
| 156 | for i := 0; i < len(buf); i += partSize { |
| 157 | length := partSize |
| 158 | if i+length > len(buf) { |
| 159 | length = len(buf) - i |
| 160 | } |
| 161 | rs, err := ss.GetSectionReader(int64(i), int64(length)) |
| 162 | if err != nil { |
| 163 | t.Errorf("StreamSectionReader.GetSectionReader() error = %v", err) |
| 164 | } |
| 165 | b1, err := io.ReadAll(rs) |
| 166 | if err != nil { |
| 167 | t.Errorf("StreamSectionReader.Read() error = %v", err) |
| 168 | } |
| 169 | rs.Seek(1, io.SeekStart) |
| 170 | b2, _ := io.ReadAll(rs) |
| 171 | if !bytes.Equal(b1[1:], b2) { |
| 172 | t.Errorf("StreamSectionReader.Read() = %s, want %s", b1[1:], b2) |
| 173 | } |
| 174 | if !bytes.Equal(buf[i:i+length], b1) { |
| 175 | t.Errorf("StreamSectionReader.Read() = %s, want %s", b1, buf[i:i+length]) |
| 176 | } |
| 177 | if i == 0 { |
| 178 | prevMinFreeMemory := conf.MinFreeMemory |
| 179 | conf.MinFreeMemory = 0 // 强制使用文件缓存 |
| 180 | t.Cleanup(func() { |
| 181 | conf.MinFreeMemory = prevMinFreeMemory |
| 182 | }) |
| 183 | } |
| 184 | } |
| 185 | } |
nothing calls this directly
no test coverage detected