lib9p

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

client2_test.go (2173B)


      1 package client
      2 
      3 import (
      4 	"context"
      5 	"sync"
      6 	"testing"
      7 
      8 	"git.mtkn.jp/lib9p"
      9 )
     10 
     11 const (
     12 	mSize = 8192
     13 	uname = "kenji"
     14 )
     15 
     16 func newClientForTest(msize uint32, uname string, tmsgc chan<- lib9p.Msg, rmsgc <-chan lib9p.Msg) *Client {
     17 	ctx, cancel := context.WithCancel(context.Background())
     18 	c := &Client{
     19 		msize:     mSize,
     20 		mSizeLock: new(sync.Mutex),
     21 		uname:     uname,
     22 		fPool:     allocClientFidPool(),
     23 		errc:      make(chan error),
     24 		cancel:    cancel,
     25 		wg:        new(sync.WaitGroup),
     26 	}
     27 	c.txc = c.runMultiplexer(ctx, tmsgc, rmsgc)
     28 	return c
     29 }
     30 
     31 func TestClientVersion(t *testing.T) {
     32 	tests := []struct {
     33 		name        string
     34 		mSize       uint32
     35 		version     string
     36 		rmsg        lib9p.Msg
     37 		wantmsize   uint32
     38 		wantversion string
     39 	}{
     40 		{"0", mSize, "9P2000",
     41 			&lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "9P2000"},
     42 			mSize, "9P2000"},
     43 		{"1", mSize, "unko",
     44 			&lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "unknown"},
     45 			mSize, "unknown"},
     46 	}
     47 	for _, test := range tests {
     48 		func() {
     49 			tmsgc := make(chan lib9p.Msg)
     50 			rmsgc := make(chan lib9p.Msg)
     51 			c := newClientForTest(mSize, uname, tmsgc, rmsgc)
     52 			tPool := newTagPool()
     53 			defer c.Stop()
     54 			bg := context.Background()
     55 			var (
     56 				gotmsize   uint32
     57 				gotversion string
     58 				goterr     error
     59 				wg         sync.WaitGroup
     60 			)
     61 			wg.Add(1)
     62 			go func() {
     63 				tag, err := tPool.add()
     64 				if err != nil {
     65 					t.Fatalf("add tag: %v", err)
     66 				}
     67 				gotmsize, gotversion, goterr = c.Version(bg, tag, test.mSize, test.version)
     68 				tPool.delete(tag)
     69 				wg.Done()
     70 			}()
     71 			gottmsg := <-tmsgc
     72 			tag := gottmsg.GetTag()
     73 			test.rmsg.SetTag(tag)
     74 			rmsgc <- test.rmsg
     75 			done := make(chan struct{})
     76 			go func() { wg.Wait(); done <- struct{}{}; close(done) }()
     77 			select {
     78 			case err := <-c.errc:
     79 				t.Errorf("client error: %v", err)
     80 				return
     81 			case <-done:
     82 			}
     83 			if goterr != nil {
     84 				t.Errorf("%s: goterr: %v", test.name, goterr)
     85 				return
     86 			}
     87 			if test.wantmsize != gotmsize || test.wantversion != gotversion {
     88 				t.Errorf("%s: (mSize, verion) want: %d, %s, got: %d, %s",
     89 					test.name, test.wantmsize, test.wantversion,
     90 					gotmsize, gotversion)
     91 			}
     92 		}()
     93 	}
     94 }