-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5141 from multiversx/add-unit-tests-for-storage
Added unit tests for `storage` package
- Loading branch information
Showing
31 changed files
with
1,940 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/multiversx/mx-chain-go/testscommon" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewTimeCache_ShouldWork(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance := NewTimeCache(0) | ||
assert.NotNil(t, instance) | ||
} | ||
|
||
func TestNewTimeCacher(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
args := ArgTimeCacher{ | ||
DefaultSpan: time.Second - time.Nanosecond, | ||
CacheExpiry: time.Second, | ||
} | ||
|
||
instance, err := NewTimeCacher(args) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
args := ArgTimeCacher{ | ||
DefaultSpan: time.Second, | ||
CacheExpiry: time.Second, | ||
} | ||
|
||
instance, err := NewTimeCacher(args) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestNewLRUCache(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLRUCache(0) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLRUCache(1) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestNewPeerTimeCache(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewPeerTimeCache(nil) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewPeerTimeCache(&testscommon.TimeCacheStub{}) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestNewCapacityLRU(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewCapacityLRU(0, 1) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewCapacityLRU(1, 1) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestNewLRUCacheWithEviction(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLRUCacheWithEviction(0, nil) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("nil handler should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLRUCacheWithEviction(1, nil) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
t.Run("with handler should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLRUCacheWithEviction(1, func(key interface{}, value interface{}) {}) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
}) | ||
} | ||
|
||
func TestNewImmunityCache(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
config := CacheConfig{ | ||
MaxNumBytes: 0, | ||
MaxNumItems: 0, | ||
NumChunks: 0, | ||
Name: "test", | ||
NumItemsToPreemptivelyEvict: 0, | ||
} | ||
instance, err := NewImmunityCache(config) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
config := CacheConfig{ | ||
MaxNumBytes: 4, | ||
MaxNumItems: 4, | ||
NumChunks: 1, | ||
Name: "test", | ||
NumItemsToPreemptivelyEvict: 1, | ||
} | ||
instance, err := NewImmunityCache(config) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package database | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewMemDB(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance := NewMemDB() | ||
assert.NotNil(t, instance) | ||
} | ||
|
||
func TestNewlruDB(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewlruDB(0) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewlruDB(1) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestNewLevelDB(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLevelDB(t.TempDir(), 0, 0, 0) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewLevelDB(t.TempDir(), 1, 1, 1) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
_ = instance.Close() | ||
}) | ||
} | ||
|
||
func TestNewSerialDB(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid argument should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewSerialDB(t.TempDir(), 0, 0, 0) | ||
assert.Nil(t, instance) | ||
assert.NotNil(t, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
instance, err := NewSerialDB(t.TempDir(), 1, 1, 1) | ||
assert.NotNil(t, instance) | ||
assert.Nil(t, err) | ||
_ = instance.Close() | ||
}) | ||
} |
Oops, something went wrong.