req_test.go (2062B)
1 package lib9p 2 3 import ( 4 "context" 5 "testing" 6 ) 7 8 // TestFlush tests if Tflush message can cancel the time-consuming Tread. 9 // It sets the testfs slow, sends a Tread message which blocks until 10 // something is sent over testfs.waitc, and sends a Tflush message, waits for 11 // the Rflush, then unblock the pending Tread message by sending something 12 // through the testfs.waitc. 13 // It checks if the req.done channel is closed. 14 // It also tests same thing for a Twrite request. 15 func TestFlush(t *testing.T) { 16 const ( 17 mSize = 8 * 1024 18 uname = "kenji" 19 ) 20 testfs.slow = true 21 defer func() { testfs.slow = false }() 22 c, rc := setupConn(testfs) 23 rtc, wtc, ftc := make(chan *request), make(chan *request), make(chan *request) 24 ctx, cancel := context.WithCancel(context.Background()) 25 defer cancel() 26 go sRead(ctx, c, rtc) 27 go sWrite(ctx, c, wtc) 28 go sFlush(ctx, c, ftc) 29 fid, err := c.fPool.add(0) 30 if err != nil { 31 t.Fatal(err) 32 } 33 fid.file, err = testfs.OpenFile("dir/file", O_RDONLY) 34 fid.path = "dir/file" 35 fid.omode = ORDWR 36 req := &request{ 37 ifcall: &TRead{Count: mSize - IOHDRSZ}, 38 done: make(chan struct{}), 39 pool: newReqPool(), 40 } 41 rtc <- req 42 <-testfs.waitc 43 ftc <- &request{ifcall: &TFlush{Tag: 1, Oldtag: 0}, oldreq: req} 44 <-rc 45 testfs.waitc <- struct{}{} // TODO: race? 46 if _, ok := <-req.done; ok { 47 t.Error("req not flushed.") 48 } 49 data := []byte("data") 50 req = &request{ 51 ifcall: &TWrite{Count: uint32(len(data)), Data: data}, 52 done: make(chan struct{}), 53 pool: newReqPool(), 54 } 55 wtc <- req 56 <-testfs.waitc 57 ftc <- &request{ifcall: &TFlush{Tag: 1, Oldtag: 0}, oldreq: req} 58 <-rc 59 testfs.waitc <- struct{}{} // TODO: race? 60 if _, ok := <-req.done; ok { 61 t.Error("req not flushed.") 62 } 63 } 64 65 func TestReqPool(t *testing.T) { 66 rp := newReqPool() 67 for i := 0; i < 10; i++ { 68 if _, err := rp.add(uint16(i)); err != nil { 69 t.Fatal(err) 70 } 71 } 72 r, err := rp.add(NOTAG) 73 if err != nil { 74 t.Fatal(err) 75 } 76 if len(rp.m) != 1 { 77 t.Fatalf("requests not deleted: %v", rp.m) 78 } 79 if rp.m[NOTAG] != r { 80 t.Fatalf("requests mismatch: %v", rp.m) 81 } 82 }