From 35e2c97f43c8cb7ecaa2e1eb6e5dddd6657e300f Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 23 Aug 2017 22:25:05 -0500 Subject: [PATCH] fix lint error in storage folder. (#276) Signed-off-by: Bo-Yi Wu --- storage/boltdb/boltdb.go | 28 ++++++++++++++++++++++++---- storage/boltdb/boltdb_test.go | 3 ++- storage/buntdb/buntdb.go | 33 ++++++++++++++++++++++++++++----- storage/buntdb/buntdb_test.go | 6 ++++-- storage/leveldb/leveldb.go | 15 +++++++++++++-- storage/leveldb/leveldb_test.go | 6 ++++-- storage/redis/redis_test.go | 3 ++- 7 files changed, 77 insertions(+), 17 deletions(-) diff --git a/storage/boltdb/boltdb.go b/storage/boltdb/boltdb.go index 11b56e7..0a051e1 100644 --- a/storage/boltdb/boltdb.go +++ b/storage/boltdb/boltdb.go @@ -1,6 +1,8 @@ package boltdb import ( + "log" + "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/storage" @@ -35,14 +37,32 @@ func (s *Storage) Reset() { func (s *Storage) setBoltDB(key string, count int64) { db, _ := storm.Open(s.config.Stat.BoltDB.Path) - db.Set(s.config.Stat.BoltDB.Bucket, key, count) - defer db.Close() + err := db.Set(s.config.Stat.BoltDB.Bucket, key, count) + if err != nil { + log.Println("BoltDB set error:", err.Error()) + } + + defer func() { + err := db.Close() + if err != nil { + log.Println("BoltDB error:", err.Error()) + } + }() } func (s *Storage) getBoltDB(key string, count *int64) { db, _ := storm.Open(s.config.Stat.BoltDB.Path) - db.Get(s.config.Stat.BoltDB.Bucket, key, count) - defer db.Close() + err := db.Get(s.config.Stat.BoltDB.Bucket, key, count) + if err != nil { + log.Println("BoltDB get error:", err.Error()) + } + + defer func() { + err := db.Close() + if err != nil { + log.Println("BoltDB error:", err.Error()) + } + }() } // AddTotalCount record push notification count. diff --git a/storage/boltdb/boltdb_test.go b/storage/boltdb/boltdb_test.go index 743504e..e80b6d3 100644 --- a/storage/boltdb/boltdb_test.go +++ b/storage/boltdb/boltdb_test.go @@ -13,7 +13,8 @@ func TestBoltDBEngine(t *testing.T) { config := c.BuildDefaultPushConf() boltDB := New(config) - boltDB.Init() + err := boltDB.Init() + assert.Nil(t, err) boltDB.Reset() boltDB.AddTotalCount(10) diff --git a/storage/buntdb/buntdb.go b/storage/buntdb/buntdb.go index 3be82f5..1d6eb88 100644 --- a/storage/buntdb/buntdb.go +++ b/storage/buntdb/buntdb.go @@ -2,6 +2,7 @@ package buntdb import ( "fmt" + "log" "strconv" "github.com/appleboy/gorush/config" @@ -39,22 +40,44 @@ func (s *Storage) Reset() { func (s *Storage) setBuntDB(key string, count int64) { db, _ := buntdb.Open(s.config.Stat.BuntDB.Path) - db.Update(func(tx *buntdb.Tx) error { - tx.Set(key, fmt.Sprintf("%d", count), nil) + err := db.Update(func(tx *buntdb.Tx) error { + if _, _, err := tx.Set(key, fmt.Sprintf("%d", count), nil); err != nil { + return err + } return nil }) - defer db.Close() + + if err != nil { + log.Println("BuntDB update error:", err.Error()) + } + + defer func() { + err := db.Close() + if err != nil { + log.Println("BuntDB error:", err.Error()) + } + }() } func (s *Storage) getBuntDB(key string, count *int64) { db, _ := buntdb.Open(s.config.Stat.BuntDB.Path) - db.View(func(tx *buntdb.Tx) error { + err := db.View(func(tx *buntdb.Tx) error { val, _ := tx.Get(key) *count, _ = strconv.ParseInt(val, 10, 64) return nil }) - defer db.Close() + + if err != nil { + log.Println("BuntDB get error:", err.Error()) + } + + defer func() { + err := db.Close() + if err != nil { + log.Println("BuntDB error:", err.Error()) + } + }() } // AddTotalCount record push notification count. diff --git a/storage/buntdb/buntdb_test.go b/storage/buntdb/buntdb_test.go index 0da440d..10ab4de 100644 --- a/storage/buntdb/buntdb_test.go +++ b/storage/buntdb/buntdb_test.go @@ -14,11 +14,13 @@ func TestBuntDBEngine(t *testing.T) { config := c.BuildDefaultPushConf() if _, err := os.Stat(config.Stat.BuntDB.Path); os.IsNotExist(err) { - os.RemoveAll(config.Stat.BuntDB.Path) + err := os.RemoveAll(config.Stat.BuntDB.Path) + assert.Nil(t, err) } buntDB := New(config) - buntDB.Init() + err := buntDB.Init() + assert.Nil(t, err) buntDB.Reset() buntDB.AddTotalCount(10) diff --git a/storage/leveldb/leveldb.go b/storage/leveldb/leveldb.go index ebf6004..35f9a0c 100644 --- a/storage/leveldb/leveldb.go +++ b/storage/leveldb/leveldb.go @@ -2,6 +2,7 @@ package leveldb import ( "fmt" + "log" "strconv" "github.com/appleboy/gorush/config" @@ -18,7 +19,12 @@ func setLevelDB(key string, count int64) { _ = db.Put([]byte(key), []byte(value), nil) - defer db.Close() + defer func() { + err := db.Close() + if err != nil { + log.Println("LevelDB error:", err.Error()) + } + }() } func getLevelDB(key string, count *int64) { @@ -27,7 +33,12 @@ func getLevelDB(key string, count *int64) { data, _ := db.Get([]byte(key), nil) *count, _ = strconv.ParseInt(string(data), 10, 64) - defer db.Close() + defer func() { + err := db.Close() + if err != nil { + log.Println("LevelDB error:", err.Error()) + } + }() } // New func implements the storage interface for gorush (https://github.com/appleboy/gorush) diff --git a/storage/leveldb/leveldb_test.go b/storage/leveldb/leveldb_test.go index 6df0c04..a05a9b0 100644 --- a/storage/leveldb/leveldb_test.go +++ b/storage/leveldb/leveldb_test.go @@ -14,11 +14,13 @@ func TestLevelDBEngine(t *testing.T) { config := c.BuildDefaultPushConf() if _, err := os.Stat(config.Stat.LevelDB.Path); os.IsNotExist(err) { - os.RemoveAll(config.Stat.LevelDB.Path) + err = os.RemoveAll(config.Stat.LevelDB.Path) + assert.Nil(t, err) } levelDB := New(config) - levelDB.Init() + err := levelDB.Init() + assert.Nil(t, err) levelDB.Reset() levelDB.AddTotalCount(10) diff --git a/storage/redis/redis_test.go b/storage/redis/redis_test.go index d8b6e5d..2cad047 100644 --- a/storage/redis/redis_test.go +++ b/storage/redis/redis_test.go @@ -24,7 +24,8 @@ func TestRedisEngine(t *testing.T) { config.Stat.Redis.Addr = "redis:6379" redis := New(config) - redis.Init() + err := redis.Init() + assert.Nil(t, err) redis.Reset() redis.AddTotalCount(10)