Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
storage/localstore: create failing test case
Browse files Browse the repository at this point in the history
  • Loading branch information
acud committed Sep 30, 2019
1 parent c080aca commit 57fe1a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
4 changes: 3 additions & 1 deletion chunk/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/ethersphere/swarm/sctx"
)

var TagUidFunc = rand.Uint32

// Tags hold tag information indexed by a unique random uint32
type Tags struct {
tags *sync.Map
Expand All @@ -41,7 +43,7 @@ func NewTags() *Tags {
// Create creates a new tag, stores it by the name and returns it
// it returns an error if the tag with this name already exists
func (ts *Tags) Create(s string, total int64, anon bool) (*Tag, error) {
t := NewTag(rand.Uint32(), s, total, anon)
t := NewTag(TagUidFunc(), s, total, anon)

if _, loaded := ts.tags.LoadOrStore(t.Uid, t); loaded {
return nil, errExists
Expand Down
84 changes: 57 additions & 27 deletions storage/localstore/mode_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,72 @@ func TestModeSetAccess(t *testing.T) {

// TestModeSetSyncPull validates ModeSetSyncPull index values on the provided DB.
func TestModeSetSyncPull(t *testing.T) {
for _, tc := range multiChunkTestCases {
t.Run(tc.name, func(t *testing.T) {
db, cleanupFunc := newTestDB(t, nil)
defer cleanupFunc()
defer func(f func() uint32) {
chunk.TagUidFunc = f
}(chunk.TagUidFunc)

chunk.TagUidFunc = func() uint32 { return 0 }

for _, mtc := range []struct {
name string
anonymous bool
}{
{
name: "normal tag",
anonymous: false,
},
{
name: "anonymous tag",
anonymous: true,
},
} {
t.Run(mtc.name, func(t *testing.T) {
for _, tc := range multiChunkTestCases {
t.Run(tc.name, func(t *testing.T) {
db, cleanupFunc := newTestDB(t, &Options{Tags: chunk.NewTags()})
defer cleanupFunc()

tag, err := db.tags.Create(mtc.name, int64(tc.count), mtc.anonymous)
if err != nil {
t.Fatal(err)
}
if tag.Uid != 0 {
t.Fatal("expected mock tag uid")
}

chunks := generateTestRandomChunks(tc.count)
chunks := generateTestRandomChunks(tc.count)

wantTimestamp := time.Now().UTC().UnixNano()
defer setNow(func() (t int64) {
return wantTimestamp
})()
wantTimestamp := time.Now().UTC().UnixNano()
defer setNow(func() (t int64) {
return wantTimestamp
})()

_, err := db.Put(context.Background(), chunk.ModePutUpload, chunks...)
if err != nil {
t.Fatal(err)
}
_, err = db.Put(context.Background(), chunk.ModePutUpload, chunks...)
if err != nil {
t.Fatal(err)
}

err = db.Set(context.Background(), chunk.ModeSetSyncPull, chunkAddresses(chunks)...)
if err != nil {
t.Fatal(err)
}
err = db.Set(context.Background(), chunk.ModeSetSyncPull, chunkAddresses(chunks)...)
if err != nil {
t.Fatal(err)
}

binIDs := make(map[uint8]uint64)
binIDs := make(map[uint8]uint64)

for _, ch := range chunks {
po := db.po(ch.Address())
binIDs[po]++
for _, ch := range chunks {
po := db.po(ch.Address())
binIDs[po]++

newRetrieveIndexesTestWithAccess(db, ch, wantTimestamp, wantTimestamp)(t)
newPushIndexTest(db, ch, wantTimestamp, leveldb.ErrNotFound)(t)
newGCIndexTest(db, ch, wantTimestamp, wantTimestamp, binIDs[po])(t)
}
newRetrieveIndexesTestWithAccess(db, ch, wantTimestamp, wantTimestamp)(t)
newPushIndexTest(db, ch, wantTimestamp, leveldb.ErrNotFound)(t)
newGCIndexTest(db, ch, wantTimestamp, wantTimestamp, binIDs[po])(t)
}

t.Run("gc index count", newItemsCountTest(db.gcIndex, tc.count))
t.Run("gc index count", newItemsCountTest(db.gcIndex, tc.count))

t.Run("gc size", newIndexGCSizeTest(db))
t.Run("gc size", newIndexGCSizeTest(db))
})
}
})
}
}
Expand Down

0 comments on commit 57fe1a2

Please sign in to comment.