Skip to content

Commit ec28914

Browse files
author
stefano galassi
committed
#14 completed testing mempool service
1 parent b26f70e commit ec28914

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

common/schema

core/service/mempoolCoreService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func sortFeePerByteThenTimestampThenID(members []*model.MempoolTransaction) {
199199
mi, mj := members[i], members[j]
200200
switch {
201201
case mi.FeePerByte != mj.FeePerByte:
202-
return mi.FeePerByte < mj.FeePerByte
202+
return mi.FeePerByte > mj.FeePerByte
203203
case mi.ArrivalTimestamp != mj.ArrivalTimestamp:
204204
return mi.ArrivalTimestamp < mj.ArrivalTimestamp
205205
default:

core/service/mempoolCoreService_test.go

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"database/sql"
55
"errors"
6+
"math"
67
"reflect"
78
"regexp"
89
"testing"
@@ -25,9 +26,13 @@ func (*mockMempoolQueryExecutorSuccess) ExecuteSelect(qe string, args ...interfa
2526
defer db.Close()
2627
switch qe {
2728
case "SELECT ID, FeePerByte, ArrivalTimestamp, TransactionBytes FROM mempool":
28-
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(sqlmock.NewRows([]string{
29-
"ID", "FeePerByte", "ArrivalTimestamp", "TransactionBytes"},
30-
).AddRow(make([]byte, 32), 1, 1562893302, []byte{}))
29+
mockedRows := sqlmock.NewRows([]string{"ID", "FeePerByte", "ArrivalTimestamp", "TransactionBytes"})
30+
mockedRows.AddRow(make([]byte, 32), 1, 1562893305, []byte{5, 5, 5, 5, 5})
31+
mockedRows.AddRow(make([]byte, 32), 10, 1562893304, []byte{2, 2, 2, 2, 2})
32+
mockedRows.AddRow(make([]byte, 32), 1, 1562893302, []byte{1, 1, 1, 1, 1})
33+
mockedRows.AddRow(make([]byte, 32), 100, 1562893306, []byte{4, 4, 4, 4, 4})
34+
mockedRows.AddRow(make([]byte, 32), 5, 1562893303, []byte{4, 4, 4, 4, 4})
35+
mock.ExpectQuery(regexp.QuoteMeta(qe)).WillReturnRows(mockedRows)
3136
case "SELECT ID, FeePerByte, ArrivalTimestamp, TransactionBytes FROM mempool WHERE id = :id":
3237
return nil, errors.New("MempoolTransactionNotFound")
3338
}
@@ -155,11 +160,35 @@ func TestMempoolService_GetMempoolTransactions(t *testing.T) {
155160
QueryExecutor: &mockMempoolQueryExecutorSuccess{},
156161
},
157162
want: []*model.MempoolTransaction{
163+
{
164+
ID: make([]byte, 32),
165+
FeePerByte: 1,
166+
ArrivalTimestamp: 1562893305,
167+
TransactionBytes: []byte{5, 5, 5, 5, 5},
168+
},
169+
{
170+
ID: make([]byte, 32),
171+
FeePerByte: 10,
172+
ArrivalTimestamp: 1562893304,
173+
TransactionBytes: []byte{2, 2, 2, 2, 2},
174+
},
158175
{
159176
ID: make([]byte, 32),
160177
FeePerByte: 1,
161178
ArrivalTimestamp: 1562893302,
162-
TransactionBytes: []byte{},
179+
TransactionBytes: []byte{1, 1, 1, 1, 1},
180+
},
181+
{
182+
ID: make([]byte, 32),
183+
FeePerByte: 100,
184+
ArrivalTimestamp: 1562893306,
185+
TransactionBytes: []byte{4, 4, 4, 4, 4},
186+
},
187+
{
188+
ID: make([]byte, 32),
189+
FeePerByte: 5,
190+
ArrivalTimestamp: 1562893303,
191+
TransactionBytes: []byte{4, 4, 4, 4, 4},
163192
},
164193
},
165194
wantErr: false,
@@ -301,3 +330,77 @@ func TestMempoolService_RemoveMempoolTransaction(t *testing.T) {
301330
})
302331
}
303332
}
333+
334+
func TestMempoolService_SelectTransactionsFromMempool(t *testing.T) {
335+
type fields struct {
336+
Chaintype contract.ChainType
337+
QueryExecutor query.ExecutorInterface
338+
MempoolQuery query.MempoolQueryInterface
339+
}
340+
type args struct {
341+
blockTimestamp int64
342+
}
343+
tests := []struct {
344+
name string
345+
fields fields
346+
args args
347+
want []*model.MempoolTransaction
348+
wantErr bool
349+
}{
350+
{
351+
name: "SelectTransactionsFromMempool:Success",
352+
fields: fields{
353+
Chaintype: &chaintype.MainChain{},
354+
MempoolQuery: query.NewMempoolQuery(&chaintype.MainChain{}),
355+
QueryExecutor: &mockMempoolQueryExecutorSuccess{},
356+
},
357+
args: args{
358+
blockTimestamp: math.MaxInt64,
359+
},
360+
want: []*model.MempoolTransaction{
361+
{
362+
ID: make([]byte, 32),
363+
FeePerByte: 100,
364+
ArrivalTimestamp: 1562893306,
365+
TransactionBytes: []byte{4, 4, 4, 4, 4},
366+
},
367+
{
368+
ID: make([]byte, 32),
369+
FeePerByte: 10,
370+
ArrivalTimestamp: 1562893304,
371+
TransactionBytes: []byte{2, 2, 2, 2, 2},
372+
},
373+
{
374+
ID: make([]byte, 32),
375+
FeePerByte: 1,
376+
ArrivalTimestamp: 1562893302,
377+
TransactionBytes: []byte{1, 1, 1, 1, 1},
378+
},
379+
{
380+
ID: make([]byte, 32),
381+
FeePerByte: 1,
382+
ArrivalTimestamp: 1562893305,
383+
TransactionBytes: []byte{5, 5, 5, 5, 5},
384+
},
385+
},
386+
wantErr: false,
387+
},
388+
}
389+
for _, tt := range tests {
390+
t.Run(tt.name, func(t *testing.T) {
391+
mps := &MempoolService{
392+
Chaintype: tt.fields.Chaintype,
393+
QueryExecutor: tt.fields.QueryExecutor,
394+
MempoolQuery: tt.fields.MempoolQuery,
395+
}
396+
got, err := mps.SelectTransactionsFromMempool(tt.args.blockTimestamp)
397+
if (err != nil) != tt.wantErr {
398+
t.Errorf("MempoolService.SelectTransactionsFromMempool() error = %v, wantErr %v", err, tt.wantErr)
399+
return
400+
}
401+
if !reflect.DeepEqual(got, tt.want) {
402+
t.Errorf("MempoolService.SelectTransactionsFromMempool() = %v, want %v", got, tt.want)
403+
}
404+
})
405+
}
406+
}

0 commit comments

Comments
 (0)