gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/dirupdatebatcher_test.go (about) 1 package renter 2 3 import ( 4 "io/ioutil" 5 "testing" 6 7 "gitlab.com/SkynetLabs/skyd/persist" 8 "gitlab.com/SkynetLabs/skyd/skymodules" 9 ) 10 11 // TestDirUpdateBatcherQueue verifies the callQueueDirUpdate method of the 12 // dirUpdateBatcher. 13 func TestDirUpdateBatcherQueue(t *testing.T) { 14 // Check that the maps of the dub are being allocated correctly. 15 dub := new(dirUpdateBatcher) 16 dub.staticRenter = new(Renter) 17 dub.nextBatch = new(dirUpdateBatch) 18 dub.nextBatch.completeChan = make(chan struct{}) 19 priorChan := make(chan struct{}) 20 close(priorChan) 21 dub.nextBatch.priorCompleteChan = priorChan 22 dub.nextBatch.dirUpdateBatchDeps.renter = new(Renter) 23 logger, err := persist.NewLogger(ioutil.Discard) 24 if err != nil { 25 t.Fatal(err) 26 } 27 dub.staticRenter.staticLog = logger 28 dub.nextBatch.dirUpdateBatchDeps.renter.staticLog = logger 29 dub.nextBatch.dirUpdateBatchDeps.executeTracker.executedDirs = make(map[skymodules.SiaPath]struct{}) 30 dub.nextBatch.dirUpdateBatchDeps.executeTracker.lastDepth = 10e3 31 depthFive, err := skymodules.NewSiaPath("one/two/three/four/five") 32 if err != nil { 33 t.Fatal(err) 34 } 35 dub.callQueueDirUpdate(depthFive) 36 if len(dub.nextBatch.batchSet) != 6 { 37 t.Error("bad") 38 } 39 for i := 0; i < 5; i++ { 40 if len(dub.nextBatch.batchSet[i]) != 0 { 41 t.Error(len(dub.nextBatch.batchSet[i])) 42 } 43 } 44 if len(dub.nextBatch.batchSet[5]) != 1 { 45 t.Error(len(dub.nextBatch.batchSet[5])) 46 } 47 // Try adding the same directory again, this should be a no-op. 48 dub.callQueueDirUpdate(depthFive) 49 if len(dub.nextBatch.batchSet) != 6 { 50 t.Error("bad") 51 } 52 for i := 0; i < 5; i++ { 53 if len(dub.nextBatch.batchSet[i]) != 0 { 54 t.Error(len(dub.nextBatch.batchSet[i])) 55 } 56 } 57 if len(dub.nextBatch.batchSet[5]) != 1 { 58 t.Error(len(dub.nextBatch.batchSet[5])) 59 } 60 61 // Try adding a directory at the same level. 62 depthFive2, err := skymodules.NewSiaPath("one/two/three/four/fiveeee") 63 if err != nil { 64 t.Fatal(err) 65 } 66 dub.callQueueDirUpdate(depthFive2) 67 if len(dub.nextBatch.batchSet) != 6 { 68 t.Error("bad") 69 } 70 for i := 0; i < 5; i++ { 71 if len(dub.nextBatch.batchSet[i]) != 0 { 72 t.Error(len(dub.nextBatch.batchSet[i])) 73 } 74 } 75 if len(dub.nextBatch.batchSet[5]) != 2 { 76 t.Error(len(dub.nextBatch.batchSet[5])) 77 } 78 // Try adding the same directory again, this should be a no-op. 79 dub.callQueueDirUpdate(depthFive2) 80 if len(dub.nextBatch.batchSet) != 6 { 81 t.Error("bad") 82 } 83 for i := 0; i < 5; i++ { 84 if len(dub.nextBatch.batchSet[i]) != 0 { 85 t.Error(len(dub.nextBatch.batchSet[i])) 86 } 87 } 88 if len(dub.nextBatch.batchSet[5]) != 2 { 89 t.Error(len(dub.nextBatch.batchSet[5])) 90 } 91 92 // Try adding a directory at a deeper level 93 depthSix, err := skymodules.NewSiaPath("one/two/three/four/five/six") 94 if err != nil { 95 t.Fatal(err) 96 } 97 dub.callQueueDirUpdate(depthSix) 98 if len(dub.nextBatch.batchSet) != 7 { 99 t.Error("bad") 100 } 101 for i := 0; i < 5; i++ { 102 if len(dub.nextBatch.batchSet[i]) != 0 { 103 t.Error(len(dub.nextBatch.batchSet[i])) 104 } 105 } 106 if len(dub.nextBatch.batchSet[5]) != 2 { 107 t.Error(len(dub.nextBatch.batchSet[5])) 108 } 109 if len(dub.nextBatch.batchSet[6]) != 1 { 110 t.Error(len(dub.nextBatch.batchSet[6])) 111 } 112 113 // Try adding a directory at a less deep level. 114 depthFour, err := skymodules.NewSiaPath("one/two/three/four") 115 if err != nil { 116 t.Fatal(err) 117 } 118 dub.callQueueDirUpdate(depthFour) 119 if len(dub.nextBatch.batchSet) != 7 { 120 t.Error("bad") 121 } 122 for i := 0; i < 4; i++ { 123 if len(dub.nextBatch.batchSet[i]) != 0 { 124 t.Error(len(dub.nextBatch.batchSet[i])) 125 } 126 } 127 if len(dub.nextBatch.batchSet[4]) != 1 { 128 t.Error(len(dub.nextBatch.batchSet[5])) 129 } 130 if len(dub.nextBatch.batchSet[5]) != 2 { 131 t.Error(len(dub.nextBatch.batchSet[5])) 132 } 133 if len(dub.nextBatch.batchSet[6]) != 1 { 134 t.Error(len(dub.nextBatch.batchSet[6])) 135 } 136 137 // Try adding a directory at disjoint level. 138 disjoint, err := skymodules.NewSiaPath("oneeee/two/three/four/five") 139 if err != nil { 140 t.Fatal(err) 141 } 142 dub.callQueueDirUpdate(disjoint) 143 if len(dub.nextBatch.batchSet) != 7 { 144 t.Error("bad") 145 } 146 for i := 0; i < 4; i++ { 147 if len(dub.nextBatch.batchSet[i]) != 0 { 148 t.Error(len(dub.nextBatch.batchSet[i])) 149 } 150 } 151 if len(dub.nextBatch.batchSet[4]) != 1 { 152 t.Error(len(dub.nextBatch.batchSet[5])) 153 } 154 if len(dub.nextBatch.batchSet[5]) != 3 { 155 t.Error(len(dub.nextBatch.batchSet[5])) 156 } 157 if len(dub.nextBatch.batchSet[6]) != 1 { 158 t.Error(len(dub.nextBatch.batchSet[6])) 159 } 160 161 // Set up the dependency injection to track calls to execute, then call 162 // execute and verify that everything we wanted got called. 163 dub.nextBatch.dirUpdateBatchDeps.testSwitch = "testExecute" 164 dub.nextBatch.managedExecute() 165 executedDirs := dub.nextBatch.dirUpdateBatchDeps.executeTracker.executedDirs 166 if len(executedDirs) != 13 { 167 t.Error("wrong number of directories executed") 168 } 169 // Check for root. 170 rootSP := skymodules.RootSiaPath() 171 _, exists := executedDirs[rootSP] 172 if !exists { 173 t.Error("root directory not scanned") 174 } 175 // Check for the other 12 dirs. 176 expectedDirs := []string{ 177 "one/two/three/four/five/six", 178 "one/two/three/four/five", 179 "one/two/three/four/fiveeee", 180 "oneeee/two/three/four/five", 181 "one/two/three/four", 182 "oneeee/two/three/four", 183 "one/two/three", 184 "oneeee/two/three", 185 "one/two", 186 "oneeee/two", 187 "one", 188 "oneeee", 189 } 190 for _, dirStr := range expectedDirs { 191 sp, err := skymodules.NewSiaPath(dirStr) 192 if err != nil { 193 t.Fatal(err) 194 } 195 _, exists := executedDirs[sp] 196 if !exists { 197 t.Error(sp) 198 } 199 } 200 }