github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/api/fs_test.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "io" 6 "reflect" 7 "strings" 8 "testing" 9 "time" 10 ) 11 12 func TestFS_FrameReader(t *testing.T) { 13 t.Parallel() 14 // Create a channel of the frames and a cancel channel 15 framesCh := make(chan *StreamFrame, 3) 16 errCh := make(chan error) 17 cancelCh := make(chan struct{}) 18 19 r := NewFrameReader(framesCh, errCh, cancelCh) 20 21 // Create some frames and send them 22 f1 := &StreamFrame{ 23 File: "foo", 24 Offset: 5, 25 Data: []byte("hello"), 26 } 27 f2 := &StreamFrame{ 28 File: "foo", 29 Offset: 10, 30 Data: []byte(", wor"), 31 } 32 f3 := &StreamFrame{ 33 File: "foo", 34 Offset: 12, 35 Data: []byte("ld"), 36 } 37 framesCh <- f1 38 framesCh <- f2 39 framesCh <- f3 40 close(framesCh) 41 42 expected := []byte("hello, world") 43 44 // Read a little 45 p := make([]byte, 12) 46 47 n, err := r.Read(p[:5]) 48 if err != nil { 49 t.Fatalf("Read failed: %v", err) 50 } 51 if off := r.Offset(); off != n { 52 t.Fatalf("unexpected read bytes: got %v; wanted %v", n, off) 53 } 54 55 off := n 56 for { 57 n, err = r.Read(p[off:]) 58 if err != nil { 59 if err == io.EOF { 60 break 61 } 62 t.Fatalf("Read failed: %v", err) 63 } 64 off += n 65 } 66 67 if !reflect.DeepEqual(p, expected) { 68 t.Fatalf("read %q, wanted %q", string(p), string(expected)) 69 } 70 71 if err := r.Close(); err != nil { 72 t.Fatalf("Close() failed: %v", err) 73 } 74 if _, ok := <-cancelCh; ok { 75 t.Fatalf("Close() didn't close cancel channel") 76 } 77 if len(expected) != r.Offset() { 78 t.Fatalf("offset %d, wanted %d", r.Offset(), len(expected)) 79 } 80 } 81 82 func TestFS_FrameReader_Unblock(t *testing.T) { 83 t.Parallel() 84 // Create a channel of the frames and a cancel channel 85 framesCh := make(chan *StreamFrame, 3) 86 errCh := make(chan error) 87 cancelCh := make(chan struct{}) 88 89 r := NewFrameReader(framesCh, errCh, cancelCh) 90 r.SetUnblockTime(10 * time.Millisecond) 91 92 // Read a little 93 p := make([]byte, 12) 94 95 n, err := r.Read(p) 96 if err != nil { 97 t.Fatalf("Read failed: %v", err) 98 } 99 100 if n != 0 { 101 t.Fatalf("should have unblocked") 102 } 103 104 // Unset the unblock 105 r.SetUnblockTime(0) 106 107 resultCh := make(chan struct{}) 108 go func() { 109 r.Read(p) 110 close(resultCh) 111 }() 112 113 select { 114 case <-resultCh: 115 t.Fatalf("shouldn't have unblocked") 116 case <-time.After(300 * time.Millisecond): 117 } 118 } 119 120 func TestFS_FrameReader_Error(t *testing.T) { 121 t.Parallel() 122 // Create a channel of the frames and a cancel channel 123 framesCh := make(chan *StreamFrame, 3) 124 errCh := make(chan error, 1) 125 cancelCh := make(chan struct{}) 126 127 r := NewFrameReader(framesCh, errCh, cancelCh) 128 r.SetUnblockTime(10 * time.Millisecond) 129 130 // Send an error 131 expected := fmt.Errorf("test error") 132 errCh <- expected 133 134 // Read a little 135 p := make([]byte, 12) 136 137 _, err := r.Read(p) 138 if err == nil || !strings.Contains(err.Error(), expected.Error()) { 139 t.Fatalf("bad error: %v", err) 140 } 141 }