commit fd27e8be4256e55e7ca5ac1342a82555d3423dc6
parent 57d25ec046ed963a79cbda43e8453efd742c6893
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 10 Oct 2023 08:25:07 +0900
add test
Diffstat:
| M | client_test.go | | | 189 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 187 insertions(+), 2 deletions(-)
diff --git a/client_test.go b/client_test.go
@@ -3,6 +3,8 @@ package lib9p
import (
"context"
"io"
+ "reflect"
+ "sync"
"testing"
)
@@ -11,6 +13,190 @@ const (
uname = "kenji"
)
+func newClientForTest(msize uint32, uname string, tmsgc chan<- Msg, rmsgc <-chan Msg) *Client {
+ ctx, cancel := context.WithCancel(context.Background())
+ c := &Client{
+ msize: mSize,
+ mSizeLock: new(sync.Mutex),
+ uname: uname,
+ fPool: allocFidPool(),
+ rPool: newClientReqPool(),
+ errc: make(chan error),
+ cancel: cancel,
+ }
+ c.txc = c.runMultiplexer(ctx, tmsgc, rmsgc)
+ return c
+}
+
+func TestClientVersion(t *testing.T) {
+ tests := []struct {
+ name string
+ tmsg *TVersion
+ rmsg Msg
+ wantrmsg Msg
+ }{
+ {"0", &TVersion{tag: NOTAG, mSize: mSize, version: "9P2000"},
+ &RVersion{tag: NOTAG, mSize: mSize, version: "9P2000"},
+ &RVersion{tag: NOTAG, mSize: mSize, version: "9P2000"}},
+ {"1", &TVersion{tag: NOTAG, mSize: mSize, version: "unko"},
+ &RVersion{tag: NOTAG, mSize: mSize, version: "unknown"},
+ &RVersion{tag: NOTAG, mSize: mSize, version: "unknown"}},
+ }
+ for _, test := range tests {
+ func() {
+ tmsgc := make(chan Msg)
+ rmsgc := make(chan Msg)
+ c := newClientForTest(mSize, uname, tmsgc, rmsgc)
+ defer c.Stop()
+ bg := context.Background()
+ var (
+ gotrmsg Msg
+ goterr error
+ wg sync.WaitGroup
+ )
+ wg.Add(1)
+ go func() {
+ gotrmsg, goterr = c.Version(bg, test.tmsg.mSize, test.tmsg.version)
+ wg.Done()
+ }()
+ gottmsg := <-tmsgc
+ tag := gottmsg.Tag()
+ test.tmsg.SetTag(tag)
+ test.rmsg.SetTag(tag)
+ test.wantrmsg.SetTag(tag)
+ if !reflect.DeepEqual(gottmsg, test.tmsg) {
+ t.Errorf("%s: input: %v, sent: %v", test.name, test.tmsg, gottmsg)
+ }
+ rmsgc <- test.rmsg
+ done := make(chan struct{})
+ go func() { wg.Wait(); done <- struct{}{} }()
+ select {
+ case err := <-c.errc:
+ t.Errorf("client error: %v", err)
+ return
+ case <-done:
+ }
+ if !reflect.DeepEqual(gotrmsg, test.wantrmsg) {
+ t.Errorf("%s: want: %v, got: %v", test.name, test.wantrmsg, gotrmsg)
+ }
+ if goterr != nil {
+ t.Errorf("%s: goterr: %v", test.name, goterr)
+ }
+ }()
+ }
+}
+
+func TestClientAuth(t *testing.T) {
+ tests := []struct {
+ name string
+ tmsg *TAuth
+ rmsg Msg
+ wantrmsg Msg
+ }{
+ {"0", &TAuth{tag: NOTAG, afid: 0, uname: uname, aname: ""},
+ &RAuth{tag: NOTAG, aqid: Qid{0, 0, 0}},
+ &RAuth{tag: NOTAG, aqid: Qid{0, 0, 0}}},
+ }
+ for _, test := range tests {
+ func() {
+ tmsgc := make(chan Msg)
+ rmsgc := make(chan Msg)
+ c := newClientForTest(mSize, uname, tmsgc, rmsgc)
+ defer c.Stop()
+ bg := context.Background()
+ var (
+ gotrmsg Msg
+ goterr error
+ wg sync.WaitGroup
+ )
+ wg.Add(1)
+ go func() {
+ gotrmsg, goterr = c.Auth(bg, test.tmsg.afid, test.tmsg.uname, test.tmsg.aname)
+ wg.Done()
+ }()
+ gottmsg := <-tmsgc
+ tag := gottmsg.Tag()
+ test.tmsg.SetTag(tag)
+ test.rmsg.SetTag(tag)
+ test.wantrmsg.SetTag(tag)
+ if !reflect.DeepEqual(gottmsg, test.tmsg) {
+ t.Errorf("%s: input: %v, sent: %v", test.name, test.tmsg, gottmsg)
+ }
+ rmsgc <- test.rmsg
+ done := make(chan struct{})
+ go func() { wg.Wait(); done <- struct{}{} }()
+ select {
+ case err := <-c.errc:
+ t.Errorf("client err: %v", err)
+ return
+ case <-done:
+ }
+ if !reflect.DeepEqual(gotrmsg, test.wantrmsg) {
+ t.Errorf("%s: want: %v, got: %v", test.name, test.wantrmsg, gotrmsg)
+ }
+ if goterr != nil {
+ t.Errorf("%s: goterr: %v", test.name, goterr)
+ }
+ }()
+ }
+}
+
+func TestClientAttach(t *testing.T) {
+ tests := []struct {
+ name string
+ tmsg *TAttach
+ rmsg Msg
+ wantrmsg Msg
+ }{
+ {"0", &TAttach{fid: 0, afid: NOFID, uname: uname, aname: ""},
+ &RAttach{qid: Qid{0, 0, 0}},
+ &RAttach{qid: Qid{0, 0, 0}}},
+ }
+ for _, test := range tests {
+ func() {
+ tmsgc := make(chan Msg)
+ rmsgc := make(chan Msg)
+ c := newClientForTest(mSize, uname, tmsgc, rmsgc)
+ defer c.Stop()
+ bg := context.Background()
+ var (
+ gotrmsg Msg
+ goterr error
+ wg sync.WaitGroup
+ )
+ wg.Add(1)
+ go func() {
+ gotrmsg, goterr = c.Attach(bg, test.tmsg.fid, test.tmsg.afid,
+ test.tmsg.uname, test.tmsg.aname)
+ wg.Done()
+ }()
+ gottmsg := <-tmsgc
+ tag := gottmsg.Tag()
+ test.tmsg.SetTag(tag)
+ test.rmsg.SetTag(tag)
+ test.wantrmsg.SetTag(tag)
+ if !reflect.DeepEqual(gottmsg, test.tmsg) {
+ t.Errorf("%s: input: %v, sent: %v", test.name, test.tmsg, gottmsg)
+ }
+ rmsgc <- test.rmsg
+ done := make(chan struct{})
+ go func() { wg.Wait(); done <- struct{}{} }()
+ select {
+ case err := <-c.errc:
+ t.Errorf("client err: %v", err)
+ return
+ case <-done:
+ }
+ if !reflect.DeepEqual(gotrmsg, test.wantrmsg) {
+ t.Errorf("%s: want: %v, got: %v", test.name, test.wantrmsg, gotrmsg)
+ }
+ if goterr != nil {
+ t.Errorf("%s: goterr: %v", test.name, goterr)
+ }
+ }()
+ }
+}
+
// the following tests are test for both client and server.
// should be moved to propper file.
func TestTransaction(t *testing.T) {
@@ -41,4 +227,4 @@ func TestTransaction(t *testing.T) {
} else {
t.Log(rattach)
}
-}
-\ No newline at end of file
+}