-
Notifications
You must be signed in to change notification settings - Fork 12
/
fetch_test.go
109 lines (83 loc) · 2.69 KB
/
fetch_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
package namesys
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/libp2p/go-libp2p/core/host"
)
func connect(t *testing.T, a, b host.Host) {
pinfo := a.Peerstore().PeerInfo(a.ID())
err := b.Connect(context.Background(), pinfo)
if err != nil {
t.Fatal(err)
}
}
type datastore struct {
data map[string][]byte
}
func (d *datastore) Lookup(_ context.Context, key string) ([]byte, error) {
v, ok := d.data[key]
if !ok {
return nil, errors.New("key not found")
}
return v, nil
}
func TestFetchProtocolTrip(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := newNetHosts(ctx, t, 2)
connect(t, hosts[0], hosts[1])
// wait for hosts to get connected
time.Sleep(time.Millisecond * 100)
d1 := &datastore{map[string][]byte{"key": []byte("value1")}}
h1 := newFetchProtocol(ctx, hosts[0], d1.Lookup)
d2 := &datastore{map[string][]byte{"key": []byte("value2")}}
h2 := newFetchProtocol(ctx, hosts[1], d2.Lookup)
fetchCheck(ctx, t, h1, h2, "key", []byte("value2"))
fetchCheck(ctx, t, h2, h1, "key", []byte("value1"))
}
func TestFetchProtocolNotFound(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := newNetHosts(ctx, t, 2)
connect(t, hosts[0], hosts[1])
// wait for hosts to get connected
time.Sleep(time.Millisecond * 100)
d1 := &datastore{map[string][]byte{"key": []byte("value1")}}
h1 := newFetchProtocol(ctx, hosts[0], d1.Lookup)
d2 := &datastore{make(map[string][]byte)}
h2 := newFetchProtocol(ctx, hosts[1], d2.Lookup)
fetchCheck(ctx, t, h1, h2, "key", nil)
fetchCheck(ctx, t, h2, h1, "key", []byte("value1"))
}
func TestFetchProtocolRepeated(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := newNetHosts(ctx, t, 2)
connect(t, hosts[0], hosts[1])
// wait for hosts to get connected
time.Sleep(time.Millisecond * 100)
d1 := &datastore{map[string][]byte{"key": []byte("value1")}}
h1 := newFetchProtocol(ctx, hosts[0], d1.Lookup)
d2 := &datastore{make(map[string][]byte)}
h2 := newFetchProtocol(ctx, hosts[1], d2.Lookup)
for i := 0; i < 10; i++ {
fetchCheck(ctx, t, h1, h2, "key", nil)
fetchCheck(ctx, t, h2, h1, "key", []byte("value1"))
}
}
func fetchCheck(ctx context.Context, t *testing.T,
requester *fetchProtocol, responder *fetchProtocol, key string, expected []byte) {
data, err := requester.Fetch(ctx, responder.host.ID(), key)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, expected) {
t.Fatalf("expected: %v, received: %v", string(expected), string(data))
}
if (data == nil && expected != nil) || (data != nil && expected == nil) {
t.Fatalf("expected []byte{} or nil and received the opposite")
}
}