...

Source file src/github.com/peterbourgon/diskv/v3/stream_test.go

Documentation: github.com/peterbourgon/diskv/v3

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

View as plain text