Skip to content

Commit

Permalink
verify that Close() calls idempotent
Browse files Browse the repository at this point in the history
It was already the case but never checked.
  • Loading branch information
paskal committed Dec 26, 2022
1 parent 899a670 commit 00dc8fd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
9 changes: 9 additions & 0 deletions avatar/bolt_test.go
Expand Up @@ -82,6 +82,15 @@ func TestBoltDB_List(t *testing.T) {
assert.Equal(t, "some picture bin data 3", string(data))
}

func TestBoltDB_DoubleClose(t *testing.T) {
_ = os.Remove(testDB)
boltStore, err := NewBoltDB(testDB, bolt.Options{})
require.Nil(t, err)
assert.NoError(t, boltStore.Close())
assert.NoError(t, boltStore.Close(), "second call should not result in panic or errors")
_ = os.Remove(testDB)
}

// makes new boltdb, put two records
func prepBoltStore(t *testing.T) (blt *BoltDB, teardown func()) {
_ = os.Remove(testDB)
Expand Down
2 changes: 1 addition & 1 deletion avatar/gridfs.go
Expand Up @@ -142,7 +142,7 @@ func (gf *GridFS) List() (ids []string, err error) {
return ids, nil
}

// Close gridfs does nothing but satisfies interface
// Close gridfs store
func (gf *GridFS) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), gf.timeout)
defer cancel()
Expand Down
13 changes: 12 additions & 1 deletion avatar/gridfs_test.go
Expand Up @@ -20,6 +20,7 @@ func TestGridFS_PutAndGet(t *testing.T) {
t.Skip("ENABLE_MONGO_TESTS env variable is not set")
}
p := prepGFStore(t)
defer p.Close()
avatar, err := p.Put("user1", strings.NewReader("some picture bin data"))
require.Nil(t, err)
assert.Equal(t, "b3daa77b4c04a9551b8781d03191fe098f325e67.image", avatar)
Expand Down Expand Up @@ -47,6 +48,7 @@ func TestGridFS_Remove(t *testing.T) {
t.Skip("ENABLE_MONGO_TESTS env variable is not set")
}
p := prepGFStore(t)
defer p.Close()
assert.Error(t, p.Remove("no-such-thing.image"))
avatar, err := p.Put("user1", strings.NewReader("some picture bin data"))
require.Nil(t, err)
Expand All @@ -60,6 +62,7 @@ func TestGridFS_List(t *testing.T) {
t.Skip("ENABLE_MONGO_TESTS env variable is not set")
}
p := prepGFStore(t)
defer p.Close()

// write some avatars
_, err := p.Put("user1", strings.NewReader("some picture bin data 1"))
Expand All @@ -83,8 +86,16 @@ func TestGridFS_List(t *testing.T) {
assert.Equal(t, "some picture bin data 3", string(data))
}

func prepGFStore(t *testing.T) *GridFS {
func TestGridFS_DoubleClose(t *testing.T) {
if _, ok := os.LookupEnv("ENABLE_MONGO_TESTS"); !ok {
t.Skip("ENABLE_MONGO_TESTS env variable is not set")
}
p := prepGFStore(t)
assert.NoError(t, p.Close())
assert.NoError(t, p.Close(), "second call should not result in panic or errors")
}

func prepGFStore(t *testing.T) *GridFS {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion avatar/localfs.go
Expand Up @@ -104,7 +104,7 @@ func (fs *LocalFS) List() (ids []string, err error) {
return ids, nil
}

// Close gridfs does nothing but satisfies interface
// Close LocalFS does nothing but satisfies interface
func (fs *LocalFS) Close() error {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions avatar/noop_test.go
Expand Up @@ -11,8 +11,8 @@ import (

func TestNoOp_Close(t *testing.T) {
p := NewNoOp()
err := p.Close()
require.NoError(t, err)
require.NoError(t, p.Close())
require.NoError(t, p.Close(), "second call should not result in panic or errors")
}

func TestNoOp_Get(t *testing.T) {
Expand Down

0 comments on commit 00dc8fd

Please sign in to comment.