/
publisher_mock.go
66 lines (57 loc) · 2.01 KB
/
publisher_mock.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
package pub
import (
"fmt"
"sync"
"sync/atomic"
)
type MockMarketDataPublisher struct {
AccountPublished []*Accounts
BooksPublished []*Books
ExecutionResultsPublished []*ExecutionResults
BlockFeePublished []BlockFee
TransferPublished []Transfers
BlockPublished []*Block
Lock *sync.Mutex // as mock publisher is only used in testing, its no harm to have this granularity Lock
MessagePublished uint32 // atomic integer used to determine the published messages
}
func (publisher *MockMarketDataPublisher) publish(msg AvroOrJsonMsg, tpe msgType, height int64, timestamp int64) {
publisher.Lock.Lock()
defer publisher.Lock.Unlock()
switch tpe {
case accountsTpe:
publisher.AccountPublished = append(publisher.AccountPublished, msg.(*Accounts))
case booksTpe:
publisher.BooksPublished = append(publisher.BooksPublished, msg.(*Books))
case executionResultTpe:
publisher.ExecutionResultsPublished = append(publisher.ExecutionResultsPublished, msg.(*ExecutionResults))
case blockFeeTpe:
publisher.BlockFeePublished = append(publisher.BlockFeePublished, msg.(BlockFee))
case transferTpe:
publisher.TransferPublished = append(publisher.TransferPublished, msg.(Transfers))
case blockTpe:
publisher.BlockPublished = append(publisher.BlockPublished, msg.(*Block))
default:
panic(fmt.Errorf("does not support type %s", tpe.String()))
}
atomic.AddUint32(&publisher.MessagePublished, 1)
}
func (publisher *MockMarketDataPublisher) Stop() {
publisher.Lock.Lock()
defer publisher.Lock.Unlock()
publisher.AccountPublished = make([]*Accounts, 0)
publisher.BooksPublished = make([]*Books, 0)
publisher.ExecutionResultsPublished = make([]*ExecutionResults, 0)
}
func NewMockMarketDataPublisher() (publisher *MockMarketDataPublisher) {
publisher = &MockMarketDataPublisher{
make([]*Accounts, 0),
make([]*Books, 0),
make([]*ExecutionResults, 0),
make([]BlockFee, 0),
make([]Transfers, 0),
make([]*Block, 0),
&sync.Mutex{},
0,
}
return publisher
}