-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton_test.go
More file actions
59 lines (49 loc) · 1.37 KB
/
Copy pathsingleton_test.go
File metadata and controls
59 lines (49 loc) · 1.37 KB
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
58
59
package goref
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSingleton(t *testing.T) {
Ref("hello").Deref()
ref := Ref("world")
snap1 := GetSnapshot()
ref.Deref()
snap2 := GetSnapshot()
// current state
assert.Contains(t, instance.data, "hello")
assert.Contains(t, instance.data, "world")
d := instance.get("hello")
assert.Equal(t, int32(0), d.active)
assert.Equal(t, int64(1), d.count)
d = instance.get("world")
assert.Equal(t, int32(0), d.active)
assert.Equal(t, int64(1), d.count)
// reset instance
Reset()
Ref("bla").Deref()
GetSnapshot() // synchronize
assert.NotContains(t, instance.data, "hello")
assert.Contains(t, instance.data, "bla")
//
// check Snapshot data after the fact
//
// snap1: Ref('hello'), Deref('hello'), Ref('world')
d1 := snap1.Data["hello"]
assert.Equal(t, int32(0), d1.Active)
assert.Equal(t, int64(1), d1.Count)
d2 := snap1.Data["world"]
assert.Equal(t, int32(1), d2.Active)
assert.Equal(t, int64(0), d2.Count)
assert.Equal(t, time.Duration(0), d2.Duration)
assert.Equal(t, 2, len(snap1.Data))
// snap2: snap1 + Deref('world')
d1 = snap2.Data["hello"]
assert.Equal(t, int32(0), d1.Active)
assert.Equal(t, int64(1), d1.Count)
d2 = snap2.Data["world"]
assert.Equal(t, int32(0), d2.Active)
assert.Equal(t, int64(1), d2.Count)
assert.True(t, d2.Duration > 0)
assert.Equal(t, 2, len(snap2.Data))
}