github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/exec/bigmachine_inv_disk_cache_test.go (about) 1 package exec 2 3 import ( 4 "io" 5 "io/ioutil" 6 "testing" 7 8 "github.com/grailbio/base/errors" 9 "github.com/grailbio/testutil/assert" 10 ) 11 12 func TestInvDiskCache(t *testing.T) { 13 c := newInvDiskCache() 14 15 rc, err := c.getOrCreate(0, func(w io.Writer) error { 16 _, err := w.Write([]byte("entry0")) 17 return err 18 }) 19 assert.NoError(t, err) 20 got, err := ioutil.ReadAll(rc) 21 assert.NoError(t, err) 22 assert.EQ(t, string(got), "entry0") 23 24 rc, err = c.getOrCreate(3, func(w io.Writer) error { 25 _, err2 := w.Write([]byte("entry3")) 26 return err2 27 }) 28 assert.NoError(t, err) 29 got, err = ioutil.ReadAll(rc) 30 assert.NoError(t, err) 31 assert.EQ(t, string(got), "entry3") 32 33 rc, err = c.getOrCreate(0, func(w io.Writer) error { 34 t.Fatal("should be cached") 35 panic("failed") 36 }) 37 assert.NoError(t, err) 38 got, err = ioutil.ReadAll(rc) 39 assert.NoError(t, err) 40 assert.EQ(t, string(got), "entry0") 41 42 _, err = c.getOrCreate(10, func(w io.Writer) error { 43 return errors.E("planned error") 44 }) 45 assert.NotNil(t, err) 46 rc, err = c.getOrCreate(10, func(w io.Writer) error { 47 _, err2 := w.Write([]byte("entry10")) 48 return err2 49 }) 50 assert.NoError(t, err) 51 got, err = ioutil.ReadAll(rc) 52 assert.NoError(t, err) 53 assert.EQ(t, string(got), "entry10") 54 55 c.close() 56 }