func TestPendingConnection(t *testing.T) { a1, b1, _ := fuse.Socketpair("unix") a2, b2, _ := fuse.Socketpair("unix") conn1 := &DummyConn{b1} conn2 := &DummyConn{b2} id1 := "pqrxyzab" id2 := "pqrxy111" io.WriteString(a1, id1) io.WriteString(a2, id2) pc := NewPendingConnections() out := make(chan net.Conn) go func() { out <- pc.WaitConnection(id1) }() go pc.Accept(conn1) go pc.Accept(conn2) io.WriteString(a1, "hello") other1 := <-out b := make([]byte, 100) n, err := other1.Read(b) if string(b[:n]) != "hello" || err != nil { t.Error("unexpected", string(b[:n]), err) } other2 := pc.WaitConnection(id2) io.WriteString(a2, "hallo") b = make([]byte, 100) n, err = other2.Read(b) if string(b[:n]) != "hallo" || err != nil { t.Error("unexpected", string(b[:n]), err) } }
func TestRpcFS(t *testing.T) { tmp, _ := ioutil.TempDir("", "") defer os.RemoveAll(tmp) mnt := tmp + "/mnt" orig := tmp + "/orig" srvCache := tmp + "/server-cache" clientCache := tmp + "/client-cache" os.Mkdir(mnt, 0700) os.Mkdir(orig, 0700) os.Mkdir(orig+"/subdir", 0700) content := "hello" err := ioutil.WriteFile(orig+"/file.txt", []byte(content), 0644) if err != nil { t.Fatal(err) } cache := NewContentCache(srvCache) server := NewFsServer(orig, cache, []string{}) l, r, err := fuse.Socketpair("unix") if err != nil { t.Fatal(err) } defer l.Close() defer r.Close() rpcServer := rpc.NewServer() rpcServer.Register(server) go rpcServer.ServeConn(l) rpcClient := rpc.NewClient(r) fs := NewRpcFs(rpcClient, NewContentCache(clientCache)) state, _, err := fuse.MountPathFileSystem(mnt, fs, nil) state.Debug = true if err != nil { t.Fatal("Mount", err) } defer func() { log.Println("unmounting") err := state.Unmount() if err == nil { os.RemoveAll(tmp) } }() go state.Loop(false) fi, err := os.Lstat(mnt + "/subdir") if fi == nil || !fi.IsDirectory() { t.Fatal("subdir stat", fi, err) } c, err := ioutil.ReadFile(mnt + "/file.txt") if err != nil || string(c) != "hello" { t.Error("Readfile", c) } entries, err := ioutil.ReadDir(mnt) if err != nil || len(entries) != 2 { t.Error("Readdir", err, entries) } // This test implementation detail - should be separate? storedHash := server.hashCache["/file.txt"] if storedHash == "" || string(storedHash) != string(md5str(content)) { t.Errorf("cache error %x (%v)", storedHash, storedHash) } newData := []FileAttr{ FileAttr{ Path: "/file.txt", Hash: md5str("somethingelse"), }, } server.updateFiles(newData) storedHash = server.hashCache["/file.txt"] if storedHash == "" || storedHash != newData[0].Hash { t.Errorf("cache error %x (%v)", storedHash, storedHash) } }