From 5ea05fb3c29c49084a581452cdcbf1e3c1023424 Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Wed, 25 Sep 2019 22:19:17 +1000 Subject: [PATCH] Add unit test for opening bad database with corrupted/invalid datafiles (#105) --- bitcask_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bitcask_test.go b/bitcask_test.go index aa9280a..167d0b5 100644 --- a/bitcask_test.go +++ b/bitcask_test.go @@ -796,6 +796,29 @@ func TestOpenErrors(t *testing.T) { _, err = Open(testdir, withBogusOption()) assert.Error(err) }) + + t.Run("LoadDatafilesError", func(t *testing.T) { + testdir, err := ioutil.TempDir("", "bitcask") + assert.NoError(err) + defer os.RemoveAll(testdir) + + db, err := Open(testdir) + assert.NoError(err) + + err = db.Put([]byte("foo"), []byte("bar")) + assert.NoError(err) + + err = db.Close() + assert.NoError(err) + + // Simulate some horrible that happened to the datafiles! + err = os.Rename(filepath.Join(testdir, "000000000.data"), filepath.Join(testdir, "000000000xxx.data")) + assert.NoError(err) + + _, err = Open(testdir) + assert.Error(err) + assert.Equal("strconv.ParseInt: parsing \"000000000xxx\": invalid syntax", err.Error()) + }) } func TestCloseErrors(t *testing.T) {