commit c0c3c839283b1e086e8eba03d07f9c2191e5f657
parent 46d6acafd1f08e02443ae59769f66b790ed2c030
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 13 Jan 2024 15:48:05 +0900
refactor tests
Diffstat:
1 file changed, 18 insertions(+), 17 deletions(-)
diff --git a/server_test.go b/server_test.go
@@ -324,13 +324,13 @@ func TestSFlush(t *testing.T) {
 	ctx, cancel := context.WithCancel(context.Background())
 	defer cancel()
 	go sFlush(ctx, c, tc)
-	for _, test := range tests {
+	for i, test := range tests {
 		tc <- test.input
 		if gotmsg, ok := (<-rc).ofcall.(*RFlush); !ok {
-			t.Errorf("unexpected message: %v", gotmsg)
+			t.Errorf("%d: unexpected message: %v", i, gotmsg)
 		}
 		if _, ok := <-test.input.oldreq.done; ok {
-			t.Errorf("done channel not closed")
+			t.Errorf("%d: done channel not closed", i)
 		}
 	}
 }
@@ -408,7 +408,7 @@ func TestSOpen(t *testing.T) {
 		{"b", "glenda", &TOpen{Fid: 0, Mode: OREAD}, &RError{}},
 		{"b", "ken", &TOpen{Fid: 0, Mode: ORDWR}, &ROpen{}},
 		{"dir", "glenda", &TOpen{Fid: 0, Mode: OREAD}, &ROpen{}},
-		// is a directory
+		// A directory can't be opened with OWRITE.
 		{"dir", "glenda", &TOpen{Fid: 0, Mode: OWRITE}, &RError{}},
 	}
 	c, rc := setupConn(testfs)
@@ -418,6 +418,7 @@ func TestSOpen(t *testing.T) {
 	go sOpen(ctx, c, tc)
 	for i, test := range tests {
 		c.fPool.delete(0)
+		// add the file to open to c.fPool.
 		fid, err := c.fPool.add(0)
 		if err != nil {
 			t.Error(i, err)
@@ -616,10 +617,12 @@ func testSRead(t *testing.T, pathname string, c *conn, tc, rc chan *request) {
 
 func TestSWrite(t *testing.T) {
 	tests := []struct {
-		pathname string
+		omode    OpenMode
 		data     []byte
+		err      error
 	}{
-		{"c", []byte("hoge")},
+		{ORDWR, []byte("hoge"), nil},
+		{OREAD, []byte("hoge"), errors.New("omode")},
 	}
 	c, rc := setupConn(testfs)
 	tc := make(chan *request)
@@ -634,30 +637,28 @@ func TestSWrite(t *testing.T) {
 				t.Error(err)
 				return
 			}
-			fid.omode = ORDWR
-			fid.path = test.pathname
-			ff, err := testfs.OpenFile(test.pathname, ORDWR)
-			if err != nil {
-				t.Error(err)
-				return
-			}
-			defer ff.Close()
-			f := ff.(*testFile)
+			fid.omode = test.omode
+			f := &testFile{fsys: testfs}
 			fid.file = f
 			tc <- &request{ifcall: &TWrite{Count: uint32(len(test.data)), Data: test.data}}
 			ofcall := (<-rc).ofcall
 			switch ofcall := ofcall.(type) {
 			case *RWrite:
+				if test.err != nil {
+					t.Errorf("%d: unexpected message: %v", i, ofcall)
+					return
+				}
 				if !bytes.Equal(f.content, test.data) {
 					t.Errorf("%d: content mismatch: want: %v, got: %v",
 						i, test.data, f.content)
 				}
 			case *RError:
-				t.Error(ofcall.Ename)
+				if test.err == nil {
+					t.Errorf("%d: unexpected err: %v", i, ofcall.Ename)
+				}
 			default:
 				t.Errorf("%d: unexpected message: %v", i, ofcall)
 			}
-			f.content = []byte("")
 		}()
 	}
 }