commit 6ed65435f02e17096812fbf8d419f8468ef3625a
parent f6789a34b394239544179747522339eca903913b
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 26 Dec 2023 11:46:58 +0900
add TestGetReq
Diffstat:
2 files changed, 52 insertions(+), 3 deletions(-)
diff --git a/server.go b/server.go
@@ -152,8 +152,9 @@ func (s *Server) runSpeaker(ctx context.Context) {
}()
}
-// GetReq reads 9P message from s.r, allocates Req and returns it.
-// Any error it encounters is embedded into the Req struct.
+// GetReq reads 9P message from s.r, allocates Req, adds it to s.rPool,
+// and returns it.
+// Any error it encountered is embedded into the Req struct.
// This function is called only by the server's listener goroutine,
// and does not need to lock s.r.
// The argument ctx is used to set the request's context.
diff --git a/server2_test.go b/server2_test.go
@@ -94,4 +94,52 @@ func TestRunSpeaker(t *testing.T) {
}
}
-func TestGetReq(t *testing.T) {}
+func TestGetReq(t *testing.T) {
+ tFile, err := os.Open("testdata/test_Tmsg.dat")
+ if err != nil {
+ t.Fatalf("open file: %v", err)
+ }
+ defer tFile.Close()
+ tFile2, err := os.Open("testdata/test_Tmsg.dat")
+ if err != nil {
+ t.Fatalf("open file: %v", err)
+ }
+ defer tFile2.Close()
+ s := &Server{r: tFile, rPool: newReqPool()}
+ for {
+ ctx, cancel := context.WithCancel(context.Background())
+ got := s.getReq(ctx)
+ if got.listenErr == io.EOF {
+ break
+ } else if got.listenErr != nil {
+ t.Fatalf("getReq: %v", got.listenErr)
+ }
+ wantMsg, err := RecvMsg(tFile2)
+ if err != nil {
+ t.Fatalf("recvmsg: %v", err)
+ }
+ if got.Srv != s {
+ t.Errorf("r.Srv not set properly.")
+ }
+ if got.Tag != wantMsg.GetTag() {
+ t.Errorf("r.Tag: want: %v, got: %v", wantMsg.GetTag(), got.Tag)
+ }
+ if !reflect.DeepEqual(got.Ifcall, wantMsg) {
+ t.Errorf("r.Ifcall:\n\twant: %v,\n\tgot: %v", wantMsg, got.Ifcall)
+ }
+ got2, ok := s.rPool.lookup(wantMsg.GetTag())
+ if !ok {
+ t.Errorf("request not registered to the pool")
+ }
+ if got != got2 {
+ t.Errorf("wrong message in pool:\n\twant: %p,\n\tgot: %p", got, got2)
+ }
+ s.rPool.delete(wantMsg.GetTag())
+ cancel()
+ select {
+ case <-got.Done:
+ default:
+ t.Errorf("r.Done not closed")
+ }
+ }
+}