Skip to content

Commit

Permalink
Merge pull request #6671 from ipfs/fix/6670
Browse files Browse the repository at this point in the history
namesys(test): test TTL on publish
  • Loading branch information
Stebalien authored Sep 24, 2019
2 parents 6e1e6db + 1858ccd commit ad9cbbd
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions namesys/namesys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"testing"
"time"

ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit ad9cbbd

Please sign in to comment.