Fix race cond

This commit is contained in:
Abhishek Pandey 2023-07-21 00:05:33 -07:00
parent b8a75434c9
commit d64b8844a5
2 changed files with 20 additions and 11 deletions

View File

@ -411,9 +411,6 @@ type driveStats struct {
itemsFound int64 itemsFound int64
} }
// global counter
var ctr int32
// populateItems iterates through items added to the collection // populateItems iterates through items added to the collection
// and uses the collection `itemReader` to read the item // and uses the collection `itemReader` to read the item
func (oc *Collection) populateItems(ctx context.Context, errs *fault.Bus) { func (oc *Collection) populateItems(ctx context.Context, errs *fault.Bus) {
@ -455,13 +452,13 @@ func (oc *Collection) populateItems(ctx context.Context, errs *fault.Bus) {
defer wg.Done() defer wg.Done()
defer func() { <-semaphoreCh }() defer func() { <-semaphoreCh }()
itf := []extensions.CreateItemExtensioner{ var itf []extensions.CreateItemExtensioner
&extensions.MockItemExtensionFactory{
ItemNumber: ctr,
},
}
atomic.AddInt32(&ctr, 1) if item.GetFile() != nil {
itf = []extensions.CreateItemExtensioner{
extensions.GetMockExtensions(),
}
}
// Read the item // Read the item
oc.populateDriveItem( oc.populateDriveItem(

View File

@ -33,8 +33,7 @@ type MockExtension struct {
} }
func (me *MockExtension) Read(p []byte) (int, error) { func (me *MockExtension) Read(p []byte) (int, error) {
// randomly fail with 50% prob if me.ItemNumber > 1 {
if me.ItemNumber > 3 {
return 0, clues.New("mock read error") return 0, clues.New("mock read error")
} }
@ -103,6 +102,8 @@ func (m *MockItemExtensionFactory) CreateItemExtension(
return nil, clues.New("factory error") return nil, clues.New("factory error")
} }
logger.Ctx(ctx).Infow("mock extension created", "itemnumber", m.ItemNumber)
return &MockExtension{ return &MockExtension{
Ctx: ctx, Ctx: ctx,
InnerRc: rc, InnerRc: rc,
@ -113,3 +114,14 @@ func (m *MockItemExtensionFactory) CreateItemExtension(
ItemNumber: m.ItemNumber, ItemNumber: m.ItemNumber,
}, nil }, nil
} }
var ctr int32
func GetMockExtensions() CreateItemExtensioner {
atomic.AddInt32(&ctr, 1)
mf := &MockItemExtensionFactory{
ItemNumber: ctr,
}
return mf
}