TODO(adg): test batch mutations TODO(adg): test auto-flush behavior
(t *testing.T)
| 26 | // TODO(adg): test auto-flush behavior |
| 27 | |
| 28 | func TestBuffer(t *testing.T) { |
| 29 | var ( |
| 30 | toBack = []mod{ |
| 31 | {false, "b", "b1"}, |
| 32 | {false, "d", "d1"}, |
| 33 | {false, "f", "f1"}, |
| 34 | } |
| 35 | toBuf = []mod{ |
| 36 | {false, "a", "a2"}, |
| 37 | {false, "b", "b2"}, |
| 38 | {false, "c", "c2"}, |
| 39 | {false, "e", "e2"}, |
| 40 | {true, "f", ""}, |
| 41 | {false, "g", "g2"}, |
| 42 | } |
| 43 | backBeforeFlush = []mod{ |
| 44 | {false, "b", "b1"}, |
| 45 | {false, "d", "d1"}, |
| 46 | // f deleted |
| 47 | } |
| 48 | want = []mod{ |
| 49 | {false, "a", "a2"}, |
| 50 | {false, "b", "b2"}, |
| 51 | {false, "c", "c2"}, |
| 52 | {false, "d", "d1"}, |
| 53 | {false, "e", "e2"}, |
| 54 | // f deleted |
| 55 | {false, "g", "g2"}, |
| 56 | } |
| 57 | ) |
| 58 | |
| 59 | // Populate backing storage. |
| 60 | backing := sorted.NewMemoryKeyValue() |
| 61 | for _, m := range toBack { |
| 62 | backing.Set(m.key, m.value) |
| 63 | } |
| 64 | // Wrap with buffered storage, populate. |
| 65 | buf := New(sorted.NewMemoryKeyValue(), backing, 1<<20) |
| 66 | for _, m := range toBuf { |
| 67 | if m.isDelete { |
| 68 | buf.Delete(m.key) |
| 69 | } else { |
| 70 | buf.Set(m.key, m.value) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Check contents of buffered storage. |
| 75 | check(t, buf, "buffered", want) |
| 76 | check(t, backing, "backing before flush", backBeforeFlush) |
| 77 | |
| 78 | // Flush. |
| 79 | if err := buf.Flush(); err != nil { |
| 80 | t.Fatal("flush error: ", err) |
| 81 | } |
| 82 | |
| 83 | // Check contents of backing storage. |
| 84 | check(t, backing, "backing after flush", want) |
| 85 | } |