lib9p

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

commit aae9422cc1c248755223104fbefda57e1aa40554
parent 6ed65435f02e17096812fbf8d419f8468ef3625a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue, 26 Dec 2023 12:10:23 +0900

add TestSVersion

Diffstat:
Mserver2_test.go | 41+++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+), 0 deletions(-)

diff --git a/server2_test.go b/server2_test.go @@ -6,6 +6,7 @@ import ( "io" "os" "reflect" + "sync" "testing" ) @@ -143,3 +144,42 @@ func TestGetReq(t *testing.T) { } } } + +func TestSVersion(t *testing.T) { + tests := []struct{ + input *Req + want *Req + }{ + {&Req{Ifcall: &TVersion{Msize: 1024, Version: "9P2000"}}, + &Req{Ofcall: &RVersion{Msize: 1024, Version: "9P2000"}}}, + {&Req{Ifcall: &TVersion{Msize: 564, Version: "9P2000"}}, + &Req{Ofcall: &RVersion{Msize: 564, Version: "9P2000"}}}, + {&Req{Ifcall: &TVersion{Msize: 8*1024, Version: "9P2000"}}, + &Req{Ofcall: &RVersion{Msize: 1024, Version: "9P2000"}}}, + {&Req{Ifcall: &TVersion{Msize: 1024, Version: "unko"}}, + &Req{Ofcall: &RVersion{Msize: 1024, Version: "unknown"}}}, + } + tc := make(chan *Req) + rc := make(chan *Req) + s := &Server{msize: 1024, mSizeLock: new(sync.Mutex), respChan: rc} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go sVersion(ctx, s, tc) + for _, test := range tests { + oldMsize := s.msize + tc <- test.input + ifcall := test.input.Ifcall.(*TVersion) + wantmsg := test.want.Ofcall.(*RVersion) + gotmsg := (<-rc).Ofcall.(*RVersion) + if !reflect.DeepEqual(wantmsg, gotmsg) { + t.Errorf("want: %v,\n\tgot: %v", wantmsg, gotmsg) + } + if ifcall.Msize < oldMsize && s.msize != ifcall.Msize { + t.Errorf("msize not changed") + } + if ifcall.Msize >= oldMsize && s.msize != oldMsize { + t.Errorf("msize changed unexpectedly") + } + s.msize = oldMsize + } +} +\ No newline at end of file