commit dafe3386aeedd740d277daf8204e6d66d63ce210
parent 6c2177e6c470a56fe374d5b7ea445921f96b99c3
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 3 Jan 2024 15:58:44 +0900
change req_test.go from lib9p_test to lib9p
Diffstat:
M | req_test.go | | | 126 | ++++++++++++++++++++++++++++++------------------------------------------------- |
1 file changed, 47 insertions(+), 79 deletions(-)
diff --git a/req_test.go b/req_test.go
@@ -1,95 +1,63 @@
-package lib9p_test
+package lib9p
import (
"context"
"testing"
-
- "git.mtkn.jp/lib9p"
- "git.mtkn.jp/lib9p/testfs"
)
// TestFlush tests if Tflush message can cancel the time-consuming Tread.
-// It sets the testfs.Fsys slow, sends a Tread message which blocks until
-// something is sent over Fsys.Waitc, and sends a Tflush message, waits for
+// It sets the testfs slow, sends a Tread message which blocks until
+// something is sent over testfs.waitc, and sends a Tflush message, waits for
// the Rflush, then unblock the pending Tread message by sending something
-// through the Fsys.Waitc.
-// And it checks if the response for Tread message is an error.
-// If the error is nil, Tflush is not successful. If not, Tflush is ok.
+// through the testfs.waitc.
+// It checks if the req.done channel is closed.
// It also tests same thing for a Twrite request.
func TestFlush(t *testing.T) {
const (
mSize = 8 * 1024
uname = "kenji"
)
- testfs.Fsys.Slow = true
- defer func() { testfs.Fsys.Slow = false }()
- conn := testfs.SetupConn()
- defer conn.Close()
- ctx := context.Background()
- _, _, err := conn.C.Version(ctx, lib9p.NOTAG, mSize, "9P2000")
- if err != nil {
- t.Fatalf("version: %v", err)
- }
- _, err = conn.C.Attach(ctx, 0, 0, lib9p.NOFID, uname, "")
- if err != nil {
- t.Fatalf("attach: %v", err)
- }
- _, err = conn.C.Walk(ctx, 0, 0, 1, []string{"a"})
- if err != nil {
- t.Fatalf("walk: %v", err)
- }
- _, _, err = conn.C.Open(ctx, 0, 1, lib9p.OREAD)
- if err != nil {
- t.Fatalf("open: %v", err)
- }
- done := make(chan struct{})
- ctx1, cancel1 := context.WithCancel(ctx)
- go func() {
- _, err = conn.C.Read(ctx1, 0, 1, 0, mSize-lib9p.IOHDRSZ)
- close(done)
- }()
- <-testfs.Fsys.Waitc
- err = conn.C.Flush(ctx, 1, 0)
- if err != nil {
- t.Errorf("flush: %v", err)
- }
- testfs.Fsys.Waitc <- struct{}{}
- cancel1()
- <-done
- if err == nil { // this err is from Read.
- t.Errorf("Rread arrived after Rflush.")
- }
- err = conn.C.Clunk(ctx, 0, 1)
- if err != nil {
- t.Fatalf("clunk: %v", err)
- }
- wname := []string{"dir", "file"}
- wqid, err := conn.C.Walk(ctx, 0, 0, 1, wname)
- if err != nil {
- t.Fatalf("walk: %v", err)
- } else if len(wqid) != len(wname) {
- t.Fatalf("walk: not found")
- }
- _, _, err = conn.C.Open(ctx, 0, 1, lib9p.OWRITE)
- if err != nil {
- t.Fatalf("open: %v", err)
- }
- done = make(chan struct{})
- ctx1, cancel1 = context.WithCancel(ctx)
- go func() {
- data := []byte("unko")
- _, err = conn.C.Write(ctx1, 0, 1, 0, uint32(len(data)), data)
- close(done)
- }()
- <-testfs.Fsys.Waitc
- err = conn.C.Flush(ctx, 1, 0)
- if err != nil {
- t.Errorf("flush: %v", err)
- }
- testfs.Fsys.Waitc <- struct{}{}
- cancel1()
- <-done
- if err == nil { // this err is from Read.
- t.Errorf("Rread arrived after Rflush.")
+ testfs.slow = true
+ defer func() { testfs.slow = false }()
+ c, _, rc := setupConn(testfs)
+ rtc, wtc, ftc := make(chan *request), make(chan *request), make(chan *request)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ go sRead(ctx, c, rtc)
+ go sWrite(ctx, c, wtc)
+ go sFlush(ctx, c, ftc)
+ fid, err := c.fPool.add(0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ fid.file, err = testfs.OpenFile("dir/file", OREAD)
+ fid.path = "dir/file"
+ fid.omode = ORDWR
+ req := &request{
+ ifcall: &TRead{Count: mSize-IOHDRSZ},
+ done: make(chan struct{}),
+ pool: newReqPool(),
+ }
+ rtc <- req
+ <-testfs.waitc
+ ftc <- &request{ifcall: &TFlush{Tag: 1, Oldtag: 0}, oldreq: req}
+ <-rc
+ testfs.waitc <- struct{}{} // TODO: race?
+ if _, ok := <- req.done; ok {
+ t.Error("req not flushed.")
+ }
+ data := []byte("data")
+ req = &request{
+ ifcall: &TWrite{Count: uint32(len(data)), Data: data},
+ done: make(chan struct{}),
+ pool: newReqPool(),
+ }
+ wtc <- req
+ <-testfs.waitc
+ ftc <- &request{ifcall: &TFlush{Tag: 1, Oldtag: 0}, oldreq: req}
+ <-rc
+ testfs.waitc <- struct{}{} // TODO: race?
+ if _, ok := <- req.done; ok {
+ t.Error("req not flushed.")
}
}