commit 57d25ec046ed963a79cbda43e8453efd742c6893
parent f9d8df1001c4f92056eb6f6dccc457f5415b1175
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 10 Oct 2023 06:44:47 +0900
delete broken tests
Diffstat:
| M | client.go | | | 15 | ++++++++++++--- |
| M | client_test.go | | | 120 | ++----------------------------------------------------------------------------- |
2 files changed, 15 insertions(+), 120 deletions(-)
diff --git a/client.go b/client.go
@@ -15,6 +15,7 @@ type Client struct {
rPool *clientReqPool
txc chan<- *clientReq
errc chan error
+ cancel context.CancelFunc
}
type LibErr string
@@ -25,6 +26,7 @@ func newLibErrMsg(format string, a ...any) *RError {
}
func NewClient(mSize uint32, uname string, r io.Reader, w io.Writer) *Client {
+ ctx, cancel := context.WithCancel(context.Background())
c := &Client{
msize: mSize,
mSizeLock: new(sync.Mutex),
@@ -32,13 +34,20 @@ func NewClient(mSize uint32, uname string, r io.Reader, w io.Writer) *Client {
fPool: allocFidPool(),
rPool: newClientReqPool(),
errc: make(chan error),
+ cancel: cancel,
}
- tmsgc := c.runSpeaker(context.TODO(), w)
- rmsgc := c.runListener(context.TODO(), r)
- c.txc = c.runMultiplexer(context.TODO(), tmsgc, rmsgc)
+ tmsgc := c.runSpeaker(ctx, w)
+ rmsgc := c.runListener(ctx, r)
+ c.txc = c.runMultiplexer(ctx, tmsgc, rmsgc)
return c
}
+func (c *Client) Stop() {
+ // TODO: fPool, rPool
+ c.cancel()
+ close(c.errc)
+}
+
func (c *Client) mSize() uint32 {
c.mSizeLock.Lock()
defer c.mSizeLock.Unlock()
diff --git a/client_test.go b/client_test.go
@@ -2,10 +2,7 @@ package lib9p
import (
"context"
- "fmt"
"io"
- "reflect"
- "sync"
"testing"
)
@@ -14,82 +11,6 @@ const (
uname = "kenji"
)
-func newClientForTest() (*Client, chan<- Msg, chan<- error) {
- tmsgc := make(chan Msg, 1)
- rerrc := make(chan error, 1)
- c := &Client{
- msize: mSize,
- mSizeLock: new(sync.Mutex),
- uname: uname,
- fPool: allocFidPool(),
- rPool: newClientReqPool(),
- txc: make(chan *clientReq, 1),
- }
- return c, tmsgc, rerrc
-}
-
-type msgTest struct {
- name string
- msg Msg
- reply Msg
- wantmsg Msg
- wanterr error
-}
-
-func dummyTransact(test msgTest) (gotmsg Msg, goterr error) {
- c, _, _ := newClientForTest()
- var wg sync.WaitGroup
- wg.Add(1)
- go func() {
- switch m := test.msg.(type) {
- case *TVersion:
- gotmsg, goterr = c.Version(context.Background(), m.mSize, m.version)
- default:
- goterr = fmt.Errorf("unknown msg type: %v", m)
- }
- wg.Done()
- }()
-
- var ok bool
- var req *clientReq
- for !ok {
- req, ok = c.rPool.lookup(NOTAG)
- }
- req.rxc <- test.reply
- close(req.errc)
- close(req.rmsgc)
-
- wg.Wait()
- return gotmsg, goterr
-}
-
-func TestClientVersion(t *testing.T) {
- tests := []msgTest{
- {
- "valid",
- &TVersion{NOTAG, mSize, "9P2000"},
- &RVersion{NOTAG, mSize, "9P2000"},
- &RVersion{NOTAG, mSize, "9P2000"},
- nil,
- },
- {
- "invalid",
- &TVersion{NOTAG, mSize, "unko"},
- &RVersion{NOTAG, mSize, "unknown"},
- &RVersion{NOTAG, mSize, "unknown"},
- nil,
- },
- }
- for _, test := range tests {
- gotmsg, goterr := dummyTransact(test)
- if !reflect.DeepEqual(gotmsg, test.wantmsg) || !reflect.DeepEqual(goterr, test.wanterr) {
- t.Errorf("%s: got: (%v, %v), want: (%v, %v)\n",
- test.name, gotmsg, goterr,
- test.wantmsg, test.wanterr)
- }
- }
-}
-/*
// the following tests are test for both client and server.
// should be moved to propper file.
func TestTransaction(t *testing.T) {
@@ -98,7 +19,7 @@ func TestTransaction(t *testing.T) {
server := NewServer(fsys, mSize, sr, sw)
//server.Chatty()
- client := NewClient(mSize, "kenji", cr, cw)
+ client := NewClient(mSize, uname, cr, cw)
bg := context.Background()
go server.Serve()
@@ -120,40 +41,4 @@ func TestTransaction(t *testing.T) {
} else {
t.Log(rattach)
}
-}
-*/
-
-func TestVersion(t *testing.T) {
- const (
- mSize = 8192
- noTag = NOTAG
- )
- tests := []struct {
- name string
- mSize uint32
- version string
- want Msg
- }{
- {"size", 4096, "9P2000",
- &RVersion{tag: NOTAG, mSize: 4096, version: "9P2000"}},
- {"valid", mSize, "9P2000",
- &RVersion{tag: NOTAG, mSize: mSize, version: "9P2000"}},
- {"invalid", mSize, "unko",
- &RVersion{tag: NOTAG, mSize: mSize, version: "unknown"}},
- }
-
- for _, test := range tests {
- cr, sw := io.Pipe()
- sr, cw := io.Pipe()
-
- server := NewServer(fsys, mSize, sr, sw)
- client := NewClient(mSize, "kenji", cr, cw)
- go server.Serve()
- got, err := client.Version(context.TODO(), test.mSize, test.version)
- if err != nil {
- t.Errorf("%s: %v", test.name, err)
- } else if !reflect.DeepEqual(got, test.want) {
- t.Errorf("%s: want %v, get %v\n", test.name, test.want, got)
- }
- }
-}
+}
+\ No newline at end of file