Skip to content

Commit

Permalink
Merge pull request #33 from dcormier/dc/examples
Browse files Browse the repository at this point in the history
Linter cleanup
  • Loading branch information
dcormier authored Jun 21, 2018
2 parents 50d10a0 + dcfedde commit 3f5d5f8
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Configuration option table, more detail description see [ObjectPoolConfig](https

| Option | Default | Description |
| ------------------------------|:--------------:| :------------|
| Lifo | true |If pool is LIFO (last in, first out)|
| LIFO | true |If pool is LIFO (last in, first out)|
| MaxTotal | 8 |The cap of pool|
| MaxIdle | 8 |Max "idle" instances in the pool |
| MinIdle | 0 |Min "idle" instances in the pool |
Expand Down
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
DefaultMaxIdle = 8
// DefaultMinIdle is the default value of ObjectPoolConfig.MinIdle
DefaultMinIdle = 0
// DefaultLifo is the default value of ObjectPoolConfig.Lifo
DefaultLifo = true
// DefaultLIFO is the default value of ObjectPoolConfig.LIFO
DefaultLIFO = true

// TODO
// DEFAULT_FAIRNESS = false
Expand Down Expand Up @@ -51,7 +51,7 @@ type ObjectPoolConfig struct {
* from the pool, or as a FIFO (first in, first out) queue, where the pool
* always returns the oldest object in the idle object pool.
*/
Lifo bool
LIFO bool

/**
* The cap on the number of objects that can be allocated by the pool
Expand Down Expand Up @@ -191,7 +191,7 @@ type ObjectPoolConfig struct {
// NewDefaultPoolConfig return a ObjectPoolConfig instance init with default value.
func NewDefaultPoolConfig() *ObjectPoolConfig {
return &ObjectPoolConfig{
Lifo: DefaultLifo,
LIFO: DefaultLIFO,
MaxTotal: DefaultMaxTotal,
MaxIdle: DefaultMaxIdle,
MinIdle: DefaultMinIdle,
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDefaultConfig(t *testing.T) {
config := NewDefaultPoolConfig()
assert.Equal(t, DefaultBlockWhenExhausted, config.BlockWhenExhausted)
assert.Equal(t, DefaultEvictionPolicyName, config.EvictionPolicyName)
assert.Equal(t, DefaultLifo, config.Lifo)
assert.Equal(t, DefaultLIFO, config.LIFO)
assert.Equal(t, DefaultMaxIdle, config.MaxIdle)
assert.Equal(t, DefaultMaxTotal, config.MaxTotal)
assert.Equal(t, DefaultMinEvictableIdleTime, config.MinEvictableIdleTime)
Expand Down
3 changes: 1 addition & 2 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ func TestTrackedUse(t *testing.T) {

now := time.Now()
object := &TrackedUseObject{lastUsed: now}
var trackedUse TrackedUse
trackedUse = object
trackedUse := TrackedUse(object)
assert.Equal(t, now, trackedUse.GetLastUsed())

pooledObject := NewPooledObject(object)
Expand Down
8 changes: 4 additions & 4 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (pool *ObjectPool) addIdleObject(ctx context.Context, p *PooledObject) erro
return err
}

if pool.Config.Lifo {
if pool.Config.LIFO {
err = pool.idleObjects.AddFirst(p)
} else {
err = pool.idleObjects.AddLast(p)
Expand Down Expand Up @@ -348,7 +348,7 @@ func (pool *ObjectPool) ensureIdle(ctx context.Context, idleCount int, always bo
// create will work. Give up.
break
}
if pool.Config.Lifo {
if pool.Config.LIFO {
pool.idleObjects.AddFirst(p)
} else {
pool.idleObjects.AddLast(p)
Expand Down Expand Up @@ -427,7 +427,7 @@ func (pool *ObjectPool) ReturnObject(ctx context.Context, object interface{}) er
if pool.IsClosed() || maxIdleSave > -1 && maxIdleSave <= pool.idleObjects.Size() {
pool.destroy(ctx, p)
} else {
if pool.Config.Lifo {
if pool.Config.LIFO {
pool.idleObjects.AddFirst(p)
} else {
pool.idleObjects.AddLast(p)
Expand Down Expand Up @@ -559,7 +559,7 @@ func (pool *ObjectPool) getNumTests() int {

// idleIterator return pool idleObjects iterator
func (pool *ObjectPool) idleIterator() collections.Iterator {
if pool.Config.Lifo {
if pool.Config.LIFO {
return pool.idleObjects.DescendingIterator()
}
return pool.idleObjects.Iterator()
Expand Down
3 changes: 1 addition & 2 deletions pool_abandoned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func TestAbandonedTestObject(t *testing.T) {
t.Parallel()

obj := NewAbandonedTestObject()
var trackedUse TrackedUse
trackedUse = obj
trackedUse := TrackedUse(obj)
assert.Zero(t, trackedUse.GetLastUsed())
}

Expand Down
30 changes: 15 additions & 15 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ func (suit *PoolTestSuite) TestBaseAddObject() {
suit.assertEquals(0, suit.pool.GetNumActive())
}

func (suit *PoolTestSuite) isLifo() bool {
func (suit *PoolTestSuite) isLIFO() bool {
return true
}

func (suit *PoolTestSuite) isFifo() bool {
func (suit *PoolTestSuite) isFIFO() bool {
return false
}

Expand All @@ -324,18 +324,18 @@ func (suit *PoolTestSuite) TestBaseBorrowReturn() {
suit.pool.ReturnObject(ctx, obj0)
suit.pool.ReturnObject(ctx, obj2)
obj2 = suit.NoErrorWithResult(suit.pool.BorrowObject(ctx))
if suit.isLifo() {
if suit.isLIFO() {
suit.assertEquals(getNthObject(2), obj2)
}
if suit.isFifo() {
if suit.isFIFO() {
suit.assertEquals(getNthObject(0), obj2)
}

obj0 = suit.NoErrorWithResult(suit.pool.BorrowObject(ctx))
if suit.isLifo() {
if suit.isLIFO() {
suit.assertEquals(getNthObject(0), obj0)
}
if suit.isFifo() {
if suit.isFIFO() {
suit.assertEquals(getNthObject(2), obj0)
}
}
Expand Down Expand Up @@ -695,7 +695,7 @@ func (suit *PoolTestSuite) checkEvict(ctx context.Context, lifo bool) {
suit.pool.Config.SoftMinEvictableIdleTime = 10 * time.Millisecond
suit.pool.Config.MinIdle = 2
suit.pool.Config.TestWhileIdle = true
suit.pool.Config.Lifo = lifo
suit.pool.Config.LIFO = lifo
Prefill(ctx, suit.pool, 5)
suit.pool.evict(ctx)
idle = suit.pool.GetNumIdle()
Expand Down Expand Up @@ -748,7 +748,7 @@ func (suit *PoolTestSuite) checkEvictionOrder(ctx context.Context, lifo bool) {
func (suit *PoolTestSuite) checkEvictionOrderPart1(ctx context.Context, lifo bool) {
suit.pool.Config.NumTestsPerEvictionRun = 2
suit.pool.Config.MinEvictableIdleTime = 100 * time.Millisecond
suit.pool.Config.Lifo = lifo
suit.pool.Config.LIFO = lifo
for i := 0; i < 5; i++ {
suit.pool.AddObject(ctx)
time.Sleep(time.Duration(100) * time.Millisecond)
Expand All @@ -772,7 +772,7 @@ func (suit *PoolTestSuite) checkEvictionOrderPart2(ctx context.Context, lifo boo
// Two eviction runs in sequence
suit.pool.Config.NumTestsPerEvictionRun = 2
suit.pool.Config.MinEvictableIdleTime = 100 * time.Millisecond
suit.pool.Config.Lifo = lifo
suit.pool.Config.LIFO = lifo
for i := 0; i < 5; i++ {
suit.pool.AddObject(ctx)
time.Sleep(time.Duration(100) * time.Millisecond)
Expand Down Expand Up @@ -1557,18 +1557,18 @@ func (suit *PoolTestSuite) TestConcurrentBorrowAndEvict() {
suit.NoError(suit.pool.AddObject(ctx))

for i := 0; i < 5000; i++ {
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()

one := concurrentBorrowAndEvictGoroutine(ctx, true, suit.pool)
two := concurrentBorrowAndEvictGoroutine(ctx, false, suit.pool)
one := concurrentBorrowAndEvictGoroutine(timeoutCtx, true, suit.pool)
two := concurrentBorrowAndEvictGoroutine(timeoutCtx, false, suit.pool)

obj := <-one
close(one)
<-two
close(two)
suit.NotNil(obj)
suit.NoError(suit.pool.ReturnObject(ctx, obj))
suit.NoError(suit.pool.ReturnObject(timeoutCtx, obj))

//Uncomment suit for a progress indication
// if i%10 == 0 {
Expand Down Expand Up @@ -1643,7 +1643,7 @@ func waitTestGoroutine(ctx context.Context, pool *ObjectPool, pause time.Duratio

func (suit *PoolTestSuite) TestFIFO() {
ctx := context.Background()
suit.pool.Config.Lifo = false
suit.pool.Config.LIFO = false
suit.NoError(suit.pool.AddObject(ctx)) // "0"
suit.NoError(suit.pool.AddObject(ctx)) // "1"
suit.NoError(suit.pool.AddObject(ctx)) // "2"
Expand All @@ -1659,7 +1659,7 @@ func (suit *PoolTestSuite) TestFIFO() {

func (suit *PoolTestSuite) TestLIFO() {
ctx := context.Background()
suit.pool.Config.Lifo = true
suit.pool.Config.LIFO = true
suit.NoError(suit.pool.AddObject(ctx)) // "0"
suit.NoError(suit.pool.AddObject(ctx)) // "1"
suit.NoError(suit.pool.AddObject(ctx)) // "2"
Expand Down

0 comments on commit 3f5d5f8

Please sign in to comment.