X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2c6557f613fcf6cdcebb08c321a5d061aeb780c6..89fc1810ba39a7a0aebccba690c7bc663bca8c0b:/services/keepstore/unix_volume_test.go diff --git a/services/keepstore/unix_volume_test.go b/services/keepstore/unix_volume_test.go index a8dc4e809a..bcdb5f6358 100644 --- a/services/keepstore/unix_volume_test.go +++ b/services/keepstore/unix_volume_test.go @@ -8,9 +8,7 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" - "io" "io/ioutil" "os" "sync" @@ -23,7 +21,7 @@ import ( ) type testableUnixVolume struct { - UnixVolume + unixVolume t TB } @@ -77,14 +75,15 @@ func (s *unixVolumeSuite) newTestableUnixVolume(c *check.C, params newVolumePara locker = &sync.Mutex{} } v := &testableUnixVolume{ - UnixVolume: UnixVolume{ - Root: d, - locker: locker, - uuid: params.UUID, - cluster: params.Cluster, - logger: params.Logger, - volume: params.ConfigVolume, - metrics: params.MetricsVecs, + unixVolume: unixVolume{ + Root: d, + locker: locker, + uuid: params.UUID, + cluster: params.Cluster, + logger: params.Logger, + volume: params.ConfigVolume, + metrics: params.MetricsVecs, + bufferPool: params.BufferPool, }, t: c, } @@ -122,16 +121,9 @@ func (s *unixVolumeSuite) TestGetNotFound(c *check.C) { defer v.Teardown() v.BlockWrite(context.Background(), TestHash, TestBlock) - buf := bytes.NewBuffer(nil) - _, err := v.BlockRead(context.Background(), TestHash2, buf) - switch { - case os.IsNotExist(err): - break - case err == nil: - c.Errorf("Read should have failed, returned %+q", buf.Bytes()) - default: - c.Errorf("Read expected ErrNotExist, got: %s", err) - } + buf := &brbuffer{} + err := v.BlockRead(context.Background(), TestHash2, buf) + c.Check(err, check.FitsTypeOf, os.ErrNotExist) } func (s *unixVolumeSuite) TestPut(c *check.C) { @@ -181,94 +173,6 @@ func (s *unixVolumeSuite) TestIsFull(c *check.C) { } } -func (s *unixVolumeSuite) TestUnixVolumeGetFuncWorkerError(c *check.C) { - v := s.newTestableUnixVolume(c, s.params, false) - defer v.Teardown() - - v.BlockWrite(context.Background(), TestHash, TestBlock) - mockErr := errors.New("Mock error") - err := v.getFunc(context.Background(), v.blockPath(TestHash), func(rdr io.Reader) error { - return mockErr - }) - if err != mockErr { - c.Errorf("Got %v, expected %v", err, mockErr) - } -} - -func (s *unixVolumeSuite) TestUnixVolumeGetFuncFileError(c *check.C) { - v := s.newTestableUnixVolume(c, s.params, false) - defer v.Teardown() - - funcCalled := false - err := v.getFunc(context.Background(), v.blockPath(TestHash), func(rdr io.Reader) error { - funcCalled = true - return nil - }) - if err == nil { - c.Errorf("Expected error opening non-existent file") - } - if funcCalled { - c.Errorf("Worker func should not have been called") - } -} - -func (s *unixVolumeSuite) TestUnixVolumeGetFuncWorkerWaitsOnMutex(c *check.C) { - v := s.newTestableUnixVolume(c, s.params, false) - defer v.Teardown() - - v.BlockWrite(context.Background(), TestHash, TestBlock) - - mtx := NewMockMutex() - v.locker = mtx - - funcCalled := make(chan struct{}) - go v.getFunc(context.Background(), v.blockPath(TestHash), func(rdr io.Reader) error { - funcCalled <- struct{}{} - return nil - }) - select { - case mtx.AllowLock <- struct{}{}: - case <-funcCalled: - c.Fatal("Function was called before mutex was acquired") - case <-time.After(5 * time.Second): - c.Fatal("Timed out before mutex was acquired") - } - select { - case <-funcCalled: - case mtx.AllowUnlock <- struct{}{}: - c.Fatal("Mutex was released before function was called") - case <-time.After(5 * time.Second): - c.Fatal("Timed out waiting for funcCalled") - } - select { - case mtx.AllowUnlock <- struct{}{}: - case <-time.After(5 * time.Second): - c.Fatal("Timed out waiting for getFunc() to release mutex") - } -} - -type MockMutex struct { - AllowLock chan struct{} - AllowUnlock chan struct{} -} - -func NewMockMutex() *MockMutex { - return &MockMutex{ - AllowLock: make(chan struct{}), - AllowUnlock: make(chan struct{}), - } -} - -// Lock waits for someone to send to AllowLock. -func (m *MockMutex) Lock() { - <-m.AllowLock -} - -// Unlock waits for someone to send to AllowUnlock. -func (m *MockMutex) Unlock() { - <-m.AllowUnlock -} - func (s *unixVolumeSuite) TestUnixVolumeContextCancelBlockWrite(c *check.C) { v := s.newTestableUnixVolume(c, s.params, true) defer v.Teardown() @@ -299,9 +203,10 @@ func (s *unixVolumeSuite) TestUnixVolumeContextCancelBlockRead(c *check.C) { time.Sleep(50 * time.Millisecond) cancel() }() - n, err := v.BlockRead(ctx, TestHash, io.Discard) - if n > 0 || err != context.Canceled { - c.Errorf("BlockRead() returned %d, %s -- expected short read / canceled", n, err) + buf := &brbuffer{} + err = v.BlockRead(ctx, TestHash, buf) + if buf.Len() != 0 || err != context.Canceled { + c.Errorf("BlockRead() returned %q, %s -- expected short read / canceled", buf.String(), err) } } @@ -313,10 +218,10 @@ func (s *unixVolumeSuite) TestStats(c *check.C) { return string(buf) } - c.Check(stats(), check.Matches, `.*"StatOps":1,.*`) // (*UnixVolume)check() calls Stat() once + c.Check(stats(), check.Matches, `.*"StatOps":1,.*`) // (*unixVolume)check() calls Stat() once c.Check(stats(), check.Matches, `.*"Errors":0,.*`) - _, err := vol.BlockRead(context.Background(), fooHash, io.Discard) + err := vol.BlockRead(context.Background(), fooHash, brdiscard) c.Check(err, check.NotNil) c.Check(stats(), check.Matches, `.*"StatOps":[^0],.*`) c.Check(stats(), check.Matches, `.*"Errors":[^0],.*`) @@ -338,8 +243,8 @@ func (s *unixVolumeSuite) TestStats(c *check.C) { c.Check(stats(), check.Matches, `.*"OpenOps":1,.*`) c.Check(stats(), check.Matches, `.*"UtimesOps":2,.*`) - buf := bytes.NewBuffer(nil) - _, err = vol.BlockRead(context.Background(), fooHash, buf) + buf := &brbuffer{} + err = vol.BlockRead(context.Background(), fooHash, buf) c.Check(err, check.IsNil) c.Check(buf.String(), check.Equals, "foo") c.Check(stats(), check.Matches, `.*"InBytes":3,.*`) @@ -353,14 +258,14 @@ func (s *unixVolumeSuite) TestStats(c *check.C) { func (s *unixVolumeSuite) TestSkipUnusedDirs(c *check.C) { vol := s.newTestableUnixVolume(c, s.params, false) - err := os.Mkdir(vol.UnixVolume.Root+"/aaa", 0777) + err := os.Mkdir(vol.unixVolume.Root+"/aaa", 0777) c.Assert(err, check.IsNil) - err = os.Mkdir(vol.UnixVolume.Root+"/.aaa", 0777) // EmptyTrash should not look here + err = os.Mkdir(vol.unixVolume.Root+"/.aaa", 0777) // EmptyTrash should not look here c.Assert(err, check.IsNil) - deleteme := vol.UnixVolume.Root + "/aaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.trash.1" + deleteme := vol.unixVolume.Root + "/aaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.trash.1" err = ioutil.WriteFile(deleteme, []byte{1, 2, 3}, 0777) c.Assert(err, check.IsNil) - skipme := vol.UnixVolume.Root + "/.aaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.trash.1" + skipme := vol.unixVolume.Root + "/.aaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.trash.1" err = ioutil.WriteFile(skipme, []byte{1, 2, 3}, 0777) c.Assert(err, check.IsNil) vol.EmptyTrash()