github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/api/fs_test.go (about)

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