-
Notifications
You must be signed in to change notification settings - Fork 66
/
tx_list_test.go
152 lines (132 loc) · 2.97 KB
/
tx_list_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package frostdb
import (
"slices"
"sync"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
)
func Test_TXList_Mark(t *testing.T) {
node := &TxNode{
next: &atomic.Pointer[TxNode]{},
original: &atomic.Pointer[TxNode]{},
}
next := &TxNode{
next: &atomic.Pointer[TxNode]{},
original: &atomic.Pointer[TxNode]{},
}
node.next.Store(next)
node.next.Store(getMarked(node))
node.original.Store(next)
require.NotNil(t, isMarked(node))
node.next.Store(getUnmarked(node))
require.Nil(t, isMarked(node))
}
func Test_TXList_Basic(t *testing.T) {
wm := atomic.Uint64{}
wm.Store(1) // set the watermark so that the sweeper won't remove any of our txs
p := NewTxPool(&wm)
txs := []uint64{9, 8, 7, 6, 4, 5, 3, 10}
for _, tx := range txs {
p.Insert(tx)
}
found := make([]uint64, 0, len(txs))
p.Iterate(func(tx uint64) bool {
found = append(found, tx)
return true
})
p.Stop() // stop the sweeper
require.True(t, slices.IsSorted(found))
require.Equal(t, 8, len(found))
}
func Test_TXList_Async(t *testing.T) {
wm := atomic.Uint64{}
p := NewTxPool(&wm)
tx := &atomic.Uint64{}
tx.Add(1) // adjust the tx id to ensure the sweeper doesn't drain the pool
// Swap writers that will each complete n transactions
n := 10
writers := 100
wg := &sync.WaitGroup{}
wg.Add(writers)
for i := 0; i < writers; i++ {
go func() {
defer wg.Done()
for j := 0; j < n; j++ {
p.Insert(tx.Add(1))
}
}()
}
wg.Wait()
found := make([]uint64, 0, writers*n)
p.Iterate(func(tx uint64) bool {
found = append(found, tx)
return true
})
require.True(t, slices.IsSorted(found))
require.Equal(t, n*writers, len(found))
p.Insert(1) // insert the missing tx to drain the pool
for v := wm.Load(); v < tx.Load(); v = wm.Load() {
// Satisfy linter with statement.
continue
}
require.Equal(t, tx.Load(), wm.Load())
// Verify the pool is empty
foundtx := false
p.Iterate(func(tx uint64) bool {
foundtx = true
return true
})
require.False(t, foundtx)
p.Stop() // stop the sweeper
}
func Benchmark_TXList_InsertAndDrain(b *testing.B) {
benchmarks := map[string]struct {
writers int
values int
}{
"10:100": {
writers: 10,
values: 100,
},
"10:1000": {
writers: 10,
values: 1000,
},
"100:100": {
writers: 100,
values: 100,
},
"1000:100": {
writers: 1000,
values: 100,
},
}
for name, benchmark := range benchmarks {
b.Run(name, func(b *testing.B) {
wm := atomic.Uint64{}
p := NewTxPool(&wm)
tx := &atomic.Uint64{}
wg := &sync.WaitGroup{}
for i := 0; i < b.N; i++ {
wg.Add(benchmark.writers)
for i := 0; i < benchmark.writers; i++ {
go func() {
defer wg.Done()
for j := 0; j < benchmark.values; j++ {
p.Insert(tx.Add(1))
}
}()
}
wg.Wait()
// Wait for the sweeper to drain
for v := wm.Load(); v < tx.Load(); v = wm.Load() {
// Satisfy linter with statement.
continue
}
require.Equal(b, tx.Load(), wm.Load())
}
p.Stop()
})
}
}