|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package orchestrator |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/google/btree" |
| 21 | + "github.com/google/go-cmp/cmp" |
| 22 | +) |
| 23 | + |
| 24 | +func getChunkStateItemList(cs *ChunkState) []chunkStateItem { |
| 25 | + var items []chunkStateItem |
| 26 | + cs.items.Ascend(func(item btree.Item) bool { |
| 27 | + items = append(items, item.(chunkStateItem)) |
| 28 | + return true |
| 29 | + }) |
| 30 | + return items |
| 31 | +} |
| 32 | + |
| 33 | +func TestChunkStateUpdateSucceedsForSeparatedChunks(t *testing.T) { |
| 34 | + cs := NewChunkState(100) |
| 35 | + cs.Update(0, 10) |
| 36 | + cs.Update(40, 50) |
| 37 | + cs.Update(90, 100) |
| 38 | + items := getChunkStateItemList(cs) |
| 39 | + expected := []chunkStateItem{ |
| 40 | + {offset: 0, isUpdated: true}, |
| 41 | + {offset: 10, isUpdated: false}, |
| 42 | + {offset: 40, isUpdated: true}, |
| 43 | + {offset: 50, isUpdated: false}, |
| 44 | + {offset: 90, isUpdated: true}, |
| 45 | + {offset: 100, isUpdated: false}, |
| 46 | + } |
| 47 | + if diff := cmp.Diff(expected, items, cmp.AllowUnexported(chunkStateItem{})); diff != "" { |
| 48 | + t.Fatalf("chunk state mismatch (-want +got):\n%s", diff) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestChunkStateUpdateSucceedsForSubranges(t *testing.T) { |
| 53 | + cs := NewChunkState(100) |
| 54 | + cs.Update(40, 50) |
| 55 | + cs.Update(60, 70) |
| 56 | + cs.Update(30, 80) |
| 57 | + cs.Update(50, 60) |
| 58 | + cs.Update(65, 75) |
| 59 | + items := getChunkStateItemList(cs) |
| 60 | + expected := []chunkStateItem{ |
| 61 | + {offset: 0, isUpdated: false}, |
| 62 | + {offset: 30, isUpdated: true}, |
| 63 | + {offset: 80, isUpdated: false}, |
| 64 | + } |
| 65 | + if diff := cmp.Diff(expected, items, cmp.AllowUnexported(chunkStateItem{})); diff != "" { |
| 66 | + t.Fatalf("chunk state mismatch (-want +got):\n%s", diff) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestChunkStateUpdateSucceedsForOverlappingRanges(t *testing.T) { |
| 71 | + cs := NewChunkState(100) |
| 72 | + cs.Update(10, 30) |
| 73 | + cs.Update(20, 40) |
| 74 | + cs.Update(70, 90) |
| 75 | + cs.Update(60, 80) |
| 76 | + items := getChunkStateItemList(cs) |
| 77 | + expected := []chunkStateItem{ |
| 78 | + {offset: 0, isUpdated: false}, |
| 79 | + {offset: 10, isUpdated: true}, |
| 80 | + {offset: 40, isUpdated: false}, |
| 81 | + {offset: 60, isUpdated: true}, |
| 82 | + {offset: 90, isUpdated: false}, |
| 83 | + } |
| 84 | + if diff := cmp.Diff(expected, items, cmp.AllowUnexported(chunkStateItem{})); diff != "" { |
| 85 | + t.Fatalf("chunk state mismatch (-want +got):\n%s", diff) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestChunkStateUpdateSucceedsForSameStartOrEnd(t *testing.T) { |
| 90 | + cs := NewChunkState(100) |
| 91 | + cs.Update(10, 15) |
| 92 | + cs.Update(10, 12) |
| 93 | + cs.Update(20, 22) |
| 94 | + cs.Update(20, 25) |
| 95 | + cs.Update(55, 60) |
| 96 | + cs.Update(58, 60) |
| 97 | + cs.Update(65, 70) |
| 98 | + cs.Update(68, 70) |
| 99 | + items := getChunkStateItemList(cs) |
| 100 | + expected := []chunkStateItem{ |
| 101 | + {offset: 0, isUpdated: false}, |
| 102 | + {offset: 10, isUpdated: true}, |
| 103 | + {offset: 15, isUpdated: false}, |
| 104 | + {offset: 20, isUpdated: true}, |
| 105 | + {offset: 25, isUpdated: false}, |
| 106 | + {offset: 55, isUpdated: true}, |
| 107 | + {offset: 60, isUpdated: false}, |
| 108 | + {offset: 65, isUpdated: true}, |
| 109 | + {offset: 70, isUpdated: false}, |
| 110 | + } |
| 111 | + if diff := cmp.Diff(expected, items, cmp.AllowUnexported(chunkStateItem{})); diff != "" { |
| 112 | + t.Fatalf("chunk state mismatch (-want +got):\n%s", diff) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestChunkStateUpdateSucceedsForMergingChunks(t *testing.T) { |
| 117 | + cs := NewChunkState(100) |
| 118 | + cs.Update(10, 20) |
| 119 | + cs.Update(30, 40) |
| 120 | + cs.Update(50, 60) |
| 121 | + cs.Update(20, 30) |
| 122 | + cs.Update(40, 50) |
| 123 | + items := getChunkStateItemList(cs) |
| 124 | + expected := []chunkStateItem{ |
| 125 | + {offset: 0, isUpdated: false}, |
| 126 | + {offset: 10, isUpdated: true}, |
| 127 | + {offset: 60, isUpdated: false}, |
| 128 | + } |
| 129 | + if diff := cmp.Diff(expected, items, cmp.AllowUnexported(chunkStateItem{})); diff != "" { |
| 130 | + t.Fatalf("chunk state mismatch (-want +got):\n%s", diff) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestChunkStateUpdateSucceedsForInvalidRanges(t *testing.T) { |
| 135 | + cs := NewChunkState(100) |
| 136 | + if err := cs.Update(-1, 10); err == nil { |
| 137 | + t.Fatalf("expected an error"); |
| 138 | + } |
| 139 | + if err := cs.Update(90, 101); err == nil { |
| 140 | + t.Fatalf("expected an error"); |
| 141 | + } |
| 142 | + items := getChunkStateItemList(cs) |
| 143 | + expected := []chunkStateItem{ |
| 144 | + {offset: 0, isUpdated: false}, |
| 145 | + } |
| 146 | + if diff := cmp.Diff(expected, items, cmp.AllowUnexported(chunkStateItem{})); diff != "" { |
| 147 | + t.Fatalf("chunk state mismatch (-want +got):\n%s", diff) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestChunkStateIsCompleted(t *testing.T) { |
| 152 | + cs := NewChunkState(100) |
| 153 | + cs.Update(0, 20) |
| 154 | + cs.Update(80, 100) |
| 155 | + cs.Update(20, 80) |
| 156 | + if !cs.IsCompleted() { |
| 157 | + t.Fatalf("expected as completed") |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestChunkStateIsNotCompletedWithMissingStart(t *testing.T) { |
| 162 | + cs := NewChunkState(100) |
| 163 | + cs.Update(1, 100) |
| 164 | + if cs.IsCompleted() { |
| 165 | + t.Fatalf("expected as not completed") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestChunkStateIsNotCompletedWithMissingIntermediate(t *testing.T) { |
| 170 | + cs := NewChunkState(100) |
| 171 | + cs.Update(0, 20) |
| 172 | + cs.Update(80, 100) |
| 173 | + cs.Update(20, 79) |
| 174 | + if cs.IsCompleted() { |
| 175 | + t.Fatalf("expected as not completed") |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestChunkStateIsNotCompletedWithMissingEnd(t *testing.T) { |
| 180 | + cs := NewChunkState(100) |
| 181 | + cs.Update(0, 99) |
| 182 | + if cs.IsCompleted() { |
| 183 | + t.Fatalf("expected as not completed") |
| 184 | + } |
| 185 | +} |
0 commit comments