Skip to content

Commit

Permalink
Merge pull request #65 from rgooch/master
Browse files Browse the repository at this point in the history
lib/filegen: fix concurrency bug and add unittest.
  • Loading branch information
rgooch committed Mar 22, 2020
2 parents 0577356 + 49ef16a commit 835d167
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/filegen/register.go
Expand Up @@ -37,14 +37,18 @@ func (m *Manager) registerHashGeneratorForPath(pathname string,
pathMgr := &pathManager{
generator: gen,
machineHashes: make(map[string]expiringHash)}
m.rwMutex.Lock()
m.pathManagers[pathname] = pathMgr
m.rwMutex.Unlock()
go m.processPathDataInvalidations(pathname, notifyChan)
return notifyChan
}

func (m *Manager) processPathDataInvalidations(pathname string,
machineNameChannel <-chan string) {
m.rwMutex.RLock()
pathMgr := m.pathManagers[pathname]
m.rwMutex.RUnlock()
for machineName := range machineNameChannel {
pathMgr.rwMutex.Lock()
if machineName == "" {
Expand Down
32 changes: 32 additions & 0 deletions lib/filegen/register_test.go
@@ -0,0 +1,32 @@
package filegen

import (
"fmt"
"testing"
"time"

"github.com/Cloud-Foundations/Dominator/lib/log"
"github.com/Cloud-Foundations/Dominator/lib/log/testlogger"
"github.com/Cloud-Foundations/Dominator/lib/mdb"
)

type testGenerator struct{}

var testData = []byte("data")

func (g *testGenerator) Generate(machine mdb.Machine, logger log.Logger) (
data []byte, validUntil time.Time, err error) {
return testData, time.Now().Add(time.Minute), nil
}

func TestManyRegisters(t *testing.T) {
m := New(testlogger.New(t))
dataGenerator := &testGenerator{}
var pathnames []string
for count := 0; count < 100; count++ {
pathnames = append(pathnames, fmt.Sprintf("dir/file%d", count))
}
for _, pathname := range pathnames {
m.RegisterGeneratorForPath(pathname, dataGenerator)
}
}

0 comments on commit 835d167

Please sign in to comment.