commit b292ad05b96626b2ae1bc70e0e926d2c93ccc9a5
parent e3315609106a109686816ddb5772eb7de2807a77
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 31 Dec 2023 11:33:57 +0900
update tests
Diffstat:
5 files changed, 64 insertions(+), 170 deletions(-)
diff --git a/export_test.go b/export_test.go
@@ -43,19 +43,21 @@ var (
type BufMsg = bufMsg
-func (s *Server) RPool() *ReqPool { return s.rPool }
-func (s *Server) FPool() *FidPool { return s.fPool }
-func (s *Server) RunListener(ctx context.Context, rp *ReqPool) {
- s.runListener(ctx, rp)
+func (s *Server) SetFS(fs FS) { s.fs = fs }
+
+type Conn = conn
+func (c *Conn) FPool() *FidPool { return c.fPool }
+func (c *Conn) RunListener(ctx context.Context, rp *ReqPool) {
+ c.runListener(ctx, rp)
}
-func (s *Server) RunResponder(ctx context.Context, rp *ReqPool) {
- s.runResponder(ctx, rp)
+func (c *Conn) RunResponder(ctx context.Context, rp *ReqPool) {
+ c.runResponder(ctx, rp)
}
-func (s *Server) SetFS(fs FS) { s.fs = fs }
-func (s *Server) SetRespChan(rc chan *Req) { s.respChan = rc }
-func (s *Server) SetFPool(fp *FidPool) { s.fPool = fp }
-func (s *Server) NewMSizeLock() { s.mSizeLock = new(sync.Mutex) }
-func (s *Server) SetMSize(size uint32) { s.setMSize(size) }
+func (c *Conn) SetServer(s *Server) { c.s = s }
+func (c *Conn) SetRespChan(rc chan *Req) { c.respChan = rc }
+func (c *Conn) SetFPool(fp *FidPool) { c.fPool = fp }
+func (c *Conn) NewMSizeLock() { c.mSizeLock = new(sync.Mutex) }
+func (c *Conn) SetMSize(size uint32) { c.setMSize(size) }
type Req = request
diff --git a/server.go b/server.go
@@ -50,7 +50,7 @@ type Server struct {
// It reads incoming messages from r and writes responses to w.
func NewServer(fsys FS) *Server {
s := &Server{
- fs: fsys,
+ fs: fsys,
}
return s
}
@@ -84,13 +84,14 @@ type conn struct {
w io.Writer
}
-func newConn(r io.Reader, w io.Writer) *conn {
+func (s *Server) newConn(r io.Reader, w io.Writer) *conn {
return &conn{
- msize: 8*1024,
+ s: s,
+ msize: 8 * 1024,
mSizeLock: new(sync.Mutex),
- fPool: newFidPool(),
- r: r,
- w: w,
+ fPool: newFidPool(),
+ r: r,
+ w: w,
}
}
@@ -1157,7 +1158,7 @@ func sWStat(ctx context.Context, c *conn, rc <-chan *request) {
// And Serve method should be attached to that struct.
func (s *Server) Serve(ctx context.Context, r io.Reader, w io.Writer) {
rp := newReqPool()
- c := newConn(r, w)
+ c := s.newConn(r, w)
c.runListener(ctx, rp)
c.runResponder(ctx, rp)
var (
diff --git a/server2_test.go b/server2_test.go
@@ -26,7 +26,8 @@ func TestRunListener(t *testing.T) {
t.Fatalf("open file: %v", err)
}
defer tFile2.Close()
- s := &Server{
+ c := &Conn{
+ s: &Server{chatty9P: false},
r: tFile,
}
oldReqPoolAdd := reqPoolAdd
@@ -34,7 +35,7 @@ func TestRunListener(t *testing.T) {
reqPoolAdd = func(*reqPool, uint16) (*request, error) { return &request{}, nil }
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- s.runListener(ctx, newReqPool())
+ c.runListener(ctx, newReqPool())
for {
want, err := RecvMsg(tFile2)
if err == io.EOF {
@@ -42,7 +43,7 @@ func TestRunListener(t *testing.T) {
} else if err != nil {
t.Fatalf("recvmsg: %v", err)
}
- r := <-s.listenChan
+ r := <-c.listenChan
if r.listenErr != nil {
t.Fatalf("listenErr: %v", r.listenErr)
}
@@ -63,11 +64,11 @@ func TestRunResponder(t *testing.T) {
}
defer rFile.Close()
r, w := io.Pipe()
- s := &Server{w: w}
+ c := &Conn{s: &Server{chatty9P: false}, w: w}
rp := newReqPool()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- s.runResponder(ctx, rp)
+ c.runResponder(ctx, rp)
for {
want, err := readMsg(rFile)
if err == io.EOF {
@@ -79,7 +80,7 @@ func TestRunResponder(t *testing.T) {
if err != nil {
t.Fatalf("unmarshal %v", err)
}
- s.respChan <- &request{
+ c.respChan <- &request{
tag: msg.GetTag(),
ofcall: msg,
}
@@ -106,9 +107,9 @@ func TestGetReq(t *testing.T) {
t.Fatalf("open file: %v", err)
}
defer tFile2.Close()
- s := &Server{r: tFile, rPool: newReqPool()}
+ rp := newReqPool()
for {
- got := getReq(s.r, s.rPool, s.chatty9P)
+ got := getReq(tFile, rp, false)
if got.listenErr == io.EOF {
break
} else if got.listenErr != nil {
@@ -124,14 +125,14 @@ func TestGetReq(t *testing.T) {
if !reflect.DeepEqual(got.ifcall, wantMsg) {
t.Errorf("r.ifcall:\n\twant: %v,\n\tgot: %v", wantMsg, got.ifcall)
}
- got2, ok := s.rPool.lookup(wantMsg.GetTag())
+ got2, ok := rp.lookup(wantMsg.GetTag())
if !ok {
t.Errorf("request not registered to the pool")
}
if got != got2 {
t.Errorf("wrong message in pool:\n\twant: %p,\n\tgot: %p", got, got2)
}
- s.rPool.delete(wantMsg.GetTag())
+ rp.delete(wantMsg.GetTag())
}
}
@@ -151,12 +152,12 @@ func TestSVersion(t *testing.T) {
}
tc := make(chan *request)
rc := make(chan *request)
- s := &Server{msize: 1024, mSizeLock: new(sync.Mutex), respChan: rc}
+ c := &Conn{msize: 1024, mSizeLock: new(sync.Mutex), respChan: rc}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go sVersion(ctx, s, tc)
+ go sVersion(ctx, c, tc)
for _, test := range tests {
- oldMsize := s.msize
+ oldMsize := c.msize
tc <- test.input
ifcall := test.input.ifcall.(*TVersion)
wantmsg := test.want.ofcall.(*RVersion)
@@ -164,13 +165,13 @@ func TestSVersion(t *testing.T) {
if !reflect.DeepEqual(wantmsg, gotmsg) {
t.Errorf("want: %v,\n\tgot: %v", wantmsg, gotmsg)
}
- if ifcall.Msize < oldMsize && s.msize != ifcall.Msize {
+ if ifcall.Msize < oldMsize && c.msize != ifcall.Msize {
t.Errorf("msize not changed")
}
- if ifcall.Msize >= oldMsize && s.msize != oldMsize {
+ if ifcall.Msize >= oldMsize && c.msize != oldMsize {
t.Errorf("msize changed unexpectedly")
}
- s.msize = oldMsize
+ c.msize = oldMsize
}
}
@@ -197,10 +198,11 @@ func TestSAuth(t *testing.T) {
rc := make(chan *request)
defer close(tc)
defer close(rc)
- s := &Server{respChan: rc, Auth: test.authFunc, fPool: newFidPool()}
+ s := &Server{Auth: test.authFunc}
+ c := &Conn{s: s, respChan: rc, fPool: newFidPool()}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go sAuth(ctx, s, tc)
+ go sAuth(ctx, c, tc)
tc <- test.input
ofcall := (<-rc).ofcall
switch wantmsg := test.want.ofcall.(type) {
@@ -237,10 +239,10 @@ func TestSFlush(t *testing.T) {
}
tc := make(chan *request)
rc := make(chan *request)
- s := &Server{msize: 1024, mSizeLock: new(sync.Mutex), respChan: rc}
+ c := &Conn{msize: 1024, mSizeLock: new(sync.Mutex), respChan: rc}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go sFlush(ctx, s, tc)
+ go sFlush(ctx, c, tc)
for _, test := range tests {
tc <- test.input
if gotmsg, ok := (<-rc).ofcall.(*RFlush); !ok {
diff --git a/server_test.go b/server_test.go
@@ -3,7 +3,6 @@ package lib9p_test
import (
"context"
"errors"
- "io"
"path"
"strings"
"testing"
@@ -56,11 +55,13 @@ func TestSAttach(t *testing.T) {
defer close(rc)
s := &lib9p.Server{}
s.SetFS(testfs.Fsys)
- s.SetRespChan(rc)
- s.SetFPool(fp)
+ c := &lib9p.Conn{}
+ c.SetServer(s)
+ c.SetRespChan(rc)
+ c.SetFPool(fp)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go lib9p.SAttach(ctx, s, tc)
+ go lib9p.SAttach(ctx, c, tc)
for i, test := range tests {
af.AuthOK = test.authOK
if test.auth {
@@ -124,11 +125,13 @@ func TestSWalk(t *testing.T) {
fid.SetPath(".")
s := &lib9p.Server{}
s.SetFS(testfs.Fsys)
- s.SetRespChan(rc)
- s.SetFPool(fp)
+ c := &lib9p.Conn{}
+ c.SetServer(s)
+ c.SetRespChan(rc)
+ c.SetFPool(fp)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go lib9p.SWalk(ctx, s, tc)
+ go lib9p.SWalk(ctx, c, tc)
for i, test := range tests {
req := new(lib9p.Req)
req.SetIfcall(test.input)
@@ -181,13 +184,15 @@ func TestSOpen(t *testing.T) {
fp := lib9p.NewFidPool()
s := &lib9p.Server{}
s.SetFS(testfs.Fsys)
- s.SetRespChan(rc)
- s.SetFPool(fp)
- s.NewMSizeLock()
- s.SetMSize(1024)
+ c := &lib9p.Conn{}
+ c.SetServer(s)
+ c.SetRespChan(rc)
+ c.SetFPool(fp)
+ c.NewMSizeLock()
+ c.SetMSize(1024)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
- go lib9p.SOpen(ctx, s, tc)
+ go lib9p.SOpen(ctx, c, tc)
for i, test := range tests {
fp.Delete(0)
fid, err := fp.Add(0)
@@ -245,119 +250,3 @@ func testWalk(t *testing.T, fs *testfs.FS, pathname string, file *testfs.File) {
func TestWalk(t *testing.T) {
testWalk(t, testfs.Fsys, ".", testfs.Fsys.Root)
}
-
-// TODO: I don't know what this test acturally tests.
-func TestServer(t *testing.T) {
- sr, cw := io.Pipe()
- defer sr.Close()
- defer cw.Close()
- cr, sw := io.Pipe()
- defer cr.Close()
- defer sw.Close()
- msg := []lib9p.Msg{
- &lib9p.TVersion{
- Tag: lib9p.NOTAG,
- Msize: 1024,
- Version: "9P2000",
- },
- &lib9p.TAttach{
- Tag: 0,
- Fid: 0,
- Afid: lib9p.NOFID,
- Uname: "glenda",
- Aname: "",
- },
- &lib9p.TStat{
- Tag: 0,
- Fid: 0,
- },
- &lib9p.TWalk{
- Tag: 0,
- Fid: 0,
- Newfid: 1,
- Wnames: []string{},
- },
- &lib9p.TOpen{
- Tag: 0,
- Fid: 1,
- Mode: lib9p.OREAD,
- },
- &lib9p.TRead{
- Tag: 0,
- Fid: 1,
- Offset: 0,
- Count: 1024,
- },
- &lib9p.TClunk{
- Fid: 1,
- },
- &lib9p.TWalk{
- Tag: 0,
- Fid: 0,
- Newfid: 1,
- Wnames: []string{"dir", "file"},
- },
- &lib9p.TOpen{
- Tag: 0,
- Fid: 1,
- Mode: lib9p.ORDWR,
- },
- &lib9p.TRead{
- Tag: 0,
- Fid: 1,
- Offset: 0,
- Count: 1024,
- },
- &lib9p.TWrite{
- Tag: 0,
- Fid: 1,
- Offset: 2,
- Count: 1,
- Data: []byte("a"),
- },
- &lib9p.TRead{
- Tag: 0,
- Fid: 1,
- Offset: 0,
- Count: 1024,
- },
- }
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- s := lib9p.NewServer(testfs.Fsys, 1024, sr, sw)
- go s.Serve(ctx)
- for _, m := range msg {
- buf := lib9p.MarshalMsg(m)
- _, err := cw.Write(buf)
- if err != nil {
- t.Fatalf("write: %v", err)
- }
- buf = make([]byte, 1024)
- _, err = cr.Read(buf)
- if err != nil {
- t.Fatalf("read: %v", err)
- }
- if lib9p.BufMsg(buf).Type() == lib9p.Rread {
- rread := lib9p.NewRRead(buf)
- data := rread.Data
- fid, ok := s.FPool().Lookup(m.(*lib9p.TRead).Fid)
- if !ok {
- t.Fatalf("lookup fid %d", m.(*lib9p.TRead).Fid)
- }
- st, err := fid.File().Stat()
- if err != nil {
- t.Errorf("stat: %v", err)
- continue
- }
- if st.Sys().(*lib9p.Stat).Qid.Type&lib9p.QTDIR != 0 {
- for i := 0; i < len(data); {
- stat := lib9p.NewStat(data[i:])
- t.Logf("stat: %v", stat)
- i += int(stat.Size()) + 2
- }
- } else {
- t.Logf("content: %s", string(data))
- }
- }
- }
-}
diff --git a/testfs/conn.go b/testfs/conn.go
@@ -26,13 +26,13 @@ func SetupConn() *Conn {
cr, sw := io.Pipe()
sr, cw := io.Pipe()
// TODO: fix the inconsistency of server and client api.
- s := lib9p.NewServer(Fsys, mSize, sr, sw)
- c := client.NewClient(mSize, uname, cr, cw)
+ s := lib9p.NewServer(Fsys)
+ clnt := client.NewClient(mSize, uname, cr, cw)
ctx, cancel := context.WithCancel(context.Background())
- go s.Serve(ctx)
+ go s.Serve(ctx, sr, sw)
return &Conn{
S: s,
- C: c,
+ C: clnt,
cr: cr,
cw: cw,
sr: sr,