MCPcopy
hub / github.com/dropbox/godropbox / testLockKeyParallel

Method testLockKeyParallel

lockstore/store_test.go:69–116  ·  view source on GitHub ↗
(c *C, tryLock bool)

Source from the content-addressed store, hash-verified

67}
68
69func (s *LockStoreSuite) testLockKeyParallel(c *C, tryLock bool) {
70 key := "test-key-parallel"
71 callbackWg := sync.WaitGroup{}
72 tryLockCallback := func() {
73 callbackWg.Done()
74 }
75 storeRaw := New(LockStoreOptions{
76 Granularity: PerKeyGranularity,
77 testTryLockCallback: tryLockCallback,
78 })
79 store := storeRaw.(*_LockStoreImp)
80 period := time.Second
81 wg := sync.WaitGroup{}
82 // rnd is not thread safe
83 rnd := rand.New(rand.NewSource(time.Now().Unix()))
84 lock := sync.Mutex{}
85 // this test will take approximately 10 sec
86 for i := 0; i < 10; i++ {
87 wg.Add(1)
88 go func() {
89 defer wg.Done()
90 // for rnd
91 lock.Lock()
92 jitter := period/2 + time.Duration(rnd.Int63n(int64(period)))
93 lock.Unlock()
94 locked := true
95 if tryLock {
96 callbackWg.Add(1)
97 locked = store.TryLock(key, jitter)
98 } else {
99 store.Lock(key)
100 }
101 time.Sleep(jitter)
102 if locked {
103 store.Unlock(key)
104 }
105 }()
106 }
107
108 wg.Wait()
109
110 if tryLock {
111 // we need to wait for all the go routines to exit
112 callbackWg.Wait()
113 }
114
115 c.Assert(len(store.perKeyLocks), Equals, 0)
116}
117
118func (s *LockStoreSuite) TestLockKeyMultiple(c *C) {
119 storeRaw := New(LockStoreOptions{Granularity: PerKeyGranularity})

Callers 2

TestLockKeyParallelMethod · 0.95

Calls 10

DurationMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
NowMethod · 0.65
AddMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65
TryLockMethod · 0.65
SleepMethod · 0.65
DoneMethod · 0.45

Tested by

no test coverage detected