Fix status table race condition (#1835)
This commit is contained in:
parent
0f5b399e35
commit
bfb44f8854
5 changed files with 26 additions and 10 deletions
|
@ -24,6 +24,18 @@ func NewStatusTable() *StatusTable {
|
|||
}
|
||||
}
|
||||
|
||||
// StartIfNotRunning sets value of given name to true if not already in pool.
|
||||
// Returns whether set value was set to true
|
||||
func (p *StatusTable) StartIfNotRunning(name string) bool {
|
||||
p.lock.Lock()
|
||||
_, ok := p.pool[name]
|
||||
if !ok {
|
||||
p.pool[name] = struct{}{}
|
||||
}
|
||||
p.lock.Unlock()
|
||||
return !ok
|
||||
}
|
||||
|
||||
// Start sets value of given name to true in the pool.
|
||||
func (p *StatusTable) Start(name string) {
|
||||
p.lock.Lock()
|
||||
|
|
|
@ -18,6 +18,15 @@ func Test_StatusTable(t *testing.T) {
|
|||
table.Start("xyz")
|
||||
assert.True(t, table.IsRunning("xyz"))
|
||||
|
||||
assert.False(t, table.StartIfNotRunning("xyz"))
|
||||
assert.True(t, table.IsRunning("xyz"))
|
||||
|
||||
table.Stop("xyz")
|
||||
assert.False(t, table.IsRunning("xyz"))
|
||||
|
||||
assert.True(t, table.StartIfNotRunning("xyz"))
|
||||
assert.True(t, table.IsRunning("xyz"))
|
||||
|
||||
table.Stop("xyz")
|
||||
assert.False(t, table.IsRunning("xyz"))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue