-
Notifications
You must be signed in to change notification settings - Fork 150
/
object_test.go
57 lines (45 loc) · 1.25 KB
/
object_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pool
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPooledObject(t *testing.T) {
t.Parallel()
object := &TestObject{Num: 1}
pooledObject := NewPooledObject(object)
pooledObject.MarkReturning()
assert.Equal(t, StateReturning, pooledObject.GetState())
pooledObject.MarkAbandoned()
assert.Equal(t, StateAbandoned, pooledObject.GetState())
}
type TrackedUseObject struct {
lastUsed time.Time
}
func (o *TrackedUseObject) GetLastUsed() time.Time {
return o.lastUsed
}
func TestTrackedUse(t *testing.T) {
t.Parallel()
now := time.Now()
object := &TrackedUseObject{lastUsed: now}
trackedUse := TrackedUse(object)
assert.Equal(t, now, trackedUse.GetLastUsed())
pooledObject := NewPooledObject(object)
time.Sleep(20 * time.Millisecond)
pooledObject.Allocate()
time2 := pooledObject.GetLastUsedTime()
assert.True(t, now != time2)
object.lastUsed = time.Now()
time3 := pooledObject.GetLastUsedTime()
assert.Equal(t, object.lastUsed, time3)
}
func TestActiveTime(t *testing.T) {
t.Parallel()
object := &TrackedUseObject{}
pooledObject := NewPooledObject(object)
pooledObject.Allocate()
time.Sleep(20 * time.Millisecond)
pooledObject.Deallocate()
assert.True(t, pooledObject.GetActiveTime() >= 20*time.Millisecond)
}