-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregator_addr_test.go
118 lines (108 loc) · 2.31 KB
/
aggregator_addr_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
package main
import (
"bytes"
"fmt"
"net"
"reflect"
"testing"
"time"
uuid "github.com/google/uuid"
)
func TestCompileKafkaMsg(t *testing.T) {
keyID := keyID{UUID: uuid.New()}
walletID := uuid.New()
addr1, addr2, addr3 := "1.1.1.1", "2.2.2.2", "3.3.3.3"
addrs := AddressSet{
addr1: empty{},
addr3: empty{},
addr2: empty{},
}
msg, err := compileKafkaMsg(keyID, walletID, addrs)
if err != nil {
t.Fatalf("Expected no error but got: %v", err)
}
justification := `{\"keyid\":\"` + keyID.String() +
`\",\"addrs\":[\"` + addr1 + `\",\"` + addr2 + `\",\"` + addr3 + `\"]}`
expectedJSON := fmt.Sprintf("{\"wallet_id\":\"%s\","+
"\"service\":\"%s\","+
"\"signal\":\"%s\","+
"\"score\":0,"+
"\"justification\":\"%s\","+
"\"created_at\":\"%s\"}",
walletID,
schemaService,
schemaSignal,
justification,
time.Now().UTC().Format(time.RFC3339),
)
expectedMsg, err := avroEncode(ourCodec, []byte(expectedJSON))
if err != nil {
t.Fatalf("Failed to encode our JSON to Avro: %v", err)
}
if !bytes.Equal(msg, []byte(expectedMsg)) {
t.Fatalf("Expected\n%s\nbut got\n%s", expectedMsg, msg)
}
}
func TestAddrAggregatorProcess(t *testing.T) {
rawAddr1 := "1.2.3.4"
rawAddr2 := "2.3.4.5"
addr1 := net.ParseIP(rawAddr1)
addr2 := net.ParseIP(rawAddr2)
wallet1 := newV4(t)
tokenizer := newVerbatimTokenizer()
_ = tokenizer.resetKey()
a := newAddrAggregator()
a.use(tokenizer)
kID := tokenizer.keyID()
tests := []struct {
reqs []*clientRequest
addrs WalletsByKeyID
}{
// One wallet mapping to one address.
{
[]*clientRequest{
{
Addr: addr1,
Wallet: wallet1,
},
},
WalletsByKeyID{
*kID: AddrsByWallet{
wallet1: AddressSet{
ipv4Addr: empty{},
},
},
},
},
// One wallet mapping to two addresses.
{
[]*clientRequest{
{
Addr: addr1,
Wallet: wallet1,
},
{
Addr: addr2,
Wallet: wallet1,
},
},
WalletsByKeyID{
*kID: AddrsByWallet{
wallet1: AddressSet{
rawAddr1: empty{},
rawAddr2: empty{},
},
},
},
},
}
for _, test := range tests {
addrAggr := a.(*addrAggregator)
for _, req := range test.reqs {
_ = addrAggr.processRequest(req)
}
if !reflect.DeepEqual(addrAggr.addrs, test.addrs) {
t.Fatalf("Expected %+v but got %+v.", test.addrs, addrAggr.addrs)
}
}
}