Unverified 提交 ad9cbbd2 作者: Steven Allen 提交者: GitHub

Merge pull request #6671 from ipfs/fix/6670

namesys(test): test TTL on publish
......@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"testing"
"time"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
......@@ -117,3 +118,51 @@ func TestPublishWithCache0(t *testing.T) {
t.Fatal(err)
}
}
func TestPublishWithTTL(t *testing.T) {
dst := dssync.MutexWrap(ds.NewMapDatastore())
priv, _, err := ci.GenerateKeyPair(ci.RSA, 2048)
if err != nil {
t.Fatal(err)
}
ps := pstoremem.NewPeerstore()
pid, err := peer.IDFromPrivateKey(priv)
if err != nil {
t.Fatal(err)
}
err = ps.AddPrivKey(pid, priv)
if err != nil {
t.Fatal(err)
}
routing := offroute.NewOfflineRouter(dst, record.NamespacedValidator{
"ipns": ipns.Validator{KeyBook: ps},
"pk": record.PublicKeyValidator{},
})
nsys := NewNameSystem(routing, dst, 128)
p, err := path.ParsePath(unixfs.EmptyDirNode().Cid().String())
if err != nil {
t.Fatal(err)
}
ttl := 1 * time.Second
eol := time.Now().Add(2 * time.Second)
ctx := context.WithValue(context.Background(), "ipns-publish-ttl", ttl)
err = nsys.Publish(ctx, priv, p)
if err != nil {
t.Fatal(err)
}
ientry, ok := nsys.(*mpns).cache.Get(pid.Pretty())
if !ok {
t.Fatal("cache get failed")
}
entry, ok := ientry.(cacheEntry)
if !ok {
t.Fatal("bad cache item returned")
}
if entry.eol.Sub(eol) > 10*time.Millisecond {
t.Fatalf("bad cache ttl: expected %s, got %s", eol, entry.eol)
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论