|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + . "github.com/agiledragon/gomonkey/v2" |
| 6 | + "reflect" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/PaddlePaddle/PaddleFlow/pkg/fs/client/ufs" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_rCache_readFromReadAhead(t *testing.T) { |
| 14 | + type fields struct { |
| 15 | + id string |
| 16 | + flags uint32 |
| 17 | + length int |
| 18 | + store *store |
| 19 | + ufs ufs.UnderFileStorage |
| 20 | + buffers ReadBufferMap |
| 21 | + bufferPool *BufferPool |
| 22 | + lock sync.RWMutex |
| 23 | + seqReadAmount uint64 |
| 24 | + } |
| 25 | + type args struct { |
| 26 | + off int64 |
| 27 | + buf []byte |
| 28 | + } |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + fields fields |
| 32 | + args args |
| 33 | + wantBytesRead int |
| 34 | + wantErr bool |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "test read err", |
| 38 | + fields: fields{ |
| 39 | + store: &store{ |
| 40 | + conf: Config{BlockSize: 1}, |
| 41 | + }, |
| 42 | + id: "xx", |
| 43 | + length: 10, |
| 44 | + buffers: ReadBufferMap{1: &ReadBuffer{}}, |
| 45 | + }, |
| 46 | + wantBytesRead: 0, |
| 47 | + wantErr: true, |
| 48 | + args: args{ |
| 49 | + off: 1, |
| 50 | + buf: make([]byte, 12), |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + for _, tt := range tests { |
| 55 | + t.Run(tt.name, func(t *testing.T) { |
| 56 | + r := &rCache{ |
| 57 | + id: tt.fields.id, |
| 58 | + flags: tt.fields.flags, |
| 59 | + length: tt.fields.length, |
| 60 | + store: tt.fields.store, |
| 61 | + ufs: tt.fields.ufs, |
| 62 | + buffers: tt.fields.buffers, |
| 63 | + bufferPool: tt.fields.bufferPool, |
| 64 | + lock: tt.fields.lock, |
| 65 | + seqReadAmount: tt.fields.seqReadAmount, |
| 66 | + } |
| 67 | + if tt.name == "test read err" { |
| 68 | + var p1 = ApplyMethod(reflect.TypeOf(&ReadBuffer{}), "ReadAt", |
| 69 | + func(_ *ReadBuffer, offset uint64, p []byte) (n int, err error) { |
| 70 | + return 0, fmt.Errorf("read fail") |
| 71 | + }) |
| 72 | + defer p1.Reset() |
| 73 | + } |
| 74 | + gotBytesRead, err := r.readFromReadAhead(tt.args.off, tt.args.buf) |
| 75 | + if (err != nil) != tt.wantErr { |
| 76 | + t.Errorf("readFromReadAhead() error = %v, wantErr %v", err, tt.wantErr) |
| 77 | + return |
| 78 | + } |
| 79 | + if gotBytesRead != tt.wantBytesRead { |
| 80 | + t.Errorf("readFromReadAhead() gotBytesRead = %v, want %v", gotBytesRead, tt.wantBytesRead) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
0 commit comments