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