From e9b2cab0fa6fd0536cf472967cd82f418218c81c Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Sun, 23 Mar 2014 14:40:08 -0700 Subject: Re-add tests for write failures Commit d2173f5f0ecbf4ed93c768e975435b04df3186ec removed the complete os & syscall mocking layer as overly complex. This commit adds back the simplest possible thing: hooks to control the database file writes. Missing tests: TestDBOpenMetaFileError, TestDBMmapStatError. These are harder to test without more extensive mocking. Conflicts: db_test.go --- db.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'db.go') diff --git a/db.go b/db.go index 51c24d3..5ec75b0 100644 --- a/db.go +++ b/db.go @@ -34,6 +34,11 @@ type DB struct { rwlock sync.Mutex // Allows only one writer at a time. metalock sync.Mutex // Protects meta page access. mmaplock sync.RWMutex // Protects mmap access during remapping. + + ops struct { + writeAt func(b []byte, off int64) (n int, err error) + metaWriteAt func(b []byte, off int64) (n int, err error) + } } // Path returns the path to currently open database file. @@ -74,6 +79,14 @@ func (db *DB) Open(path string, mode os.FileMode) error { return err } + // default values for test hooks + if db.ops.writeAt == nil { + db.ops.writeAt = db.file.WriteAt + } + if db.ops.metaWriteAt == nil { + db.ops.metaWriteAt = db.metafile.WriteAt + } + // Initialize the database if it doesn't exist. if info, err := db.file.Stat(); err != nil { return &Error{"stat error", err} @@ -226,7 +239,7 @@ func (db *DB) init() error { p.count = 0 // Write the buffer to our data file. - if _, err := db.metafile.WriteAt(buf, 0); err != nil { + if _, err := db.ops.metaWriteAt(buf, 0); err != nil { return err } -- cgit v1.2.3