| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/google/go-cmp/cmp" |
| 8 | "github.com/influxdata/influxdb/services/meta" |
| 9 | ) |
| 10 | |
| 11 | func ts(s int64) time.Time { return time.Unix(s, 0).UTC() } |
| 12 | |
| 13 | func ms(times ...int64) meta.ShardGroupInfos { |
| 14 | sgis := make(meta.ShardGroupInfos, len(times)-1) |
| 15 | for i := range sgis { |
| 16 | sgis[i] = meta.ShardGroupInfo{ID: uint64(i), StartTime: ts(times[i]), EndTime: ts(times[i+1])} |
| 17 | } |
| 18 | return sgis |
| 19 | } |
| 20 | |
| 21 | func TestMakeShardGroupsForDuration(t *testing.T) { |
| 22 | tests := []struct { |
| 23 | name string |
| 24 | min time.Time |
| 25 | max time.Time |
| 26 | d time.Duration |
| 27 | exp meta.ShardGroupInfos |
| 28 | }{ |
| 29 | { |
| 30 | min: ts(15), |
| 31 | max: ts(25), |
| 32 | d: 10 * time.Second, |
| 33 | exp: ms(10, 20, 30), |
| 34 | }, |
| 35 | { |
| 36 | min: ts(15), |
| 37 | max: ts(20), |
| 38 | d: 10 * time.Second, |
| 39 | exp: ms(10, 20, 30), |
| 40 | }, |
| 41 | { |
| 42 | min: ts(15), |
| 43 | max: ts(17), |
| 44 | d: 10 * time.Second, |
| 45 | exp: ms(10, 20), |
| 46 | }, |
| 47 | { |
| 48 | min: ts(10), |
| 49 | max: ts(20), |
| 50 | d: 10 * time.Second, |
| 51 | exp: ms(10, 20, 30), |
| 52 | }, |
| 53 | } |
| 54 | for _, tt := range tests { |
| 55 | t.Run(tt.name, func(t *testing.T) { |
| 56 | if got := makeShardGroupsForDuration(tt.min, tt.max, tt.d); !cmp.Equal(got, tt.exp) { |
| 57 | t.Errorf("unexpected value -got/+exp\n%s", cmp.Diff(got, tt.exp)) |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func ms2(times ...int64) meta.ShardGroupInfos { |
| 64 | sgis := make(meta.ShardGroupInfos, len(times)/2) |
| 65 | for i := 0; i < len(times); i += 2 { |
| 66 | sgis[i/2] = meta.ShardGroupInfo{ID: uint64(i / 2), StartTime: ts(times[i]), EndTime: ts(times[i+1])} |
| 67 | } |
| 68 | return sgis |
| 69 | } |
| 70 | |
| 71 | func shardGroupEqual(x, y meta.ShardGroupInfo) bool { |
| 72 | return x.StartTime == y.StartTime && x.EndTime == y.EndTime |
| 73 | } |
| 74 | |
| 75 | func TestPlanShardGroups(t *testing.T) { |
| 76 | tests := []struct { |
| 77 | name string |
| 78 | g meta.ShardGroupInfos |
| 79 | d time.Duration |
| 80 | exp meta.ShardGroupInfos |
| 81 | }{ |
| 82 | { |
| 83 | name: "20s->10s.nogap", |
| 84 | g: ms2(20, 40, 40, 60, 60, 80), |
| 85 | d: 10 * time.Second, |
| 86 | exp: ms2(20, 30, 30, 40, 40, 50, 50, 60, 60, 70, 70, 80), |
| 87 | }, |
| 88 | { |
| 89 | name: "20s->10s.gap", |
| 90 | g: ms2(20, 40, 60, 80, 80, 100), |
| 91 | d: 10 * time.Second, |
| 92 | exp: ms2(20, 30, 30, 40, 60, 70, 70, 80, 80, 90, 90, 100), |
| 93 | }, |
| 94 | { |
| 95 | name: "05s->10s.nogap", |
| 96 | g: ms2(15, 20, 20, 25, 25, 30), |
| 97 | d: 10 * time.Second, |
| 98 | exp: ms2(10, 20, 20, 30), |
| 99 | }, |
| 100 | { |
| 101 | name: "05s->10s.gap", |
| 102 | g: ms2(15, 20, 20, 25, 50, 55, 55, 60), |
| 103 | d: 10 * time.Second, |
| 104 | exp: ms2(10, 20, 20, 30, 50, 60), |
| 105 | }, |
| 106 | } |
| 107 | for _, tc := range tests { |
| 108 | t.Run(tc.name, func(t *testing.T) { |
| 109 | min, max := tc.g[0].StartTime, tc.g[len(tc.g)-1].EndTime |
| 110 | got := planShardGroups(tc.g, min, max, tc.d) |
| 111 | if !cmp.Equal(got, tc.exp, cmp.Comparer(shardGroupEqual)) { |
| 112 | t.Errorf("unexpected value -got/+exp\n%s", cmp.Diff(got, tc.exp)) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |