commit 93dbba4dab189b6ce365bd5f19a544b6de3e97ad
parent f52fb3b604d476aa1237ff6928832315a53501e2
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 23 Dec 2023 11:30:24 +0900
use SetupConn in client test
Diffstat:
3 files changed, 149 insertions(+), 0 deletions(-)
diff --git a/client/client2_test.go b/client/client2_test.go
@@ -0,0 +1,94 @@
+package client
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "git.mtkn.jp/lib9p"
+)
+
+const (
+ mSize = 8192
+ uname = "kenji"
+)
+
+func newClientForTest(msize uint32, uname string, tmsgc chan<- lib9p.Msg, rmsgc <-chan lib9p.Msg) *Client {
+ ctx, cancel := context.WithCancel(context.Background())
+ c := &Client{
+ msize: mSize,
+ mSizeLock: new(sync.Mutex),
+ uname: uname,
+ fPool: allocClientFidPool(),
+ errc: make(chan error),
+ cancel: cancel,
+ wg: new(sync.WaitGroup),
+ }
+ c.txc = c.runMultiplexer(ctx, tmsgc, rmsgc)
+ return c
+}
+
+func TestClientVersion(t *testing.T) {
+ tests := []struct {
+ name string
+ mSize uint32
+ version string
+ rmsg lib9p.Msg
+ wantmsize uint32
+ wantversion string
+ }{
+ {"0", mSize, "9P2000",
+ &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "9P2000"},
+ mSize, "9P2000"},
+ {"1", mSize, "unko",
+ &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "unknown"},
+ mSize, "unknown"},
+ }
+ for _, test := range tests {
+ func() {
+ tmsgc := make(chan lib9p.Msg)
+ rmsgc := make(chan lib9p.Msg)
+ c := newClientForTest(mSize, uname, tmsgc, rmsgc)
+ tPool := newTagPool()
+ defer c.Stop()
+ bg := context.Background()
+ var (
+ gotmsize uint32
+ gotversion string
+ goterr error
+ wg sync.WaitGroup
+ )
+ wg.Add(1)
+ go func() {
+ tag, err := tPool.add()
+ if err != nil {
+ t.Fatalf("add tag: %v", err)
+ }
+ gotmsize, gotversion, goterr = c.Version(bg, tag, test.mSize, test.version)
+ tPool.delete(tag)
+ wg.Done()
+ }()
+ gottmsg := <-tmsgc
+ tag := gottmsg.GetTag()
+ test.rmsg.SetTag(tag)
+ rmsgc <- test.rmsg
+ done := make(chan struct{})
+ go func() { wg.Wait(); done <- struct{}{}; close(done) }()
+ select {
+ case err := <-c.errc:
+ t.Errorf("client error: %v", err)
+ return
+ case <-done:
+ }
+ if goterr != nil {
+ t.Errorf("%s: goterr: %v", test.name, goterr)
+ return
+ }
+ if test.wantmsize != gotmsize || test.wantversion != gotversion {
+ t.Errorf("%s: (mSize, verion) want: %d, %s, got: %d, %s",
+ test.name, test.wantmsize, test.wantversion,
+ gotmsize, gotversion)
+ }
+ }()
+ }
+}
diff --git a/client/export_test.go b/client/export_test.go
@@ -0,0 +1,3 @@
+package client
+
+var AllocClientFidPool = allocClientFidPool
+\ No newline at end of file
diff --git a/testfs/conn.go b/testfs/conn.go
@@ -0,0 +1,51 @@
+package testfs
+
+import (
+ "context"
+ "io"
+
+ "git.mtkn.jp/lib9p"
+ "git.mtkn.jp/lib9p/client"
+)
+
+type Conn struct {
+ S *lib9p.Server
+ C *client.Client
+ cr, sr *io.PipeReader
+ cw, sw *io.PipeWriter
+ cancel context.CancelFunc
+}
+
+// SetupTestConn setups a connection between a server and a client and
+// returns those.
+func SetupConn() *Conn {
+ const (
+ mSize = 8 * 1024
+ uname = "kenji"
+ )
+ 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)
+ ctx, cancel := context.WithCancel(context.Background())
+ go s.Serve(ctx)
+ return &Conn{
+ S: s,
+ C: c,
+ cr: cr,
+ cw: cw,
+ sr: sr,
+ sw: sw,
+ cancel: cancel,
+ }
+}
+
+func (conn *Conn) Close() {
+ conn.cancel()
+ conn.C.Stop()
+ conn.cw.Close()
+ conn.cr.Close()
+ conn.sw.Close()
+ conn.sr.Close()
+}