forked from iotaledger/goshimmer
/
testutils.go
113 lines (92 loc) · 3.17 KB
/
testutils.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
package notarization
import (
"sync/atomic"
"testing"
"time"
"github.com/izuc/zipp.foundation/core/generics/event"
"github.com/izuc/zipp.foundation/core/types/confirmation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/izuc/zipp/packages/core/consensus/acceptance"
"github.com/izuc/zipp/packages/core/ledger/utxo"
"github.com/izuc/zipp/packages/core/mana"
)
const (
testingAcceptanceThreshold = 0.4
)
var (
// TestConflictAcceptanceStateTranslation translates a conflict's AW into a confirmation state.
TestConflictAcceptanceStateTranslation acceptance.ConflictThresholdTranslation = func(_ utxo.TransactionID, aw float64) confirmation.State {
if aw >= testingAcceptanceThreshold {
return confirmation.Accepted
}
return confirmation.Pending
}
// TestBlockAcceptanceStateTranslation translates a block's AW into a confirmation state.
TestBlockAcceptanceStateTranslation acceptance.BlockThresholdTranslation = func(aw float64) confirmation.State {
if aw >= testingAcceptanceThreshold {
return confirmation.Accepted
}
return confirmation.Pending
}
)
// EventMock acts as a container for event mocks.
type EventMock struct {
mock.Mock
expectedEvents uint64
calledEvents uint64
test *testing.T
attached []struct {
*event.Event[*EpochCommittableEvent]
*event.Closure[*EpochCommittableEvent]
}
}
// NewEventMock creates a new EventMock.
func NewEventMock(t *testing.T, notarizationManager *Manager) *EventMock {
e := &EventMock{
test: t,
}
// attach all events
notarizationManager.Events.EpochCommittable.Hook(event.NewClosure(e.EpochCommittable))
notarizationManager.Events.ManaVectorUpdate.Hook(event.NewClosure(e.ManaVectorUpdate))
return e
}
// DetachAll detaches all event handlers.
func (e *EventMock) DetachAll() {
for _, a := range e.attached {
a.Event.Detach(a.Closure)
}
}
// Expect is a proxy for Mock.On() but keeping track of num of calls.
func (e *EventMock) Expect(eventName string, arguments ...interface{}) {
event.Loop.PendingTasksCounter.WaitIsZero()
e.On(eventName, arguments...)
atomic.AddUint64(&e.expectedEvents, 1)
}
// AssertExpectations asserts expectations.
func (e *EventMock) AssertExpectations(t mock.TestingT) bool {
var calledEvents, expectedEvents uint64
event.Loop.PendingTasksCounter.WaitIsZero()
assert.Eventuallyf(t, func() bool {
calledEvents = atomic.LoadUint64(&e.calledEvents)
expectedEvents = atomic.LoadUint64(&e.expectedEvents)
return calledEvents == expectedEvents
}, 5*time.Second, 1*time.Millisecond, "number of called (%d) events is not equal to number of expected events (%d)", calledEvents, expectedEvents)
defer func() {
e.Calls = make([]mock.Call, 0)
e.ExpectedCalls = make([]*mock.Call, 0)
e.expectedEvents = 0
e.calledEvents = 0
}()
return e.Mock.AssertExpectations(t)
}
// EpochCommittable is the mocked EpochCommittable event.
func (e *EventMock) EpochCommittable(event *EpochCommittableEvent) {
e.Called(event.EI)
atomic.AddUint64(&e.calledEvents, 1)
}
// ManaVectorUpdate is the mocked ManaVectorUpdate event.
func (e *EventMock) ManaVectorUpdate(event *mana.ManaVectorUpdateEvent) {
e.Called(event.EI)
atomic.AddUint64(&e.calledEvents, 1)
}