From 05ce0c01e32633fe296a30a6a66cc3be56cd2a5a Mon Sep 17 00:00:00 2001 From: Nan Deng Date: Wed, 18 Jun 2014 13:43:08 -0700 Subject: [PATCH] fix a deadlock bug along with unit tests --- storage/memory/memory.go | 3 +++ storage/memory/memory_test.go | 12 +++++++++++ storage/test/storagetests.go | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/storage/memory/memory.go b/storage/memory/memory.go index 4af35c07..f86dacb1 100644 --- a/storage/memory/memory.go +++ b/storage/memory/memory.go @@ -183,6 +183,7 @@ func (self *InMemoryStorage) Samples(name string, numSamples int) ([]*info.Conta var ok bool self.lock.RLock() if cstore, ok = self.containerStorageMap[name]; !ok { + self.lock.RUnlock() return nil, fmt.Errorf("unable to find data for container %v", name) } self.lock.RUnlock() @@ -195,6 +196,7 @@ func (self *InMemoryStorage) RecentStats(name string, numStats int) ([]*info.Con var ok bool self.lock.RLock() if cstore, ok = self.containerStorageMap[name]; !ok { + self.lock.RUnlock() return nil, fmt.Errorf("unable to find data for container %v", name) } self.lock.RUnlock() @@ -207,6 +209,7 @@ func (self *InMemoryStorage) Percentiles(name string, cpuPercentiles, memPercent var ok bool self.lock.RLock() if cstore, ok = self.containerStorageMap[name]; !ok { + self.lock.RUnlock() return nil, fmt.Errorf("unable to find data for container %v", name) } self.lock.RUnlock() diff --git a/storage/memory/memory_test.go b/storage/memory/memory_test.go index 6ecca408..394e48f6 100644 --- a/storage/memory/memory_test.go +++ b/storage/memory/memory_test.go @@ -61,3 +61,15 @@ func TestRetrievePartialRecentStats(t *testing.T) { func TestRetrieveAllRecentStats(t *testing.T) { runStorageTest(test.StorageDriverTestRetrieveAllRecentStats, t) } + +func TestNoRecentStats(t *testing.T) { + runStorageTest(test.StorageDriverTestNoRecentStats, t) +} + +func TestNoSamples(t *testing.T) { + runStorageTest(test.StorageDriverTestNoSamples, t) +} + +func TestPercentilesWithoutStats(t *testing.T) { + runStorageTest(test.StorageDriverTestPercentilesWithoutStats, t) +} diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index 1bef8610..311c4479 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -319,3 +319,43 @@ func StorageDriverTestRetrieveAllRecentStats(driver storage.StorageDriver, t *te } } } + +func StorageDriverTestNoRecentStats(driver storage.StorageDriver, t *testing.T) { + defer driver.Close() + nonExistContainer := "somerandomecontainer" + stats, _ := driver.RecentStats(nonExistContainer, -1) + if len(stats) > 0 { + t.Errorf("RecentStats() returns %v stats on non exist container", len(stats)) + } +} + +func StorageDriverTestNoSamples(driver storage.StorageDriver, t *testing.T) { + defer driver.Close() + nonExistContainer := "somerandomecontainer" + samples, _ := driver.Samples(nonExistContainer, -1) + if len(samples) > 0 { + t.Errorf("Samples() returns %v samples on non exist container", len(samples)) + } +} + +func StorageDriverTestPercentilesWithoutStats(driver storage.StorageDriver, t *testing.T) { + defer driver.Close() + nonExistContainer := "somerandomecontainer" + percentiles, _ := driver.Percentiles(nonExistContainer, []int{50, 80}, []int{50, 80}) + if percentiles == nil { + return + } + if percentiles.MaxMemoryUsage != 0 { + t.Errorf("Percentiles() reports max memory usage > 0 when there's no stats.") + } + for _, p := range percentiles.CpuUsagePercentiles { + if p.Value != 0 { + t.Errorf("Percentiles() reports cpu usage is %v when there's no stats.", p.Value) + } + } + for _, p := range percentiles.MemoryUsagePercentiles { + if p.Value != 0 { + t.Errorf("Percentiles() reports memory usage is %v when there's no stats.", p.Value) + } + } +}