Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 656dc88

Browse files
committedAug 5, 2015
test coverage on mfs
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 6adc786 commit 656dc88

File tree

3 files changed

+197
-15
lines changed

3 files changed

+197
-15
lines changed
 

‎mfs/mfs_test.go

+118-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
1515
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
1616
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
17+
1718
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
1819
key "github.com/ipfs/go-ipfs/blocks/key"
1920
bserv "github.com/ipfs/go-ipfs/blockservice"
@@ -112,6 +113,20 @@ func assertDirAtPath(root *Directory, path string, children []string) error {
112113
return nil
113114
}
114115

116+
func compStrArrs(a, b []string) bool {
117+
if len(a) != len(b) {
118+
return false
119+
}
120+
121+
for i := 0; i < len(a); i++ {
122+
if a[i] != b[i] {
123+
return false
124+
}
125+
}
126+
127+
return true
128+
}
129+
115130
func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path string) error {
116131
parts := strings.Split(path, "/")
117132
cur := root
@@ -188,7 +203,8 @@ func setupFsAndRoot(ctx context.Context, t *testing.T, rootname string) (*dagser
188203
}
189204

190205
func TestBasic(t *testing.T) {
191-
ctx := context.TODO()
206+
ctx, cancel := context.WithCancel(context.Background())
207+
defer cancel()
192208
dsp, _, rt := setupFsAndRoot(ctx, t, "test")
193209

194210
rootdir := rt.GetValue().(*Directory)
@@ -217,7 +233,8 @@ func TestBasic(t *testing.T) {
217233
}
218234

219235
func TestMkdir(t *testing.T) {
220-
ctx := context.TODO()
236+
ctx, cancel := context.WithCancel(context.Background())
237+
defer cancel()
221238
_, _, rt := setupFsAndRoot(ctx, t, "test")
222239

223240
rootdir := rt.GetValue().(*Directory)
@@ -247,16 +264,108 @@ func TestMkdir(t *testing.T) {
247264
}
248265
}
249266

250-
func compStrArrs(a, b []string) bool {
251-
if len(a) != len(b) {
252-
return false
267+
func TestFilesystemMethods(t *testing.T) {
268+
ctx, cancel := context.WithCancel(context.Background())
269+
defer cancel()
270+
dsp := getDagservAndPinner(t)
271+
272+
fs, err := NewFilesystem(ctx, dsp.ds, dsp.mp)
273+
if err != nil {
274+
t.Fatal(err)
253275
}
254276

255-
for i := 0; i < len(a); i++ {
256-
if a[i] != b[i] {
257-
return false
277+
empty := &dag.Node{Data: ft.FolderPBData()}
278+
pf := func(_ context.Context, _ key.Key) error { return nil }
279+
280+
roots := []string{"a", "b", "c", "d", "e"}
281+
sort.Strings(roots)
282+
for _, r := range roots {
283+
_, err := fs.NewRoot(r, empty, pf)
284+
if err != nil {
285+
t.Fatal(err)
258286
}
259287
}
260288

261-
return true
289+
// make sure they all show up in the listing
290+
var rootstrs []string
291+
for _, r := range fs.ListRoots() {
292+
rootstrs = append(rootstrs, r.Name)
293+
}
294+
295+
sort.Strings(rootstrs)
296+
297+
if !compStrArrs(rootstrs, roots) {
298+
t.Fatal("didnt get expected roots in fs")
299+
}
300+
301+
// make sure we can 'get' each of them
302+
for _, r := range roots {
303+
_, err := fs.GetRoot(r)
304+
if err != nil {
305+
t.Fatal(err)
306+
}
307+
}
308+
309+
// make sure we *cant* get ones that dont exist
310+
_, err = fs.GetRoot("NOTREAL")
311+
if err != ErrNotExist {
312+
t.Fatal("expected ErrNotExist, got: ", err)
313+
}
314+
315+
// make sure we cant make a root that already exists
316+
_, err = fs.NewRoot("a", empty, pf)
317+
if err == nil {
318+
t.Fatal("expected create already existing root to fail")
319+
}
320+
321+
// closing a root works
322+
final, err := fs.CloseRoot("b")
323+
if err != nil {
324+
t.Fatal(err)
325+
}
326+
327+
ek, _ := empty.Key()
328+
if final != ek {
329+
t.Fatal("final key was not as expected")
330+
}
331+
332+
// closing filesystem succeeds
333+
err = fs.Close()
334+
if err != nil {
335+
t.Fatal(err)
336+
}
337+
}
338+
339+
func TestMfsFile(t *testing.T) {
340+
ctx, cancel := context.WithCancel(context.Background())
341+
defer cancel()
342+
dsp, _, rt := setupFsAndRoot(ctx, t, "test")
343+
344+
rootdir := rt.GetValue().(*Directory)
345+
346+
nd := getRandFile(t, dsp.ds, 1000)
347+
348+
err := rootdir.AddChild("file", nd)
349+
if err != nil {
350+
t.Fatal(err)
351+
}
352+
353+
fsn, err := rootdir.Child("file")
354+
if err != nil {
355+
t.Fatal(err)
356+
}
357+
358+
fi := fsn.(*File)
359+
360+
b := []byte("THIS IS A TEST")
361+
n, err := fi.Write(b)
362+
if err != nil {
363+
t.Fatal(err)
364+
}
365+
366+
if n != len(b) {
367+
t.Fatal("didnt write correct number of bytes")
368+
}
369+
370+
// TODO: test more file stuff
262371
}

‎mfs/repub_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package mfs
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
key "github.com/ipfs/go-ipfs/blocks/key"
8+
ci "github.com/ipfs/go-ipfs/util/testutil/ci"
9+
10+
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11+
)
12+
13+
func TestRepublisher(t *testing.T) {
14+
if ci.IsRunning() {
15+
t.Skip("dont run timing tests in CI")
16+
}
17+
18+
ctx := context.TODO()
19+
20+
pub := make(chan struct{})
21+
22+
pf := func(ctx context.Context, k key.Key) error {
23+
pub <- struct{}{}
24+
return nil
25+
}
26+
27+
tshort := time.Millisecond * 50
28+
tlong := time.Second / 2
29+
30+
rp := NewRepublisher(ctx, pf, tshort, tlong)
31+
go rp.Run()
32+
33+
rp.Update("test")
34+
35+
// should hit short timeout
36+
select {
37+
case <-time.After(tshort * 2):
38+
t.Fatal("publish didnt happen in time")
39+
case <-pub:
40+
}
41+
42+
cctx, cancel := context.WithCancel(context.Background())
43+
44+
go func() {
45+
for {
46+
rp.Update("a")
47+
time.Sleep(time.Millisecond * 10)
48+
select {
49+
case <-cctx.Done():
50+
return
51+
default:
52+
}
53+
}
54+
}()
55+
56+
select {
57+
case <-pub:
58+
t.Fatal("shouldnt have received publish yet!")
59+
case <-time.After((tlong * 9) / 10):
60+
}
61+
select {
62+
case <-pub:
63+
case <-time.After(tlong / 2):
64+
t.Fatal("waited too long for pub!")
65+
}
66+
67+
cancel()
68+
69+
go func() {
70+
err := rp.Close()
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
}()
75+
76+
// final pub from closing
77+
<-pub
78+
}

‎mfs/system.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (fs *Filesystem) GetRoot(name string) (*Root, error) {
9898
if ok {
9999
return r, nil
100100
}
101-
panic("noooo")
101+
102102
return nil, ErrNotExist
103103
}
104104

@@ -154,8 +154,6 @@ type FSNode interface {
154154

155155
// Root represents the root of a filesystem tree pointed to by a given keypair
156156
type Root struct {
157-
name string
158-
159157
// node is the merkledag node pointed to by this keypair
160158
node *dag.Node
161159

@@ -173,16 +171,13 @@ type PubFunc func(context.Context, key.Key) error
173171
// newRoot creates a new Root for the given key, and starts up a republisher routine
174172
// for it
175173
func (fs *Filesystem) newRoot(parent context.Context, node *dag.Node, pf PubFunc) (*Root, error) {
176-
name := "NO NAME (WIP)"
177-
178174
ndk, err := node.Key()
179175
if err != nil {
180176
return nil, err
181177
}
182178

183179
root := new(Root)
184180
root.fs = fs
185-
root.name = name
186181

187182
root.node = node
188183

0 commit comments

Comments
 (0)
Please sign in to comment.