2
0

Use more detailed stat names

This commit is contained in:
Jack Christensen
2018-12-26 14:10:50 -06:00
parent d83b67b4a8
commit aa05ab2ade
2 changed files with 43 additions and 41 deletions
+28 -26
View File
@@ -60,11 +60,13 @@ type Pool struct {
allResources []*Resource allResources []*Resource
idleResources []*Resource idleResources []*Resource
maxSize int
closed bool
constructor Constructor constructor Constructor
destructor Destructor destructor Destructor
maxSize int
acquireCount uint64
closed bool
} }
func NewPool(constructor Constructor, destructor Destructor, maxSize int) *Pool { func NewPool(constructor Constructor, destructor Destructor, maxSize int) *Pool {
@@ -97,53 +99,53 @@ func (p *Pool) Close() {
} }
type Stat struct { type Stat struct {
constructing int constructingResources int
acquired int acquiredResources int
idle int idleResources int
maxSize int maxResources int
} }
// Size returns the total number of resources in the pool. // TotalResource returns the total number of resources in the pool.
func (s *Stat) Size() int { func (s *Stat) TotalResources() int {
return s.constructing + s.acquired + s.idle return s.constructingResources + s.acquiredResources + s.idleResources
} }
// Constructing returns the number of resources with construction in progress in // ConstructingResources returns the number of resources with construction in progress in
// the pool. // the pool.
func (s *Stat) Constructing() int { func (s *Stat) ConstructingResources() int {
return s.constructing return s.constructingResources
} }
// Acquired returns the number of acquired resources in the pool. // AcquiredResources returns the number of acquired resources in the pool.
func (s *Stat) Acquired() int { func (s *Stat) AcquiredResources() int {
return s.acquired return s.acquiredResources
} }
// Idle returns the number of idle resources in the pool. // IdleResources returns the number of idle resources in the pool.
func (s *Stat) Idle() int { func (s *Stat) IdleResources() int {
return s.idle return s.idleResources
} }
// MaxSize returns the maximum size of the pool // MaxResources returns the maximum size of the pool
func (s *Stat) MaxSize() int { func (s *Stat) MaxResources() int {
return s.maxSize return s.maxResources
} }
// Stat returns the current pool statistics. // Stat returns the current pool statistics.
func (p *Pool) Stat() *Stat { func (p *Pool) Stat() *Stat {
p.cond.L.Lock() p.cond.L.Lock()
s := &Stat{ s := &Stat{
maxSize: p.maxSize, maxResources: p.maxSize,
} }
for _, res := range p.allResources { for _, res := range p.allResources {
switch res.status { switch res.status {
case resourceStatusConstructing: case resourceStatusConstructing:
s.constructing += 1 s.constructingResources += 1
case resourceStatusIdle: case resourceStatusIdle:
s.idle += 1 s.idleResources += 1
case resourceStatusAcquired: case resourceStatusAcquired:
s.acquired += 1 s.acquiredResources += 1
} }
} }
+15 -15
View File
@@ -113,7 +113,7 @@ func TestPoolAcquireDoesNotCreatesResourceWhenItWouldExceedMaxSize(t *testing.T)
wg.Wait() wg.Wait()
assert.Equal(t, 1, createCounter.Value()) assert.Equal(t, 1, createCounter.Value())
assert.Equal(t, 1, pool.Stat().Size()) assert.Equal(t, 1, pool.Stat().TotalResources())
} }
func TestPoolAcquireWithCancellableContext(t *testing.T) { func TestPoolAcquireWithCancellableContext(t *testing.T) {
@@ -140,7 +140,7 @@ func TestPoolAcquireWithCancellableContext(t *testing.T) {
wg.Wait() wg.Wait()
assert.Equal(t, 1, createCounter.Value()) assert.Equal(t, 1, createCounter.Value())
assert.Equal(t, 1, pool.Stat().Size()) assert.Equal(t, 1, pool.Stat().TotalResources())
} }
func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) { func TestPoolAcquireReturnsErrorFromFailedResourceCreate(t *testing.T) {
@@ -292,20 +292,20 @@ func TestPoolStat(t *testing.T) {
<-waitingChan <-waitingChan
stat := pool.Stat() stat := pool.Stat()
assert.Equal(t, 2, stat.Size()) assert.Equal(t, 2, stat.TotalResources())
assert.Equal(t, 1, stat.Constructing()) assert.Equal(t, 1, stat.ConstructingResources())
assert.Equal(t, 1, stat.Acquired()) assert.Equal(t, 1, stat.AcquiredResources())
assert.Equal(t, 0, stat.Idle()) assert.Equal(t, 0, stat.IdleResources())
assert.Equal(t, 10, stat.MaxSize()) assert.Equal(t, 10, stat.MaxResources())
resAcquired.Release() resAcquired.Release()
stat = pool.Stat() stat = pool.Stat()
assert.Equal(t, 2, stat.Size()) assert.Equal(t, 2, stat.TotalResources())
assert.Equal(t, 1, stat.Constructing()) assert.Equal(t, 1, stat.ConstructingResources())
assert.Equal(t, 0, stat.Acquired()) assert.Equal(t, 0, stat.AcquiredResources())
assert.Equal(t, 1, stat.Idle()) assert.Equal(t, 1, stat.IdleResources())
assert.Equal(t, 10, stat.MaxSize()) assert.Equal(t, 10, stat.MaxResources())
close(endWaitChan) close(endWaitChan)
} }
@@ -325,7 +325,7 @@ func TestResourceDestroyRemovesResourceFromPool(t *testing.T) {
res.Hijack() res.Hijack()
assert.Equal(t, 0, pool.Stat().Size()) assert.Equal(t, 0, pool.Stat().TotalResources())
assert.Equal(t, 0, closeCalls.Value()) assert.Equal(t, 0, closeCalls.Value())
} }
@@ -337,9 +337,9 @@ func TestResourceHijackRemovesResourceFromPoolButDoesNotDestroy(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, 1, res.Value()) assert.Equal(t, 1, res.Value())
assert.Equal(t, 1, pool.Stat().Size()) assert.Equal(t, 1, pool.Stat().TotalResources())
res.Destroy() res.Destroy()
assert.Equal(t, 0, pool.Stat().Size()) assert.Equal(t, 0, pool.Stat().TotalResources())
} }
func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) { func TestPoolAcquireReturnsErrorWhenPoolIsClosed(t *testing.T) {