commit 2212645c2192d583d93a5ad9b12ce48d4b278475
parent 44459189bd0cc96547ebcd3cddba7b4f499e05c9
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 21 Oct 2023 06:57:00 +0900
add req_test
Diffstat:
4 files changed, 61 insertions(+), 11 deletions(-)
diff --git a/client.go b/client.go
@@ -46,7 +46,8 @@ func NewClient(mSize uint32, uname string, r io.Reader, w io.Writer) *Client {
func (c *Client) Stop() {
// TODO: fPool, rPool
c.cancel()
- close(c.errc)
+ // TODO: check all goroutines are stopped.
+ // close(c.errc)
}
func (c *Client) mSize() uint32 {
diff --git a/req_test.go b/req_test.go
@@ -0,0 +1,54 @@
+package lib9p
+
+import (
+ "context"
+ "io"
+ "testing"
+)
+
+func TestFlush(t *testing.T) {
+ const (
+ mSize = 1024
+ uname = "kenji"
+ )
+ fsys.slow = true
+ defer func() { fsys.slow = false }()
+ sr, cw := io.Pipe()
+ defer sr.Close()
+ defer cw.Close()
+ cr, sw := io.Pipe()
+ defer cr.Close()
+ defer sw.Close()
+ s := NewServer(fsys, mSize, sr, sw)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ go s.Serve(ctx)
+ c := NewClient(mSize, uname, cr, cw)
+ defer c.Stop()
+ rmsize, rversion, err := c.Version(ctx, mSize, "9P2000")
+ if err != nil {
+ t.Fatalf("version: %v", err)
+ }
+ t.Logf("rversion: %d, %s", rmsize, rversion)
+ _, err = c.Attach(ctx, 0, NOFID, uname, "")
+ if err != nil {
+ t.Fatalf("attach: %v", err)
+ }
+ _, err = c.Walk(ctx, 0, 1, []string{"a"})
+ if err != nil {
+ t.Fatalf("walk: %v", err)
+ }
+ _, _, err = c.Open(ctx, 1, OREAD)
+ if err != nil {
+ t.Fatalf("open: %v", err)
+ }
+ ctx1, cancel1 := context.WithCancel(ctx)
+ done := make(chan string)
+ go func() {
+ data, err := c.Read(ctx1, 1, 0, mSize-IOHDRSZ)
+ t.Logf("read data: %v, err: %v", data, err)
+ done <- "read goroutine returned"
+ }()
+ cancel1()
+ t.Logf("canceled: %v", <-done)
+}
diff --git a/server_test.go b/server_test.go
@@ -74,8 +74,12 @@ func TestWalk(t *testing.T) {
}
func TestServer(t *testing.T) {
- sr, _ := io.Pipe()
+ sr, cw := io.Pipe()
+ defer sr.Close()
+ defer cw.Close()
cr, sw := io.Pipe()
+ defer cr.Close()
+ defer sw.Close()
msg := []Msg{
&TVersion{
tag: NOTAG,
diff --git a/test_fs.go b/test_fs.go
@@ -22,9 +22,6 @@ type testFile struct {
}
func (f *testFile) Stat() (*FileInfo, error) {
- if f.fsys.slow {
- time.Sleep(sleepTime)
- }
return &FileInfo{Stat: f.stat}, nil
}
func (f *testFile) Close() error {
@@ -47,9 +44,6 @@ func (f *testFile) ReadAt(b []byte, off int64) (n int, err error) {
}
func (f *testFile) ReadDir(n int) ([]*DirEntry, error) {
- if f.fsys.slow {
- time.Sleep(sleepTime)
- }
de := make([]*DirEntry, len(f.children))
for i, c := range f.children {
de[i], _ = c.Stat()
@@ -84,9 +78,6 @@ type testFS struct {
}
func (fs *testFS) OpenFile(path string, omode OpenMode, perm fs.FileMode) (File, error) {
- if fs.slow {
- time.Sleep(sleepTime)
- }
path = clean9path(path)
wnames := split9path(path)
f, err := fs.walk(wnames)