-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tx_test.go
152 lines (124 loc) · 3.26 KB
/
main_tx_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 main
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type MockedGetTxByTxidReturn struct {
Txs []RpcTx
E error
}
type MockedTxGetter struct {
CallsCount int
TxidArgs []string
Returns []MockedGetTxByTxidReturn
}
func (g *MockedTxGetter) GetTransferByTxid(c context.Context, t string) ([]RpcTx, error) {
g.CallsCount++
g.TxidArgs = append(g.TxidArgs, t)
result := g.Returns[0]
if len(g.Returns) > 1 {
g.Returns = g.Returns[1:]
} else {
g.Returns = []MockedGetTxByTxidReturn{}
}
return result.Txs, result.E
}
type MockedTxPublisher struct {
CallsCount int
TxArgs []Tx
Returns []error
}
func (g *MockedTxPublisher) PushTxEvent(tx Tx) error {
g.CallsCount++
g.TxArgs = append(g.TxArgs, tx)
result := g.Returns[0]
if len(g.Returns) > 1 {
g.Returns = g.Returns[1:]
} else {
g.Returns = []error{}
}
return result
}
func TestProcessTxid(t *testing.T) {
t.Run("Success", func(t *testing.T) {
txid := "dummy tx"
txHeight := 3
txGetter := MockedTxGetter{
Returns: []MockedGetTxByTxidReturn{
{
E: nil,
Txs: []RpcTx{{TXID: txid, Type: "in", Height: txHeight}},
},
},
}
evPublisher := MockedTxPublisher{Returns: []error{nil}}
ignoreBelowHeight := 0 // Don't ignore any Tx
err := ProcessTxid(txid, ignoreBelowHeight, &txGetter, &evPublisher)
assert.Nil(t, err)
assert.Equal(t, 1, evPublisher.CallsCount)
assert.Equal(t, txid, evPublisher.TxArgs[0].TXID)
})
t.Run("Success, Tx below ignoring height", func(t *testing.T) {
txid := "dummy tx"
txHeight := 3
txGetter := MockedTxGetter{
Returns: []MockedGetTxByTxidReturn{
{
E: nil,
Txs: []RpcTx{{TXID: txid, Type: "in", Height: txHeight}},
},
},
}
evPublisher := MockedTxPublisher{Returns: []error{nil}}
ignoreBelowHeight := txHeight + 2 // The Tx will be ignored
err := ProcessTxid(txid, ignoreBelowHeight, &txGetter, &evPublisher)
assert.Nil(t, err)
assert.Equal(t, 1, txGetter.CallsCount)
// The Tx was not pushed to NATS, because it is below ignoring height
assert.Equal(t, 0, evPublisher.CallsCount)
})
t.Run("RPC Error", func(t *testing.T) {
txid := "dummy tx"
txGetter := MockedTxGetter{
Returns: []MockedGetTxByTxidReturn{
{
E: fmt.Errorf("Dummy Error"),
Txs: nil,
},
},
}
evPublisher := MockedTxPublisher{
Returns: []error{nil},
}
ignoreBelowHeight := 0
err := ProcessTxid(txid, ignoreBelowHeight, &txGetter, &evPublisher)
assert.Error(t, err)
assert.Equal(t, 1, txGetter.CallsCount)
assert.Equal(t, txid, txGetter.TxidArgs[0])
assert.Equal(t, 0, evPublisher.CallsCount)
})
t.Run("Event publishing fails", func(t *testing.T) {
txid := "dummy tx"
txHeight := 3
txGetter := MockedTxGetter{
Returns: []MockedGetTxByTxidReturn{
{
E: nil,
Txs: []RpcTx{{TXID: txid, Type: "in", Height: txHeight}},
},
},
}
evPublisher := MockedTxPublisher{
Returns: []error{fmt.Errorf("Dummy NATS Error")},
}
ignoreBelowHeight := 0
err := ProcessTxid(txid, ignoreBelowHeight, &txGetter, &evPublisher)
assert.Error(t, err)
assert.Equal(t, 1, txGetter.CallsCount)
assert.Equal(t, txid, txGetter.TxidArgs[0])
assert.Equal(t, 1, evPublisher.CallsCount)
assert.Equal(t, txid, evPublisher.TxArgs[0].TXID)
})
}