namesys(test): test TTL on publish
No fix is complete without a test. fixes #6670
Steven Allen committed
Sep 23, 2019 at 17:52 UTC
1858ccdd3579e909adea8b182b982b34467af9cb
1 file changed
+49
namesys/namesys_test.go
+49
@@ -4,6 +4,7 @@ import (
4
"context"
5
"fmt"
6
"testing"
7
+ "time"
8
9
ds "github.com/ipfs/go-datastore"
10
dssync "github.com/ipfs/go-datastore/sync"
@@ -117,3 +118,51 @@ func TestPublishWithCache0(t *testing.T) {
118
t.Fatal(err)
119
}
120
}
121
+
122
+func TestPublishWithTTL(t *testing.T) {
123
+ dst := dssync.MutexWrap(ds.NewMapDatastore())
124
+ priv, _, err := ci.GenerateKeyPair(ci.RSA, 2048)
125
+ if err != nil {
126
+ t.Fatal(err)
127
+ }
128
+ ps := pstoremem.NewPeerstore()
129
+ pid, err := peer.IDFromPrivateKey(priv)
130
+ if err != nil {
131
+ t.Fatal(err)
132
+ }
133
+ err = ps.AddPrivKey(pid, priv)
134
+ if err != nil {
135
+ t.Fatal(err)
136
+ }
137
+
138
+ routing := offroute.NewOfflineRouter(dst, record.NamespacedValidator{
139
+ "ipns": ipns.Validator{KeyBook: ps},
140
+ "pk": record.PublicKeyValidator{},
141
+ })
142
+
143
+ nsys := NewNameSystem(routing, dst, 128)
144
+ p, err := path.ParsePath(unixfs.EmptyDirNode().Cid().String())
145
+ if err != nil {
146
+ t.Fatal(err)
147
+ }
148
+
149
+ ttl := 1 * time.Second
150
+ eol := time.Now().Add(2 * time.Second)
151
+
152
+ ctx := context.WithValue(context.Background(), "ipns-publish-ttl", ttl)
153
+ err = nsys.Publish(ctx, priv, p)
154
+ if err != nil {
155
+ t.Fatal(err)
156
+ }
157
+ ientry, ok := nsys.(*mpns).cache.Get(pid.Pretty())
158
+ if !ok {
159
+ t.Fatal("cache get failed")
160
+ }
161
+ entry, ok := ientry.(cacheEntry)
162
+ if !ok {
163
+ t.Fatal("bad cache item returned")
164
+ }
165
+ if entry.eol.Sub(eol) > 10*time.Millisecond {
166
+ t.Fatalf("bad cache ttl: expected %s, got %s", eol, entry.eol)
167
+ }
168
+}