This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathevent_subscription_test.go
202 lines (174 loc) · 4.47 KB
/
event_subscription_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package txpool
import (
"context"
cryptoRand "crypto/rand"
"math/big"
mathRand "math/rand"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/0xPolygon/polygon-edge/helper/tests"
"github.com/0xPolygon/polygon-edge/txpool/proto"
"github.com/0xPolygon/polygon-edge/types"
"github.com/stretchr/testify/assert"
)
func shuffleTxPoolEvents(
supportedTypes []proto.EventType,
count int,
numInvalid int,
) []*proto.TxPoolEvent {
if count == 0 || len(supportedTypes) == 0 {
return []*proto.TxPoolEvent{}
}
if numInvalid > count {
numInvalid = count
}
allEvents := []proto.EventType{
proto.EventType_ADDED,
proto.EventType_PROMOTED,
proto.EventType_PROMOTED,
proto.EventType_DROPPED,
proto.EventType_DEMOTED,
}
txHash := types.StringToHash("123")
tempSubscription := &eventSubscription{eventTypes: supportedTypes}
randomEventType := func(supported bool) proto.EventType {
for {
randNum, _ := cryptoRand.Int(cryptoRand.Reader, big.NewInt(int64(len(supportedTypes))))
randType := allEvents[randNum.Int64()]
if tempSubscription.eventSupported(randType) == supported {
return randType
}
}
}
events := make([]*proto.TxPoolEvent, 0)
// Fill in the unsupported events first
for invalidFilled := 0; invalidFilled < numInvalid; invalidFilled++ {
events = append(events, &proto.TxPoolEvent{
TxHash: txHash.String(),
Type: randomEventType(false),
})
}
// Fill in the supported events
for validFilled := 0; validFilled < count-numInvalid; validFilled++ {
events = append(events, &proto.TxPoolEvent{
TxHash: txHash.String(),
Type: randomEventType(true),
})
}
// Shuffle the events
mathRand.Seed(time.Now().UTC().UnixNano())
mathRand.Shuffle(len(events), func(i, j int) {
events[i], events[j] = events[j], events[i]
})
return events
}
func TestEventSubscription_ProcessedEvents(t *testing.T) {
// Set up the default values
supportedEvents := []proto.EventType{
proto.EventType_ADDED,
proto.EventType_ENQUEUED,
proto.EventType_DROPPED,
}
testTable := []struct {
name string
events []*proto.TxPoolEvent
expectedProcessed int
}{
{
"All supported events processed",
shuffleTxPoolEvents(supportedEvents, 10, 0),
10,
},
{
"All unsupported events not processed",
shuffleTxPoolEvents(supportedEvents, 10, 10),
0,
},
{
"Mixed events processed",
shuffleTxPoolEvents(supportedEvents, 10, 6),
4,
},
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
subscription := &eventSubscription{
eventTypes: supportedEvents,
outputCh: make(chan *proto.TxPoolEvent, len(testCase.events)),
doneCh: make(chan struct{}),
eventStore: &eventQueue{
events: make([]*proto.TxPoolEvent, 0),
},
notifyCh: make(chan struct{}),
}
go subscription.runLoop()
// Set the event listener
processed := int64(0)
go func() {
for range subscription.outputCh {
atomic.AddInt64(&processed, 1)
}
}()
// Fire off the events
var wg sync.WaitGroup
for _, event := range testCase.events {
wg.Add(1)
go func(event *proto.TxPoolEvent) {
defer wg.Done()
subscription.pushEvent(event)
}(event)
}
wg.Wait()
eventWaitCtx, eventWaitFn := context.WithTimeout(context.Background(), 10*time.Second)
defer eventWaitFn()
if _, err := tests.RetryUntilTimeout(eventWaitCtx, func() (interface{}, bool) {
return nil, atomic.LoadInt64(&processed) < int64(testCase.expectedProcessed)
}); err != nil {
t.Fatalf("Unable to wait for events to be processed, %v", err)
}
subscription.close()
assert.Equal(t, int64(testCase.expectedProcessed), processed)
})
}
}
func TestEventSubscription_EventSupported(t *testing.T) {
t.Parallel()
supportedEvents := []proto.EventType{
proto.EventType_ADDED,
proto.EventType_PROMOTED,
proto.EventType_DEMOTED,
}
subscription := &eventSubscription{
eventTypes: supportedEvents,
}
testTable := []struct {
name string
events []proto.EventType
supported bool
}{
{
"Supported events processed",
supportedEvents,
true,
},
{
"Unsupported events not processed",
[]proto.EventType{
proto.EventType_DROPPED,
proto.EventType_ENQUEUED,
},
false,
},
}
for _, testCase := range testTable {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
for _, eventType := range testCase.events {
assert.Equal(t, testCase.supported, subscription.eventSupported(eventType))
}
})
}
}