gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/uploadheapworsthealth_test.go (about) 1 package renter 2 3 import ( 4 "testing" 5 ) 6 7 // TestUpdateWorstIgnoredHealth probes the implementation of the 8 // updateWorstIgnoredHealth function. 9 func TestUpdateWorstIgnoredHealth(t *testing.T) { 10 t.Parallel() 11 12 // Start by updating a blank with blank values. 13 var wh worstIgnoredHealth 14 wh.updateWorstIgnoredHealth(0, false) 15 if wh.health != 0 || wh.remote != false { 16 t.Error("bad wh update") 17 } 18 // Try updating with a higher health, but below the repair threshold. 19 wh.updateWorstIgnoredHealth(0.2, false) 20 if wh.health != 0 || wh.remote != false { 21 t.Error("bad wh update") 22 } 23 // Try updating with a higher health. 24 wh.updateWorstIgnoredHealth(0.4, false) 25 if wh.health != 0.4 || wh.remote != false { 26 t.Error("bad wh update") 27 } 28 // Try updating with a lower health. 29 wh.updateWorstIgnoredHealth(0.3, false) 30 if wh.health != 0.4 || wh.remote != false { 31 t.Error("bad wh update") 32 } 33 // Try updating with a lower health and remote set, but below the repair 34 // threshold. 35 wh.updateWorstIgnoredHealth(0.1, true) 36 if wh.health != 0.4 || wh.remote != false { 37 t.Error("bad wh update") 38 } 39 // Try updating with a lower health and remote set, above repair threshold. 40 wh.updateWorstIgnoredHealth(0.3, true) 41 if wh.health != 0.3 || wh.remote != true { 42 t.Error("bad wh update") 43 } 44 // Try updating with a lower health and remote set. 45 wh.updateWorstIgnoredHealth(0.28, true) 46 if wh.health != 0.3 || wh.remote != true { 47 t.Error("bad wh update") 48 } 49 // Try updating with a lower health and remote not set. 50 wh.updateWorstIgnoredHealth(0.01, false) 51 if wh.health != 0.3 || wh.remote != true { 52 t.Error("bad wh update") 53 } 54 // Try updating with a lower health and remote not set. 55 wh.updateWorstIgnoredHealth(0.27, false) 56 if wh.health != 0.3 || wh.remote != true { 57 t.Error("bad wh update") 58 } 59 // Try updating with a higher health but remote not set. 60 wh.updateWorstIgnoredHealth(1.01, false) 61 if wh.health != 0.3 || wh.remote != true { 62 t.Error("bad wh update") 63 } 64 // Try updating with a higher health and remote set. 65 wh.updateWorstIgnoredHealth(1.01, true) 66 if wh.health != 1.01 || wh.remote != true { 67 t.Error("bad wh update") 68 } 69 } 70 71 // TestWIHCanSkip checks the logic of the canSkip method. 72 func TestWIHCanSkip(t *testing.T) { 73 t.Parallel() 74 75 // The target is not set, nothing should be skippable unless it is below the 76 // repair threshold. 77 wh := worstIgnoredHealth{ 78 health: 0.3, 79 remote: false, 80 81 nextDirHealth: 0.5, 82 nextDirRemote: false, 83 } 84 // TEST GROUP A 85 if !wh.canSkip(0.2, true) { 86 t.Error("Bad skip") 87 } 88 if !wh.canSkip(0.2, true) { 89 t.Error("Bad skip") 90 } 91 if wh.canSkip(0.28, false) { 92 t.Error("Bad skip") 93 } 94 if wh.canSkip(2.1, false) { 95 t.Error("Bad skip") 96 } 97 if wh.canSkip(2.1, true) { 98 t.Error("Bad skip") 99 } 100 if wh.canSkip(0.3, true) { 101 t.Error("Bad skip") 102 } 103 if wh.canSkip(0.3, false) { 104 t.Error("Bad skip") 105 } 106 107 // Set the target, now should be possible to skip non-remote chunks that are 108 // better than nextDirHealth. 109 // 110 // TEST GROUP B 111 wh.target = targetUnstuckChunks 112 if !wh.canSkip(0.1, true) { 113 t.Error("Bad skip") 114 } 115 if !wh.canSkip(0.1, false) { 116 t.Error("Bad skip") 117 } 118 if wh.canSkip(0.4, true) { 119 t.Error("Bad skip") 120 } 121 if !wh.canSkip(0.4, false) { 122 t.Error("Bad skip") 123 } 124 if wh.canSkip(2.1, false) { 125 t.Error("Bad skip") 126 } 127 if wh.canSkip(2.1, true) { 128 t.Error("Bad skip") 129 } 130 if wh.canSkip(0.3, true) { 131 t.Error("Bad skip") 132 } 133 134 // Set the remote health to true. Should be able to skip anything false, and 135 // anything under 0.3 health. 136 // 137 // TEST GROUP C 138 wh.remote = true 139 if !wh.canSkip(0.1, true) { 140 t.Error("Bad skip") 141 } 142 if !wh.canSkip(0.1, false) { 143 t.Error("Bad skip") 144 } 145 if !wh.canSkip(0.28, true) { 146 t.Error("Bad skip") 147 } 148 if !wh.canSkip(0.28, false) { 149 t.Error("Bad skip") 150 } 151 if !wh.canSkip(2.1, false) { 152 t.Error("Bad skip") 153 } 154 if wh.canSkip(2.1, true) { 155 t.Error("Bad skip") 156 } 157 if wh.canSkip(0.31, true) { 158 t.Error("Bad skip") 159 } 160 if !wh.canSkip(0.3, false) { 161 t.Error("Bad skip") 162 } 163 164 // Set the next dir to remote, now should be possible to skip remote chunks 165 // that are better than wh, and non-remote chunks at any health. 166 // 167 // TEST GROUP D 168 wh.nextDirRemote = true 169 if !wh.canSkip(0.1, true) { 170 t.Error("Bad skip") 171 } 172 if !wh.canSkip(0.1, false) { 173 t.Error("Bad skip") 174 } 175 if !wh.canSkip(0.48, true) { 176 t.Error("Bad skip") 177 } 178 if !wh.canSkip(0.4, false) { 179 t.Error("Bad skip") 180 } 181 if !wh.canSkip(2.1, false) { 182 t.Error("Bad skip") 183 } 184 if wh.canSkip(2.1, true) { 185 t.Error("Bad skip") 186 } 187 if !wh.canSkip(0.3, true) { 188 t.Error("Bad skip") 189 } 190 191 // Flip the roles of next dir and worst ignored from test group B for test 192 // group E. Results should be the same. 193 // 194 // TEST GROUP E 195 wh.health = 0.5 196 wh.remote = false 197 wh.nextDirHealth = 0.3 198 wh.nextDirRemote = false 199 if !wh.canSkip(0.1, true) { 200 t.Error("Bad skip") 201 } 202 if !wh.canSkip(0.1, false) { 203 t.Error("Bad skip") 204 } 205 if wh.canSkip(0.4, true) { 206 t.Error("Bad skip") 207 } 208 if !wh.canSkip(0.4, false) { 209 t.Error("Bad skip") 210 } 211 if wh.canSkip(2.1, false) { 212 t.Error("Bad skip") 213 } 214 if wh.canSkip(2.1, true) { 215 t.Error("Bad skip") 216 } 217 if wh.canSkip(0.3, true) { 218 t.Error("Bad skip") 219 } 220 221 // Flip the roles of next dir and worst ignored from test group C for test 222 // group F. Results should be the same. 223 // 224 // TEST GROUP F 225 wh.health = 0.5 226 wh.remote = false 227 wh.nextDirHealth = 0.3 228 wh.nextDirRemote = true 229 if !wh.canSkip(0.1, true) { 230 t.Error("Bad skip") 231 } 232 if !wh.canSkip(0.1, false) { 233 t.Error("Bad skip") 234 } 235 if !wh.canSkip(0.28, true) { 236 t.Error("Bad skip") 237 } 238 if !wh.canSkip(0.28, false) { 239 t.Error("Bad skip") 240 } 241 if !wh.canSkip(2.1, false) { 242 t.Error("Bad skip") 243 } 244 if wh.canSkip(2.1, true) { 245 t.Error("Bad skip") 246 } 247 if wh.canSkip(0.31, true) { 248 t.Error("Bad skip") 249 } 250 if !wh.canSkip(0.3, false) { 251 t.Error("Bad skip") 252 } 253 254 // Flip the roles of next dir and worst ignored from test group D for test 255 // group G. Results should be the same. 256 // 257 // TEST GROUP G 258 wh.health = 0.5 259 wh.remote = true 260 wh.nextDirHealth = 0.3 261 wh.nextDirRemote = true 262 if !wh.canSkip(0.1, true) { 263 t.Error("Bad skip") 264 } 265 if !wh.canSkip(0.1, false) { 266 t.Error("Bad skip") 267 } 268 if !wh.canSkip(0.48, true) { 269 t.Error("Bad skip") 270 } 271 if !wh.canSkip(0.4, false) { 272 t.Error("Bad skip") 273 } 274 if !wh.canSkip(2.1, false) { 275 t.Error("Bad skip") 276 } 277 if wh.canSkip(2.1, true) { 278 t.Error("Bad skip") 279 } 280 if !wh.canSkip(0.3, true) { 281 t.Error("Bad skip") 282 } 283 }