| 1 | package buffer |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/rand" |
| 6 | "encoding/gob" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "io/ioutil" |
| 10 | "math" |
| 11 | "os" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func BenchmarkMemory(b *testing.B) { |
| 16 | buf := New(32 * 1024) |
| 17 | for i := 0; i < b.N; i++ { |
| 18 | io.Copy(buf, io.LimitReader(rand.Reader, 32*1024)) |
| 19 | io.Copy(ioutil.Discard, buf) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestPool(t *testing.T) { |
| 24 | pool := NewPool(func() Buffer { return New(10) }) |
| 25 | buf, err := pool.Get() |
| 26 | if err != nil { |
| 27 | t.Error(err) |
| 28 | } |
| 29 | buf.Write([]byte("hello world")) |
| 30 | pool.Put(buf) |
| 31 | } |
| 32 | |
| 33 | func TestMemPool(t *testing.T) { |
| 34 | p := NewMemPool(10) |
| 35 | poolTest(p, t) |
| 36 | |
| 37 | b := bytes.NewBuffer(nil) |
| 38 | enc := gob.NewEncoder(b) |
| 39 | if err := enc.Encode(&p); err != nil { |
| 40 | t.Error(err) |
| 41 | } |
| 42 | |
| 43 | var pool Pool |
| 44 | dec := gob.NewDecoder(b) |
| 45 | if err := dec.Decode(&pool); err != nil { |
| 46 | t.Error(err) |
| 47 | } |
| 48 | poolTest(pool, t) |
| 49 | } |
| 50 | |
| 51 | func poolTest(pool Pool, t *testing.T) { |
| 52 | buf, err := pool.Get() |
| 53 | if err != nil { |
| 54 | t.Error(err) |
| 55 | } |
| 56 | if n, err := buf.Write([]byte("hello world")); n != 10 { |
| 57 | t.Errorf("wrote incorrect amount") |
| 58 | } else if err == nil { |
| 59 | t.Errorf("should have been a shortwrite error here") |
| 60 | } |
| 61 | pool.Put(buf) |
| 62 | if buf.Len() > 0 { |
| 63 | t.Errorf("should have emptied the buffer") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestFilePool(t *testing.T) { |
| 68 | pool := NewFilePool(1024, "::~_bad_dir_~::") |
| 69 | buf := NewPartition(pool) |
| 70 | _, err := buf.Write([]byte("hello")) |
| 71 | if err == nil { |
| 72 | t.Error("an error was expected here") |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestFilePool2(t *testing.T) { |
| 77 | pool := NewFilePool(1024, "::~_bad_dir_~::") |
| 78 | buf := NewPartition(pool, New(0)) |
| 79 | _, err := buf.Write([]byte("hello")) |
| 80 | if err == nil { |
| 81 | t.Error("an error was expected here") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestOverflow(t *testing.T) { |
| 86 | buf := NewMulti(New(5), Discard) |
| 87 | buf.Write([]byte("Hello World")) |
| 88 | |
| 89 | b := bytes.NewBuffer(nil) |
| 90 | enc := gob.NewEncoder(b) |
| 91 | if err := enc.Encode(&buf); err != nil { |
| 92 | t.Error(err) |
| 93 | } |
| 94 | |
| 95 | var buf2 Buffer |
| 96 | dec := gob.NewDecoder(b) |
| 97 | if err := dec.Decode(&buf2); err != nil { |
| 98 | t.Error(err) |
| 99 | } |
| 100 | |
| 101 | buf = buf2 |
| 102 | |
| 103 | data, err := ioutil.ReadAll(buf) |
| 104 | if err != nil { |
| 105 | t.Error(err.Error()) |
| 106 | } |
| 107 | |
| 108 | if !bytes.Equal(data, []byte("Hello")) { |
| 109 | t.Errorf("Expected Hello got %s", string(data)) |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | func TestWriteAt(t *testing.T) { |
| 115 | var b BufferAt |
| 116 | |
| 117 | b = New(5) |
| 118 | BufferAtTester(t, b) |
| 119 | |
| 120 | file, err := ioutil.TempFile("", "buffer") |
| 121 | if err != nil { |
| 122 | t.Error(err.Error()) |
| 123 | } |
| 124 | defer os.Remove(file.Name()) |
| 125 | defer file.Close() |
| 126 | |
| 127 | b = NewFile(5, file) |
| 128 | BufferAtTester(t, b) |
| 129 | |
| 130 | b = NewMultiAt(New(1), NewFile(4, file)) |
| 131 | BufferAtTester(t, b) |
| 132 | |
| 133 | b = NewMultiAt(New(2), NewFile(3, file)) |
| 134 | BufferAtTester(t, b) |
| 135 | |
| 136 | b = NewMultiAt(New(3), NewFile(2, file)) |
| 137 | BufferAtTester(t, b) |
| 138 | |
| 139 | b = NewMultiAt(New(4), NewFile(1, file)) |
| 140 | BufferAtTester(t, b) |
| 141 | |
| 142 | b = NewSwapAt(New(1), New(5)) |
| 143 | BufferAtTester(t, b) |
| 144 | |
| 145 | b = NewSwapAt(New(2), New(5)) |
| 146 | BufferAtTester(t, b) |
| 147 | |
| 148 | b = NewSwapAt(New(3), New(5)) |
| 149 | BufferAtTester(t, b) |
| 150 | |
| 151 | b = NewSwapAt(New(4), New(5)) |
| 152 | BufferAtTester(t, b) |
| 153 | |
| 154 | b = NewPartitionAt(NewMemPoolAt(3)) |
| 155 | BufferAtTester(t, b) |
| 156 | |
| 157 | b = NewPartitionAt(NewFilePoolAt(3, os.TempDir())) |
| 158 | BufferAtTester(t, b) |
| 159 | |
| 160 | b = NewPartitionAt(NewMemPoolAt(1)) |
| 161 | BufferAtTester(t, b) |
| 162 | |
| 163 | b = NewPartitionAt(NewFilePoolAt(1, os.TempDir())) |
| 164 | BufferAtTester(t, b) |
| 165 | |
| 166 | b = NewPartitionAt(NewMemPoolAt(5)) |
| 167 | BufferAtTester(t, b) |
| 168 | |
| 169 | b = NewPartitionAt(NewFilePoolAt(5, os.TempDir())) |
| 170 | BufferAtTester(t, b) |
| 171 | } |
| 172 | |
| 173 | func BufferAtTester(t *testing.T, b BufferAt) { |
| 174 | t.Helper() |
| 175 | b.WriteAt([]byte("abc"), 0) |
| 176 | Compare(t, b, "abc") |
| 177 | |
| 178 | b.WriteAt([]byte("abc"), 1) |
| 179 | Compare(t, b, "aabc") |
| 180 | |
| 181 | b.WriteAt([]byte("abc"), 2) |
| 182 | Compare(t, b, "aaabc") |
| 183 | |
| 184 | b.WriteAt([]byte("def"), 3) |
| 185 | switch { |
| 186 | case b.Cap() > 5: |
| 187 | Compare(t, b, "aaadef") |
| 188 | default: |
| 189 | Compare(t, b, "aaade") |
| 190 | } |
| 191 | |
| 192 | b.Read(make([]byte, 2)) |
| 193 | switch { |
| 194 | case b.Cap() > 5: |
| 195 | Compare(t, b, "adef") |
| 196 | default: |
| 197 | Compare(t, b, "ade") |
| 198 | } |
| 199 | |
| 200 | b.WriteAt([]byte("ab"), 3) |
| 201 | Compare(t, b, "adeab") |
| 202 | |
| 203 | b.Reset() |
| 204 | } |
| 205 | |
| 206 | func Compare(t *testing.T, b BufferAt, s string) { |
| 207 | t.Helper() |
| 208 | data := make([]byte, b.Len()) |
| 209 | n, _ := b.ReadAt(data, 0) |
| 210 | if string(data[:n]) != s { |
| 211 | t.Errorf("Mismatch: got %q want %q", string(data[:n]), s) |
| 212 | } |
| 213 | off := int64(len(s) / 2) |
| 214 | n, _ = b.ReadAt(data, off) |
| 215 | if string(data[:n]) != s[off:] { |
| 216 | t.Errorf("Mismatch: got %q want %q", string(data[:n]), s[off:]) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestRingComplex(t *testing.T) { |
| 221 | ringSize := 10 |
| 222 | data := []byte("hello!") |
| 223 | ring := NewRing(New(int64(ringSize))) |
| 224 | buf := make([]byte, ringSize) |
| 225 | |
| 226 | for i := 0; i < 10; i++ { |
| 227 | ring.Write(data) |
| 228 | n, err := ring.Read(buf) |
| 229 | if err != nil { |
| 230 | t.Errorf("unexpected error %s", err) |
| 231 | } else if !bytes.Equal(buf[:n], data) { |
| 232 | t.Errorf("expected %s got %s", data, buf[:n]) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // we are going to overflow the buffer here, we expect the last 10 bytes to remain |
| 237 | repData := bytes.Repeat(data, 3) |
| 238 | leftover := repData[len(repData)-ringSize:] |
| 239 | for i := 0; i < 10; i++ { |
| 240 | ring.Write(repData) |
| 241 | n, err := ring.Read(buf) |
| 242 | if err != nil { |
| 243 | t.Errorf("unexpected error %s", err) |
| 244 | } else if !bytes.Equal(buf[:n], leftover) { |
| 245 | t.Errorf("expected %s got %s", leftover, buf[:n]) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func TestRing(t *testing.T) { |
| 251 | ring := NewRing(New(3)) |
| 252 | if ring.Len() != 0 { |
| 253 | t.Errorf("Ring non-empty start!") |
| 254 | } |
| 255 | |
| 256 | if ring.Cap() != math.MaxInt64 { |
| 257 | t.Errorf("Ring has < max capacity") |
| 258 | } |
| 259 | |
| 260 | ring.Write([]byte("abc")) |
| 261 | if ring.Len() != 3 { |
| 262 | t.Errorf("expected ring len == 3") |
| 263 | } |
| 264 | |
| 265 | ring.Write([]byte("de")) |
| 266 | if ring.Len() != 3 { |
| 267 | t.Errorf("expected ring len == 3") |
| 268 | } |
| 269 | |
| 270 | data := make([]byte, 12) |
| 271 | if n, err := ring.Read(data); err != nil { |
| 272 | t.Error(err.Error()) |
| 273 | } else { |
| 274 | if !bytes.Equal(data[:n], []byte("cde")) { |
| 275 | t.Errorf("expected cde, got %s", data[:n]) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if ring.Len() != 0 { |
| 280 | t.Errorf("ring should now be empty") |
| 281 | } |
| 282 | |
| 283 | ring.Write([]byte("hello")) |
| 284 | ring.Reset() |
| 285 | |
| 286 | if ring.Len() != 0 { |
| 287 | t.Errorf("ring should still be empty") |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | func TestGob(t *testing.T) { |
| 292 | str := "HelloWorld" |
| 293 | |
| 294 | buf := NewUnboundedBuffer(2, 2) |
| 295 | buf.Write([]byte(str)) |
| 296 | b := bytes.NewBuffer(nil) |
| 297 | if err := gob.NewEncoder(b).Encode(&buf); err != nil { |
| 298 | t.Error(err.Error()) |
| 299 | return |
| 300 | } |
| 301 | var buffer Buffer |
| 302 | if err := gob.NewDecoder(b).Decode(&buffer); err != nil { |
| 303 | t.Error(err.Error()) |
| 304 | return |
| 305 | } |
| 306 | data := make([]byte, len(str)) |
| 307 | if _, err := buffer.Read(data); err != nil && err != io.EOF { |
| 308 | t.Error(err.Error()) |
| 309 | } |
| 310 | if !bytes.Equal(data, []byte(str)) { |
| 311 | t.Error("Gob Recover Failed... " + string(data)) |
| 312 | } |
| 313 | buffer.Reset() |
| 314 | } |
| 315 | |
| 316 | func TestDiscard(t *testing.T) { |
| 317 | buf := Discard |
| 318 | if buf.Cap() != math.MaxInt64 { |
| 319 | t.Errorf("cap isn't infinite") |
| 320 | } |
| 321 | buf.Write([]byte("hello")) |
| 322 | if buf.Len() != 0 { |
| 323 | t.Errorf("buf should always be empty") |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | func TestList(t *testing.T) { |
| 328 | mem := New(10) |
| 329 | ory := New(10) |
| 330 | mem.Write([]byte("Hello")) |
| 331 | ory.Write([]byte("world")) |
| 332 | |
| 333 | buf := List([]Buffer{mem, ory, Discard}) |
| 334 | if buf.Len() != 10 { |
| 335 | t.Errorf("incorrect sum of lengths") |
| 336 | } |
| 337 | if buf.Cap() != math.MaxInt64 { |
| 338 | t.Errorf("incorrect sum of caps") |
| 339 | } |
| 340 | |
| 341 | buf.Reset() |
| 342 | if buf.Len() != 0 { |
| 343 | t.Errorf("buffer should be empty") |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | func TestList2(t *testing.T) { |
| 348 | mem := New(10) |
| 349 | ory := New(10) |
| 350 | mem.Write([]byte("Hello")) |
| 351 | ory.Write([]byte("world")) |
| 352 | |
| 353 | buf := List([]Buffer{mem, ory}) |
| 354 | if buf.Len() != 10 { |
| 355 | t.Errorf("incorrect sum of lengths") |
| 356 | } |
| 357 | if buf.Cap() != 20 { |
| 358 | t.Errorf("incorrect sum of caps") |
| 359 | } |
| 360 | |
| 361 | buf.Reset() |
| 362 | if buf.Len() != 0 { |
| 363 | t.Errorf("buffer should be empty") |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | func TestSpill(t *testing.T) { |
| 368 | buf := NewSpill(New(5), Discard) |
| 369 | buf.Write([]byte("Hello World")) |
| 370 | data := make([]byte, 12) |
| 371 | n, _ := buf.Read(data) |
| 372 | if !bytes.Equal(data[:n], []byte("Hello")) { |
| 373 | t.Error("ReadAt Failed. " + string(data[:n])) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | func TestSpill2(t *testing.T) { |
| 378 | buf := NewSpill(New(5), nil) |
| 379 | |
| 380 | if buf.Cap() != math.MaxInt64 { |
| 381 | t.Errorf("cap isn't infinite") |
| 382 | } |
| 383 | |
| 384 | towrite := []byte("Hello World") |
| 385 | m, _ := buf.Write(towrite) |
| 386 | if m != len(towrite) { |
| 387 | t.Errorf("failed to write all data: %d != %d", m, len(towrite)) |
| 388 | } |
| 389 | data := make([]byte, 12) |
| 390 | n, _ := buf.Read(data) |
| 391 | if !bytes.Equal(data[:n], []byte("Hello")) { |
| 392 | t.Error("Read Failed. " + string(data[:n])) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | func TestNoSpill(t *testing.T) { |
| 397 | buf := NewSpill(New(1024), nil) |
| 398 | buf.Write([]byte("Hello World")) |
| 399 | data := make([]byte, 12) |
| 400 | n, _ := buf.Read(data) |
| 401 | if !bytes.Equal(data[:n], []byte("Hello World")) { |
| 402 | t.Error("Read Failed. " + string(data[:n])) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | func TestFile(t *testing.T) { |
| 407 | file, err := ioutil.TempFile("", "buffer") |
| 408 | if err != nil { |
| 409 | t.Error(err.Error()) |
| 410 | } |
| 411 | defer os.Remove(file.Name()) |
| 412 | defer file.Close() |
| 413 | |
| 414 | buf := NewFile(1024, file) |
| 415 | checkCap(t, buf, 1024) |
| 416 | runPerfectSeries(t, buf) |
| 417 | buf.Reset() |
| 418 | |
| 419 | buf = NewFile(3, file) |
| 420 | buf.Write([]byte("abc")) |
| 421 | buf.Read(make([]byte, 1)) |
| 422 | buf.Write([]byte("a")) |
| 423 | d, _ := ioutil.ReadAll(buf) |
| 424 | if !bytes.Equal(d, []byte("bca")) { |
| 425 | t.Error("back and forth error!") |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | func TestMem(t *testing.T) { |
| 430 | buf := New(1024) |
| 431 | checkCap(t, buf, 1024) |
| 432 | runPerfectSeries(t, buf) |
| 433 | buf.Reset() |
| 434 | if n, err := buf.WriteAt([]byte("hello"), 1); err == nil || n != 0 { |
| 435 | t.Errorf("write should have failed") |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | func TestFilePartition(t *testing.T) { |
| 440 | buf := NewPartition(NewFilePool(1024, "")) |
| 441 | checkCap(t, buf, math.MaxInt64) |
| 442 | runPerfectSeries(t, buf) |
| 443 | buf.Reset() |
| 444 | } |
| 445 | |
| 446 | func TestSmallMulti(t *testing.T) { |
| 447 | if empty := NewMulti(); empty != nil { |
| 448 | t.Errorf("the empty buffer should return nil") |
| 449 | } |
| 450 | |
| 451 | one := NewMulti(New(10)) |
| 452 | if one.Len() != 0 { |
| 453 | t.Errorf("singleton multi doesn't match inner buffer len") |
| 454 | } |
| 455 | if one.Cap() != 10 { |
| 456 | t.Errorf("singleton multi doesn't match inner buffer cap") |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | func TestMulti(t *testing.T) { |
| 461 | file, err := ioutil.TempFile("", "buffer") |
| 462 | if err != nil { |
| 463 | t.Error(err.Error()) |
| 464 | } |
| 465 | defer os.Remove(file.Name()) |
| 466 | defer file.Close() |
| 467 | |
| 468 | buf := NewMulti(New(5), New(5), NewFile(500, file), NewPartition(NewFilePool(1024, ""))) |
| 469 | checkCap(t, buf, math.MaxInt64) |
| 470 | runPerfectSeries(t, buf) |
| 471 | isPerfectMatch(t, buf, 1024*1024) |
| 472 | buf.Reset() |
| 473 | } |
| 474 | |
| 475 | func TestMulti2(t *testing.T) { |
| 476 | file, err := ioutil.TempFile("", "buffer") |
| 477 | if err != nil { |
| 478 | t.Error(err.Error()) |
| 479 | } |
| 480 | defer os.Remove(file.Name()) |
| 481 | defer file.Close() |
| 482 | |
| 483 | buf := NewMulti(New(5), New(5), NewFile(500, file)) |
| 484 | checkCap(t, buf, 510) |
| 485 | runPerfectSeries(t, buf) |
| 486 | isPerfectMatch(t, buf, 1024*1024) |
| 487 | buf.Reset() |
| 488 | } |
| 489 | |
| 490 | func runPerfectSeries(t *testing.T, buf Buffer) { |
| 491 | checkEmpty(t, buf) |
| 492 | simple(t, buf) |
| 493 | |
| 494 | max := int64(1024) |
| 495 | isPerfectMatch(t, buf, 0) |
| 496 | for i := int64(1); i < max; i *= 2 { |
| 497 | isPerfectMatch(t, buf, i) |
| 498 | } |
| 499 | isPerfectMatch(t, buf, max) |
| 500 | } |
| 501 | |
| 502 | func simple(t *testing.T, buf Buffer) { |
| 503 | buf.Write([]byte("hello world")) |
| 504 | data, err := ioutil.ReadAll(buf) |
| 505 | if err != nil { |
| 506 | t.Error(err.Error()) |
| 507 | } |
| 508 | if !bytes.Equal([]byte("hello world"), data) { |
| 509 | t.Error("Hello world failed.") |
| 510 | } |
| 511 | |
| 512 | buf.Write([]byte("hello world")) |
| 513 | data = make([]byte, 3) |
| 514 | buf.Read(data) |
| 515 | buf.Write([]byte(" yolo")) |
| 516 | data, err = ioutil.ReadAll(buf) |
| 517 | if err != nil { |
| 518 | t.Error(err.Error()) |
| 519 | } |
| 520 | if !bytes.Equal([]byte("lo world yolo"), data) { |
| 521 | t.Error("Buffer crossing error :(", string(data)) |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | func buildOutputs(t *testing.T, buf Buffer, size int64) (wrote []byte, read []byte) { |
| 526 | r := io.LimitReader(rand.Reader, size) |
| 527 | tee := io.TeeReader(r, buf) |
| 528 | |
| 529 | wrote, _ = ioutil.ReadAll(tee) |
| 530 | read, _ = ioutil.ReadAll(buf) |
| 531 | |
| 532 | return wrote, read |
| 533 | } |
| 534 | |
| 535 | func isPerfectMatch(t *testing.T, buf Buffer, size int64) { |
| 536 | wrote, read := buildOutputs(t, buf, size) |
| 537 | if !bytes.Equal(wrote, read) { |
| 538 | t.Error("Buffer should have matched") |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | func checkEmpty(t *testing.T, buf Buffer) { |
| 543 | if !Empty(buf) { |
| 544 | t.Error("Buffer should start empty!") |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | func checkCap(t *testing.T, buf Buffer, correctCap int64) { |
| 549 | if buf.Cap() != correctCap { |
| 550 | t.Error("Buffer cap is incorrect", buf.Cap(), correctCap) |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | type bigBuffer struct{} |
| 555 | |
| 556 | func (b bigBuffer) Len() int64 { return math.MaxInt64 } |
| 557 | |
| 558 | func (b bigBuffer) Cap() int64 { return math.MaxInt64 } |
| 559 | |
| 560 | func (b bigBuffer) Read(p []byte) (int, error) { return 0, io.ErrUnexpectedEOF } |
| 561 | |
| 562 | func (b bigBuffer) Write(p []byte) (int, error) { return 0, io.ErrShortBuffer } |
| 563 | |
| 564 | func (b bigBuffer) Reset() {} |
| 565 | |
| 566 | func TestBigList(t *testing.T) { |
| 567 | l := List([]Buffer{bigBuffer{}, bigBuffer{}}) |
| 568 | if l.Len() != math.MaxInt64 { |
| 569 | t.Errorf("expected list to be max-size: %d", l.Len()) |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | func TestBigMulti(t *testing.T) { |
| 574 | single := NewMulti(bigBuffer{}) |
| 575 | if single.Len() != math.MaxInt64 { |
| 576 | t.Errorf("expected len to be max-size: %d", single.Len()) |
| 577 | } |
| 578 | if single.Cap() != math.MaxInt64 { |
| 579 | t.Errorf("expected cap to be max-size: %d", single.Cap()) |
| 580 | } |
| 581 | |
| 582 | buf := NewMulti(bigBuffer{}, bigBuffer{}) |
| 583 | if buf.Len() != math.MaxInt64 { |
| 584 | t.Errorf("expected len to be max-size: %d", buf.Len()) |
| 585 | } |
| 586 | if buf.Cap() != math.MaxInt64 { |
| 587 | t.Errorf("expected cap to be max-size: %d", buf.Cap()) |
| 588 | } |
| 589 | |
| 590 | // test defrag failure |
| 591 | NewMulti(badBuffer{}, badBuffer{}) |
| 592 | } |
| 593 | |
| 594 | type badBuffer struct{} |
| 595 | |
| 596 | func (b badBuffer) Len() int64 { return 1024 } |
| 597 | |
| 598 | func (b badBuffer) Cap() int64 { return 10 * 1024 } |
| 599 | |
| 600 | func (b badBuffer) Read(p []byte) (int, error) { return 0, io.ErrUnexpectedEOF } |
| 601 | |
| 602 | func (b badBuffer) Write(p []byte) (int, error) { return 0, io.ErrShortBuffer } |
| 603 | |
| 604 | func (b badBuffer) Reset() {} |
| 605 | |
| 606 | func (b badBuffer) MarshalBinary() ([]byte, error) { |
| 607 | return nil, errors.New("no data") |
| 608 | } |
| 609 | |
| 610 | func TestBadPartition(t *testing.T) { |
| 611 | p := NewPool(func() Buffer { return badBuffer{} }) |
| 612 | buf := NewPartition(p) |
| 613 | _, err := buf.Write([]byte("bad write")) |
| 614 | if err != io.ErrShortBuffer { |
| 615 | t.Errorf("wrong read error was returned! %v", err) |
| 616 | } |
| 617 | |
| 618 | b := make([]byte, 1024) |
| 619 | _, err = buf.Read(b) |
| 620 | if err != io.ErrUnexpectedEOF { |
| 621 | t.Errorf("wrong write error was returned! %v", err) |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | type fakeFile struct { |
| 626 | name string |
| 627 | } |
| 628 | |
| 629 | func (f *fakeFile) Name() string { |
| 630 | return f.name |
| 631 | } |
| 632 | |
| 633 | func (f *fakeFile) Stat() (fi os.FileInfo, err error) { |
| 634 | return nil, nil |
| 635 | } |
| 636 | |
| 637 | func (f *fakeFile) ReadAt(p []byte, off int64) (int, error) { |
| 638 | return 0, io.EOF |
| 639 | } |
| 640 | |
| 641 | func (f *fakeFile) WriteAt(p []byte, off int64) (int, error) { |
| 642 | return 0, io.ErrShortWrite |
| 643 | } |
| 644 | |
| 645 | func (f *fakeFile) Close() error { return nil } |
| 646 | |
| 647 | func TestBadGobFile(t *testing.T) { |
| 648 | b := NewFile(10, &fakeFile{name: "test"}) |
| 649 | b2 := NewFile(10, &fakeFile{name: "test2"}) |
| 650 | b3 := NewMulti(b, b2) |
| 651 | buf := bytes.NewBuffer(nil) |
| 652 | enc := gob.NewEncoder(buf) |
| 653 | if err := enc.Encode(&b3); err != nil { |
| 654 | t.Error(err) |
| 655 | } |
| 656 | var buffer Buffer |
| 657 | dec := gob.NewDecoder(buf) |
| 658 | if err := dec.Decode(&buffer); err == nil { |
| 659 | t.Error("expected an error here, file does not exist") |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | func TestBadGobFile2(t *testing.T) { |
| 664 | b := New(10) |
| 665 | b2 := NewFile(10, &fakeFile{name: "test2"}) |
| 666 | b3 := NewMulti(b, b2) |
| 667 | buf := bytes.NewBuffer(nil) |
| 668 | enc := gob.NewEncoder(buf) |
| 669 | if err := enc.Encode(&b3); err != nil { |
| 670 | t.Error(err) |
| 671 | } |
| 672 | var buffer Buffer |
| 673 | dec := gob.NewDecoder(buf) |
| 674 | if err := dec.Decode(&buffer); err == nil { |
| 675 | t.Error("expected an error here, file does not exist") |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | func TestBadMultiGob(t *testing.T) { |
| 680 | var b Buffer = badBuffer{} |
| 681 | var b2 Buffer = badBuffer{} |
| 682 | var b3 Buffer = New(10) |
| 683 | b4 := NewMulti(b, b3) |
| 684 | b5 := NewMulti(b3, b2) |
| 685 | |
| 686 | enc := gob.NewEncoder(ioutil.Discard) |
| 687 | if err := enc.Encode(&b4); err == nil { |
| 688 | t.Error("expected an error here, bad buffer can't be gobbed") |
| 689 | } |
| 690 | if err := enc.Encode(&b5); err == nil { |
| 691 | t.Error("expected an error here, bad buffer can't be gobbed") |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | func TestSwapAt(t *testing.T) { |
| 696 | buf := NewSwapAt(New(5), New(10)) |
| 697 | buf.WriteAt([]byte("hey"), 0) |
| 698 | data := make([]byte, 10) |
| 699 | n, _ := buf.ReadAt(data, 0) |
| 700 | if string(data[:n]) != "hey" { |
| 701 | t.Error("expected hey got", string(data[:n])) |
| 702 | } |
| 703 | buf.WriteAt([]byte("hey"), 3) |
| 704 | n, _ = buf.ReadAt(data, 1) |
| 705 | if string(data[:n]) != "eyhey" { |
| 706 | t.Error("expected eyhey got", string(data[:n])) |
| 707 | } |
| 708 | buf.WriteAt([]byte("hey"), 5) |
| 709 | n, _ = buf.ReadAt(data, 1) |
| 710 | if string(data[:n]) != "eyhehey" { |
| 711 | t.Error("expected eyhehey got", string(data[:n])) |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | func TestSwap(t *testing.T) { |
| 716 | defer func() { |
| 717 | recover() |
| 718 | }() |
| 719 | buf := NewSwap(New(5), New(10)) |
| 720 | if buf.Len() != 0 { |
| 721 | t.Error("buffer should start empty got", buf.Len()) |
| 722 | } |
| 723 | if buf.Cap() != 10 { |
| 724 | t.Error("cap should be second buffer size (10) got", buf.Cap()) |
| 725 | } |
| 726 | io.WriteString(buf, "hey") |
| 727 | data, _ := ioutil.ReadAll(buf) |
| 728 | if string(data) != "hey" { |
| 729 | t.Error("expected hey got", string(data)) |
| 730 | } |
| 731 | io.WriteString(buf, "hey") |
| 732 | io.WriteString(buf, "hey") |
| 733 | io.WriteString(buf, "hey") |
| 734 | data, _ = ioutil.ReadAll(buf) |
| 735 | if string(data) != "heyheyhey" { |
| 736 | t.Error("expected heyheyhey got", string(data)) |
| 737 | } |
| 738 | io.WriteString(buf, "hey") |
| 739 | if buf.Len() != 3 { |
| 740 | t.Error("should have data") |
| 741 | } |
| 742 | buf.Reset() |
| 743 | if buf.Len() != 0 { |
| 744 | t.Error("should be empty") |
| 745 | } |
| 746 | |
| 747 | runPerfectSeries(t, NewSwap(New(512), New(1024))) |
| 748 | |
| 749 | NewSwap(New(1), New(0)) |
| 750 | t.Error("expected panic") |
| 751 | } |
| 752 | |
| 753 | func TestPanicReadAt(t *testing.T) { |
| 754 | defer func() { |
| 755 | recover() |
| 756 | }() |
| 757 | buf := toBufferAt(New(5)) |
| 758 | buf.ReadAt(nil, 0) |
| 759 | t.Error("expected a panic!") |
| 760 | } |
| 761 | |
| 762 | func TestPanicWriteAt(t *testing.T) { |
| 763 | defer func() { |
| 764 | recover() |
| 765 | }() |
| 766 | buf := toBufferAt(New(5)) |
| 767 | buf.WriteAt(nil, 0) |
| 768 | t.Error("expected a panic!") |
| 769 | } |
| 770 | |
| 771 | type badFile struct{} |
| 772 | |
| 773 | func (b badFile) Name() string { return "" } |
| 774 | func (b badFile) Stat() (os.FileInfo, error) { return nil, errors.New("unsupported") } |
| 775 | func (b badFile) ReadAt(p []byte, off int64) (int, error) { return 0, nil } |
| 776 | func (b badFile) WriteAt(p []byte, off int64) (int, error) { return len(p), nil } |
| 777 | func (b badFile) Close() error { return nil } |
| 778 | |
| 779 | func TestWrapioBreakout(t *testing.T) { |
| 780 | buf := NewFile(10, badFile{}) |
| 781 | io.WriteString(buf, "hello world") |
| 782 | if _, err := ioutil.ReadAll(buf); err != io.ErrNoProgress { |
| 783 | t.Error("expected no progress to be made") |
| 784 | t.FailNow() |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | func TestListAt(t *testing.T) { |
| 789 | mem := New(10) |
| 790 | ory := New(10) |
| 791 | mem.Write([]byte("Hello")) |
| 792 | ory.Write([]byte("world")) |
| 793 | |
| 794 | buf := ListAt([]BufferAt{mem, ory}) |
| 795 | if buf.Len() != 10 { |
| 796 | t.Errorf("incorrect sum of lengths") |
| 797 | } |
| 798 | if buf.Cap() != 20 { |
| 799 | t.Errorf("incorrect sum of caps") |
| 800 | } |
| 801 | |
| 802 | buf.Reset() |
| 803 | if buf.Len() != 0 { |
| 804 | t.Errorf("buffer should be empty") |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | func TestPoolAt(t *testing.T) { |
| 809 | pool := NewPoolAt(func() BufferAt { return New(10) }) |
| 810 | buf, err := pool.Get() |
| 811 | if err != nil { |
| 812 | t.Error(err) |
| 813 | } |
| 814 | buf.Write([]byte("hello world")) |
| 815 | pool.Put(buf) |
| 816 | } |
| 817 | |
| 818 | func TestMemPoolAt(t *testing.T) { |
| 819 | p := NewMemPoolAt(10) |
| 820 | poolAtTest(p, t) |
| 821 | |
| 822 | b := bytes.NewBuffer(nil) |
| 823 | enc := gob.NewEncoder(b) |
| 824 | if err := enc.Encode(&p); err != nil { |
| 825 | t.Error(err) |
| 826 | } |
| 827 | |
| 828 | var pool PoolAt |
| 829 | dec := gob.NewDecoder(b) |
| 830 | if err := dec.Decode(&pool); err != nil { |
| 831 | t.Error(err) |
| 832 | } |
| 833 | poolAtTest(pool, t) |
| 834 | } |
| 835 | |
| 836 | func poolAtTest(pool PoolAt, t *testing.T) { |
| 837 | buf, err := pool.Get() |
| 838 | if err != nil { |
| 839 | t.Error(err) |
| 840 | } |
| 841 | if n, err := buf.Write([]byte("hello world")); n != 10 { |
| 842 | t.Errorf("wrote incorrect amount") |
| 843 | } else if err == nil { |
| 844 | t.Errorf("should have been a shortwrite error here") |
| 845 | } |
| 846 | pool.Put(buf) |
| 847 | if buf.Len() > 0 { |
| 848 | t.Errorf("should have emptied the buffer") |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | func TestPartitionAt(t *testing.T) { |
| 853 | buf := NewPartitionAt(NewMemPoolAt(5)) |
| 854 | buf.Write([]byte("Hello World")) |
| 855 | data := make([]byte, 12) |
| 856 | n, _ := buf.Read(data) |
| 857 | if !bytes.Equal(data[:n], []byte("Hello World")) { |
| 858 | t.Error("Read Failed. " + string(data[:n])) |
| 859 | } |
| 860 | |
| 861 | checkCap(t, buf, math.MaxInt64) |
| 862 | runPerfectSeries(t, buf) |
| 863 | } |
| 864 | |
| 865 | // TestPartitionAt2 tests ability to read at various offsets from buffer previously written. |
| 866 | func TestPartitionAt2(t *testing.T) { |
| 867 | buf := NewPartitionAt(NewMemPoolAt(5)) |
| 868 | buf.Write([]byte("Hello World")) |
| 869 | data := make([]byte, 2) |
| 870 | n, _ := buf.ReadAt(data, 2) |
| 871 | if !bytes.Equal(data[:n], []byte("ll")) { |
| 872 | t.Error("Read Failed. " + string(data[:n])) |
| 873 | } |
| 874 | n, _ = buf.ReadAt(data, 4) |
| 875 | if !bytes.Equal(data[:n], []byte("o ")) { |
| 876 | t.Error("Read Failed. " + string(data[:n])) |
| 877 | } |
| 878 | n, _ = buf.ReadAt(data, 10) |
| 879 | if !bytes.Equal(data[:n], []byte("d")) { |
| 880 | t.Error("Read Failed. " + string(data[:n])) |
| 881 | } |
| 882 | n, _ = buf.ReadAt(data, 100) |
| 883 | if !bytes.Equal(data[:n], []byte{}) { |
| 884 | t.Error("Read Failed. " + string(data[:n])) |
| 885 | } |
| 886 | |
| 887 | buf.Reset() |
| 888 | checkCap(t, buf, math.MaxInt64) |
| 889 | runPerfectSeries(t, buf) |
| 890 | } |
| 891 | |
| 892 | // TestPartitionAt3 tests ability to overwrite buffer previously written. |
| 893 | func TestPartitionAt3(t *testing.T) { |
| 894 | buf := NewPartitionAt(NewMemPoolAt(5)) |
| 895 | buf.Write(make([]byte, 15, 15)) // allocates 3 membuffers |
| 896 | buf.WriteAt([]byte("hey"), 0) |
| 897 | data := make([]byte, 10) |
| 898 | data = data[:3] |
| 899 | buf.ReadAt(data, 0) |
| 900 | if string(data) != "hey" { |
| 901 | t.Error("expected hey got", string(data)) |
| 902 | } |
| 903 | buf.WriteAt([]byte("hey"), 3) |
| 904 | data = data[:5] |
| 905 | buf.ReadAt(data, 1) |
| 906 | if string(data) != "eyhey" { |
| 907 | t.Error("expected eyhey got", string(data)) |
| 908 | } |
| 909 | buf.WriteAt([]byte("hey"), 5) |
| 910 | data = data[:7] |
| 911 | buf.ReadAt(data, 1) |
| 912 | if string(data) != "eyhehey" { |
| 913 | t.Error("expected eyhehey got", string(data)) |
| 914 | } |
| 915 | } |