stream_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package diskv
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "testing"
  6. )
  7. func TestBasicStreamCaching(t *testing.T) {
  8. d := New(Options{
  9. BasePath: "test-data",
  10. CacheSizeMax: 1024,
  11. })
  12. defer d.EraseAll()
  13. input := "a1b2c3"
  14. key, writeBuf, sync := "a", bytes.NewBufferString(input), true
  15. if err := d.WriteStream(key, writeBuf, sync); err != nil {
  16. t.Fatal(err)
  17. }
  18. if d.isCached(key) {
  19. t.Fatalf("'%s' cached, but shouldn't be (yet)", key)
  20. }
  21. rc, err := d.ReadStream(key, false)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. readBuf, err := ioutil.ReadAll(rc)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if !cmpBytes(readBuf, []byte(input)) {
  30. t.Fatalf("'%s' != '%s'", string(readBuf), input)
  31. }
  32. if !d.isCached(key) {
  33. t.Fatalf("'%s' isn't cached, but should be", key)
  34. }
  35. }
  36. func TestReadStreamDirect(t *testing.T) {
  37. var (
  38. basePath = "test-data"
  39. )
  40. dWrite := New(Options{
  41. BasePath: basePath,
  42. CacheSizeMax: 0,
  43. })
  44. defer dWrite.EraseAll()
  45. dRead := New(Options{
  46. BasePath: basePath,
  47. CacheSizeMax: 1024,
  48. })
  49. // Write
  50. key, val1, val2 := "a", []byte(`1234567890`), []byte(`aaaaaaaaaa`)
  51. if err := dWrite.Write(key, val1); err != nil {
  52. t.Fatalf("during first write: %s", err)
  53. }
  54. // First, caching read.
  55. val, err := dRead.Read(key)
  56. if err != nil {
  57. t.Fatalf("during initial read: %s", err)
  58. }
  59. t.Logf("read 1: %s => %s", key, string(val))
  60. if !cmpBytes(val1, val) {
  61. t.Errorf("expected %q, got %q", string(val1), string(val))
  62. }
  63. if !dRead.isCached(key) {
  64. t.Errorf("%q should be cached, but isn't", key)
  65. }
  66. // Write a different value.
  67. if err := dWrite.Write(key, val2); err != nil {
  68. t.Fatalf("during second write: %s", err)
  69. }
  70. // Second read, should hit cache and get the old value.
  71. val, err = dRead.Read(key)
  72. if err != nil {
  73. t.Fatalf("during second (cache-hit) read: %s", err)
  74. }
  75. t.Logf("read 2: %s => %s", key, string(val))
  76. if !cmpBytes(val1, val) {
  77. t.Errorf("expected %q, got %q", string(val1), string(val))
  78. }
  79. // Third, direct read, should get the updated value.
  80. rc, err := dRead.ReadStream(key, true)
  81. if err != nil {
  82. t.Fatalf("during third (direct) read, ReadStream: %s", err)
  83. }
  84. defer rc.Close()
  85. val, err = ioutil.ReadAll(rc)
  86. if err != nil {
  87. t.Fatalf("during third (direct) read, ReadAll: %s", err)
  88. }
  89. t.Logf("read 3: %s => %s", key, string(val))
  90. if !cmpBytes(val2, val) {
  91. t.Errorf("expected %q, got %q", string(val1), string(val))
  92. }
  93. // Fourth read, should hit cache and get the new value.
  94. val, err = dRead.Read(key)
  95. if err != nil {
  96. t.Fatalf("during fourth (cache-hit) read: %s", err)
  97. }
  98. t.Logf("read 4: %s => %s", key, string(val))
  99. if !cmpBytes(val2, val) {
  100. t.Errorf("expected %q, got %q", string(val1), string(val))
  101. }
  102. }