-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathrepub_test.go
124 lines (102 loc) · 3.28 KB
/
repub_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
package republisher_test
import (
"errors"
"testing"
"time"
goprocess "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
"github.com/ipfs/go-ipfs/core"
mock "github.com/ipfs/go-ipfs/core/mock"
namesys "github.com/ipfs/go-ipfs/namesys"
. "github.com/ipfs/go-ipfs/namesys/republisher"
path "github.com/ipfs/go-ipfs/path"
mocknet "gx/ipfs/QmUuwQUJmtvC6ReYcu7xaYKEUM3pD46H18dFn3LBhVt2Di/go-libp2p/p2p/net/mock"
pstore "gx/ipfs/QmdMfSLMDBDYhtc4oF3NYGCZr5dy4wQb6Ji26N4D4mdxa2/go-libp2p-peerstore"
)
func TestRepublish(t *testing.T) {
// set cache life to zero for testing low-period repubs
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create network
mn := mocknet.New(ctx)
var nodes []*core.IpfsNode
for i := 0; i < 10; i++ {
nd, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: mock.MockHostOption(mn),
})
if err != nil {
t.Fatal(err)
}
nd.Namesys = namesys.NewNameSystem(nd.Routing, nd.Repo.Datastore(), 0)
nodes = append(nodes, nd)
}
mn.LinkAll()
bsinf := core.BootstrapConfigWithPeers(
[]pstore.PeerInfo{
nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
},
)
for _, n := range nodes[1:] {
if err := n.Bootstrap(bsinf); err != nil {
t.Fatal(err)
}
}
// have one node publish a record that is valid for 1 second
publisher := nodes[3]
p := path.FromString("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn") // does not need to be valid
rp := namesys.NewRoutingPublisher(publisher.Routing, publisher.Repo.Datastore())
err := rp.PublishWithEOL(ctx, publisher.PrivateKey, p, time.Now().Add(time.Second))
if err != nil {
t.Fatal(err)
}
name := "/ipns/" + publisher.Identity.Pretty()
if err := verifyResolution(nodes, name, p); err != nil {
t.Fatal(err)
}
// Now wait a second, the records will be invalid and we should fail to resolve
time.Sleep(time.Second)
if err := verifyResolutionFails(nodes, name); err != nil {
t.Fatal(err)
}
// The republishers that are contained within the nodes have their timeout set
// to 12 hours. Instead of trying to tweak those, we're just going to pretend
// they dont exist and make our own.
repub := NewRepublisher(publisher.Routing, publisher.Repo.Datastore(), publisher.Peerstore)
repub.Interval = time.Second
repub.RecordLifetime = time.Second * 5
repub.AddName(publisher.Identity)
proc := goprocess.Go(repub.Run)
defer proc.Close()
// now wait a couple seconds for it to fire
time.Sleep(time.Second * 2)
// we should be able to resolve them now
if err := verifyResolution(nodes, name, p); err != nil {
t.Fatal(err)
}
}
func verifyResolution(nodes []*core.IpfsNode, key string, exp path.Path) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, n := range nodes {
val, err := n.Namesys.Resolve(ctx, key)
if err != nil {
return err
}
if val != exp {
return errors.New("resolved wrong record")
}
}
return nil
}
func verifyResolutionFails(nodes []*core.IpfsNode, key string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, n := range nodes {
_, err := n.Namesys.Resolve(ctx, key)
if err == nil {
return errors.New("expected resolution to fail")
}
}
return nil
}