mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 09:34:03 +08:00
ipns TestFastRepublish
This commit is contained in:
@ -9,7 +9,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
fstest "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
|
fstest "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
|
||||||
"github.com/jbenet/go-ipfs/core"
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func randBytes(size int) []byte {
|
func randBytes(size int) []byte {
|
||||||
@ -19,7 +20,10 @@ func randBytes(size int) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeFile(t *testing.T, size int, path string) []byte {
|
func writeFile(t *testing.T, size int, path string) []byte {
|
||||||
data := randBytes(size)
|
return writeFileData(t, randBytes(size), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeFileData(t *testing.T, data []byte, path string) []byte {
|
||||||
fi, err := os.Create(path)
|
fi, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -179,6 +183,106 @@ func TestAppendFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFastRepublish(t *testing.T) {
|
||||||
|
|
||||||
|
// make timeout noticeable.
|
||||||
|
osrt := shortRepublishTimeout
|
||||||
|
shortRepublishTimeout = time.Millisecond * 100
|
||||||
|
|
||||||
|
olrt := longRepublishTimeout
|
||||||
|
longRepublishTimeout = time.Second
|
||||||
|
|
||||||
|
node, mnt := setupIpnsTest(t, nil)
|
||||||
|
|
||||||
|
h, err := node.Identity.PrivKey.GetPublic().Hash()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
pubkeyHash := u.Key(h).Pretty()
|
||||||
|
|
||||||
|
// set them back
|
||||||
|
defer func() {
|
||||||
|
shortRepublishTimeout = osrt
|
||||||
|
longRepublishTimeout = olrt
|
||||||
|
mnt.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
closed := make(chan struct{})
|
||||||
|
dataA := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
||||||
|
dataB := []byte("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
|
||||||
|
|
||||||
|
fname := mnt.Dir + "/local/file"
|
||||||
|
|
||||||
|
// get first resolved hash
|
||||||
|
log.Debug("publishing first hash")
|
||||||
|
writeFileData(t, dataA, fname) // random
|
||||||
|
<-time.After(shortRepublishTimeout * 11 / 10)
|
||||||
|
log.Debug("resolving first hash")
|
||||||
|
resolvedHash, err := node.Namesys.Resolve(pubkeyHash)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("resolve err:", pubkeyHash, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// constantly keep writing to the file
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-closed:
|
||||||
|
return
|
||||||
|
|
||||||
|
case <-time.After(shortRepublishTimeout * 8 / 10):
|
||||||
|
writeFileData(t, dataB, fname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
hasPublished := func() bool {
|
||||||
|
res, err := node.Namesys.Resolve(pubkeyHash)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("resolve err: %v", err)
|
||||||
|
}
|
||||||
|
return res != resolvedHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// test things
|
||||||
|
|
||||||
|
// at this point, should not have written dataA and not have written dataB
|
||||||
|
rbuf, err := ioutil.ReadFile(fname)
|
||||||
|
if err != nil || !bytes.Equal(rbuf, dataA) {
|
||||||
|
t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasPublished() {
|
||||||
|
t.Fatal("published (wrote)")
|
||||||
|
}
|
||||||
|
|
||||||
|
<-time.After(shortRepublishTimeout * 11 / 10)
|
||||||
|
|
||||||
|
// at this point, should have written written dataB, but not published it
|
||||||
|
rbuf, err = ioutil.ReadFile(fname)
|
||||||
|
if err != nil || !bytes.Equal(rbuf, dataB) {
|
||||||
|
t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasPublished() {
|
||||||
|
t.Fatal("published (wrote)")
|
||||||
|
}
|
||||||
|
|
||||||
|
<-time.After(longRepublishTimeout * 11 / 10)
|
||||||
|
|
||||||
|
// at this point, should have written written dataB, and published it
|
||||||
|
rbuf, err = ioutil.ReadFile(fname)
|
||||||
|
if err != nil || !bytes.Equal(rbuf, dataB) {
|
||||||
|
t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasPublished() {
|
||||||
|
t.Fatal("not published")
|
||||||
|
}
|
||||||
|
|
||||||
|
close(closed)
|
||||||
|
}
|
||||||
|
|
||||||
// Test writing a medium sized file one byte at a time
|
// Test writing a medium sized file one byte at a time
|
||||||
func TestMultiWrite(t *testing.T) {
|
func TestMultiWrite(t *testing.T) {
|
||||||
_, mnt := setupIpnsTest(t, nil)
|
_, mnt := setupIpnsTest(t, nil)
|
||||||
|
@ -21,6 +21,11 @@ import (
|
|||||||
|
|
||||||
var log = u.Logger("ipns")
|
var log = u.Logger("ipns")
|
||||||
|
|
||||||
|
var (
|
||||||
|
shortRepublishTimeout = time.Millisecond * 5
|
||||||
|
longRepublishTimeout = time.Millisecond * 500
|
||||||
|
)
|
||||||
|
|
||||||
// FileSystem is the readwrite IPNS Fuse Filesystem.
|
// FileSystem is the readwrite IPNS Fuse Filesystem.
|
||||||
type FileSystem struct {
|
type FileSystem struct {
|
||||||
Ipfs *core.IpfsNode
|
Ipfs *core.IpfsNode
|
||||||
@ -71,7 +76,7 @@ func CreateRoot(n *core.IpfsNode, keys []ci.PrivKey, ipfsroot string) (*Root, er
|
|||||||
nd := new(Node)
|
nd := new(Node)
|
||||||
nd.Ipfs = n
|
nd.Ipfs = n
|
||||||
nd.key = k
|
nd.key = k
|
||||||
nd.repub = NewRepublisher(nd, time.Millisecond*5, time.Millisecond*500)
|
nd.repub = NewRepublisher(nd, shortRepublishTimeout, longRepublishTimeout)
|
||||||
|
|
||||||
go nd.repub.Run()
|
go nd.repub.Run()
|
||||||
|
|
||||||
|
@ -22,28 +22,21 @@ func (np *Republisher) Run() {
|
|||||||
for _ = range np.Publish {
|
for _ = range np.Publish {
|
||||||
quick := time.After(np.TimeoutShort)
|
quick := time.After(np.TimeoutShort)
|
||||||
longer := time.After(np.TimeoutLong)
|
longer := time.After(np.TimeoutLong)
|
||||||
for {
|
|
||||||
select {
|
wait:
|
||||||
case <-quick:
|
select {
|
||||||
//Do the publish!
|
case <-quick:
|
||||||
log.Info("Publishing Changes!")
|
case <-longer:
|
||||||
err := np.node.republishRoot()
|
case <-np.Publish:
|
||||||
if err != nil {
|
quick = time.After(np.TimeoutShort)
|
||||||
log.Critical("republishRoot error: %s", err)
|
goto wait
|
||||||
}
|
|
||||||
goto done
|
|
||||||
case <-longer:
|
|
||||||
//Do the publish!
|
|
||||||
log.Info("Publishing Changes!")
|
|
||||||
err := np.node.republishRoot()
|
|
||||||
if err != nil {
|
|
||||||
log.Critical("republishRoot error: %s", err)
|
|
||||||
}
|
|
||||||
goto done
|
|
||||||
case <-np.Publish:
|
|
||||||
quick = time.After(np.TimeoutShort)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
done:
|
|
||||||
|
log.Info("Publishing Changes!")
|
||||||
|
err := np.node.republishRoot()
|
||||||
|
if err != nil {
|
||||||
|
log.Critical("republishRoot error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user