lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 622d67e09516d3ec534d8bffb4ce810a06c54a8e
parent d4e21b1fff6be55635313efe2db1f8df38d792e6
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat,  6 Jan 2024 08:43:47 +0900

delete tc from setupConn

Diffstat:
Mauth_test.go | 2+-
Mfid.go | 23+++++++++++------------
Mfile_test.go | 3++-
Mfs_test.go | 15+++++++++++++++
Mreq_test.go | 2+-
Mserver_test.go | 55+++++++++++++++++++++++++++----------------------------
6 files changed, 57 insertions(+), 43 deletions(-)

diff --git a/auth_test.go b/auth_test.go @@ -7,7 +7,7 @@ import ( ) func TestAuth(t *testing.T) { - c, _, rc := setupConn(testfs) + c, rc := setupConn(testfs) atc, otc, rtc, wtc := make(chan *request), make(chan *request), make(chan *request), make(chan *request) ctx, cancel := context.WithCancel(context.Background()) diff --git a/fid.go b/fid.go @@ -22,11 +22,11 @@ const ( // An fid represents an fid defined by 9P. // It is used as the identifier of a file in 9P session. -// It is specified by the client and the server connect it to a file in the +// It is specified by the client and the server connects it to a file in the // file system. // Multiple fids can be connected to a single file. // On the other hand, a Qid is the Identifier in the file system itself, -// and not change between 9P sessions. +// and does not change between 9P sessions. type fid struct { fid uint32 omode OpenMode /* -1 = not open */ @@ -48,7 +48,6 @@ func newFid(id uint32) *fid { } } -// not safe for concurrent use. func (f *fid) String() string { fid := int64(f.fid) if uint32(fid) == NOFID { @@ -62,27 +61,27 @@ func (f *fid) String() string { // It is safe for concurrent use by multiple goroutines except the String method. type fidPool struct { m map[uint32]*fid - lock *sync.Mutex + *sync.Mutex } // newFidPool allocates an fidPool. func newFidPool() *fidPool { return &fidPool{ - m: make(map[uint32]*fid), - lock: new(sync.Mutex), + make(map[uint32]*fid), + new(sync.Mutex), } } func (pool *fidPool) lookup(id uint32) (*fid, bool) { - pool.lock.Lock() - defer pool.lock.Unlock() + pool.Lock() + defer pool.Unlock() f, ok := pool.m[id] return f, ok } func (pool *fidPool) add(id uint32) (*fid, error) { - pool.lock.Lock() - defer pool.lock.Unlock() + pool.Lock() + defer pool.Unlock() if _, ok := pool.m[id]; ok { return nil, fmt.Errorf("fid already in use.") } @@ -92,8 +91,8 @@ func (pool *fidPool) add(id uint32) (*fid, error) { } func (pool *fidPool) delete(id uint32) { - pool.lock.Lock() - defer pool.lock.Unlock() + pool.Lock() + defer pool.Unlock() delete(pool.m, id) } diff --git a/file_test.go b/file_test.go @@ -6,7 +6,8 @@ import ( ) func BenchmarkRead(b *testing.B) { - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sRead(ctx, c, tc) diff --git a/fs_test.go b/fs_test.go @@ -6,12 +6,27 @@ import ( "io/fs" "path" "strings" + "sync" "testing" "time" "git.mtkn.jp/lib9p/testdata" ) +// SetupConn sets up a conn struct to serve fs. +// rc is the channel from which reply *requests are derived. +func setupConn(fs FS) (c *conn, rc chan *request) { + rc = make(chan *request) + c = &conn{ + s: NewServer(fs), + msize: 1024, + mSizeLock: new(sync.Mutex), + fPool: newFidPool(), + respChan: rc, + } + return +} + type testFile struct { // fsys is the testFS this testFile belongs to. fsys *testFS diff --git a/req_test.go b/req_test.go @@ -19,7 +19,7 @@ func TestFlush(t *testing.T) { ) testfs.slow = true defer func() { testfs.slow = false }() - c, _, rc := setupConn(testfs) + c, rc := setupConn(testfs) rtc, wtc, ftc := make(chan *request), make(chan *request), make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/server_test.go b/server_test.go @@ -9,7 +9,6 @@ import ( "os" "path" "reflect" - "sync" "testing" ) @@ -138,19 +137,6 @@ func TestGetReq(t *testing.T) { } } -func setupConn(fs FS) (c *conn, tc, rc chan *request) { - tc = make(chan *request) - rc = make(chan *request) - c = &conn{ - s: NewServer(fs), - msize: 1024, - mSizeLock: new(sync.Mutex), - fPool: newFidPool(), - respChan: rc, - } - return -} - func TestSVersion(t *testing.T) { tests := []struct { input *request @@ -165,7 +151,8 @@ func TestSVersion(t *testing.T) { {&request{ifcall: &TVersion{Msize: 1024, Version: "unko"}}, &request{ofcall: &RVersion{Msize: 1024, Version: "unknown"}}}, } - c, tc, rc := setupConn(nil) + c, rc := setupConn(nil) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sVersion(ctx, c, tc) @@ -207,8 +194,9 @@ func TestSAuth(t *testing.T) { } for _, test := range tests { func() { - c, tc, rc := setupConn(nil) - c.s.Auth = test.authFunc + c, rc := setupConn(nil) + tc := make(chan *request) + c.s.Auth = test.authFunc ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sAuth(ctx, c, tc) @@ -246,7 +234,8 @@ func TestSFlush(t *testing.T) { {&request{ifcall: &TFlush{}, oldreq: &request{pool: rp, done: make(chan struct{})}}}, } - c, tc, rc := setupConn(nil) + c, rc := setupConn(nil) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sFlush(ctx, c, tc) @@ -287,7 +276,8 @@ func TestSAttach(t *testing.T) { true, true, &RAttach{}}, // ok } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) dammyAuth := func(context.Context, *request) {} af := &AuthFile{ Qid: Qid{Type: QTAUTH}, @@ -351,7 +341,8 @@ func TestSWalk(t *testing.T) { {&TWalk{Fid: 0, Newfid: 5, Wnames: []string{"unko", "unko"}}, 0, errors.New("not found")}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) fid, err := c.fPool.add(0) if err != nil { t.Fatal(err) @@ -405,7 +396,8 @@ func TestSOpen(t *testing.T) { // is a directory {"dir", "glenda", &TOpen{Fid: 0, Mode: OWRITE}, &RError{}}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sOpen(ctx, c, tc) @@ -459,7 +451,8 @@ func TestSCreate(t *testing.T) { &TCreate{Name: "test", Perm: 0777, Mode: OREAD}, &RCreate{}}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sCreate(ctx, c, tc) @@ -526,7 +519,8 @@ func TestSCreate(t *testing.T) { } func TestSRead(t *testing.T) { - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sRead(ctx, c, tc) @@ -579,7 +573,8 @@ func TestSWrite(t *testing.T) { }{ {"c", []byte("unko")}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sWrite(ctx, c, tc) @@ -629,7 +624,8 @@ func TestSClunk(t *testing.T) { {0, &TClunk{Fid: 1}, &RError{}}, {1, &TClunk{Fid: 1}, &RClunk{}}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sClunk(ctx, c, tc) @@ -694,7 +690,8 @@ func TestSRemove(t *testing.T) { // permission denied. {0, "dir/unko", "glenda", OREAD, 0777, "glenda", &TRemove{Fid: 0}, &RError{}}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sRemove(ctx, c, tc) @@ -744,7 +741,8 @@ func TestSRemove(t *testing.T) { } func TestSStat(t *testing.T) { - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sStat(ctx, c, tc) @@ -915,7 +913,8 @@ func TestSWstat(t *testing.T) { Stat: &Stat{Gid: "glenda", Mode: 0755}, }, &RWstat{}}, } - c, tc, rc := setupConn(testfs) + c, rc := setupConn(testfs) + tc := make(chan *request) ctx, cancel := context.WithCancel(context.Background()) defer cancel() go sWStat(ctx, c, tc)