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

     1  package nomad
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/ugorji/go/codec"
    11  )
    12  
    13  func TestTimeTable(t *testing.T) {
    14  	tt := NewTimeTable(time.Second, time.Minute)
    15  
    16  	index := tt.NearestIndex(time.Now())
    17  	if index != 0 {
    18  		t.Fatalf("bad: %v", index)
    19  	}
    20  
    21  	when := tt.NearestTime(1000)
    22  	if !when.IsZero() {
    23  		t.Fatalf("bad: %v", when)
    24  	}
    25  
    26  	// Witness some data
    27  	start := time.Now()
    28  	plusOne := start.Add(time.Minute)
    29  	plusTwo := start.Add(2 * time.Minute)
    30  	plusFive := start.Add(5 * time.Minute)
    31  	plusThirty := start.Add(30 * time.Minute)
    32  	plusHour := start.Add(60 * time.Minute)
    33  	plusHourHalf := start.Add(90 * time.Minute)
    34  
    35  	tt.Witness(2, start)
    36  	tt.Witness(2, start)
    37  
    38  	tt.Witness(10, plusOne)
    39  	tt.Witness(10, plusOne)
    40  
    41  	tt.Witness(20, plusTwo)
    42  	tt.Witness(20, plusTwo)
    43  
    44  	tt.Witness(30, plusFive)
    45  	tt.Witness(30, plusFive)
    46  
    47  	tt.Witness(40, plusThirty)
    48  	tt.Witness(40, plusThirty)
    49  
    50  	tt.Witness(50, plusHour)
    51  	tt.Witness(50, plusHour)
    52  
    53  	type tcase struct {
    54  		when        time.Time
    55  		expectIndex uint64
    56  
    57  		index      uint64
    58  		expectWhen time.Time
    59  	}
    60  	cases := []tcase{
    61  		// Exact match
    62  		{start, 2, 2, start},
    63  		{plusOne, 10, 10, plusOne},
    64  		{plusHour, 50, 50, plusHour},
    65  
    66  		// Before the newest entry
    67  		{plusHourHalf, 50, 51, plusHour},
    68  
    69  		// After the oldest entry
    70  		{time.Time{}, 0, 1, time.Time{}},
    71  
    72  		// Mid range
    73  		{start.Add(3 * time.Minute), 20, 25, plusTwo},
    74  	}
    75  
    76  	for _, tc := range cases {
    77  		index := tt.NearestIndex(tc.when)
    78  		if index != tc.expectIndex {
    79  			t.Fatalf("bad: %v %v", index, tc.expectIndex)
    80  		}
    81  
    82  		when := tt.NearestTime(tc.index)
    83  		if when != tc.expectWhen {
    84  			t.Fatalf("bad: for %d %v %v", tc.index, when, tc.expectWhen)
    85  		}
    86  	}
    87  }
    88  
    89  func TestTimeTable_SerializeDeserialize(t *testing.T) {
    90  	tt := NewTimeTable(time.Second, time.Minute)
    91  
    92  	// Witness some data
    93  	start := time.Now()
    94  	plusOne := start.Add(time.Minute)
    95  	plusTwo := start.Add(2 * time.Minute)
    96  	plusFive := start.Add(5 * time.Minute)
    97  	plusThirty := start.Add(30 * time.Minute)
    98  	plusHour := start.Add(60 * time.Minute)
    99  
   100  	tt.Witness(2, start)
   101  	tt.Witness(10, plusOne)
   102  	tt.Witness(20, plusTwo)
   103  	tt.Witness(30, plusFive)
   104  	tt.Witness(40, plusThirty)
   105  	tt.Witness(50, plusHour)
   106  
   107  	var buf bytes.Buffer
   108  	enc := codec.NewEncoder(&buf, structs.MsgpackHandle)
   109  
   110  	err := tt.Serialize(enc)
   111  	if err != nil {
   112  		t.Fatalf("err: %v", err)
   113  	}
   114  
   115  	dec := codec.NewDecoder(&buf, structs.MsgpackHandle)
   116  
   117  	tt2 := NewTimeTable(time.Second, time.Minute)
   118  	err = tt2.Deserialize(dec)
   119  	if err != nil {
   120  		t.Fatalf("err: %v", err)
   121  	}
   122  
   123  	if !reflect.DeepEqual(tt.table, tt2.table) {
   124  		t.Fatalf("bad: %#v %#v", tt, tt2)
   125  	}
   126  }
   127  
   128  func TestTimeTable_Overflow(t *testing.T) {
   129  	tt := NewTimeTable(time.Second, 3*time.Second)
   130  
   131  	// Witness some data
   132  	start := time.Now()
   133  	plusOne := start.Add(time.Second)
   134  	plusTwo := start.Add(2 * time.Second)
   135  	plusThree := start.Add(3 * time.Second)
   136  
   137  	tt.Witness(10, start)
   138  	tt.Witness(20, plusOne)
   139  	tt.Witness(30, plusTwo)
   140  	tt.Witness(40, plusThree)
   141  
   142  	if len(tt.table) != 3 {
   143  		t.Fatalf("bad")
   144  	}
   145  
   146  	index := tt.NearestIndex(start)
   147  	if index != 0 {
   148  		t.Fatalf("bad: %v %v", index, 0)
   149  	}
   150  
   151  	when := tt.NearestTime(15)
   152  	if !when.IsZero() {
   153  		t.Fatalf("bad: %v", when)
   154  	}
   155  }