Best Testkube code snippet using executor.Run
queued.go
Source:queued.go
...25 TypeThenAccept26 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»ææ¶è27 TypeThenAcceptAsync28 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸å
³å¿ä¸ä¸æ¥ç»æ29 TypeThenRun30 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸å
³å¿ä¸ä¸æ¥ç»æ31 TypeThenRunAsync32 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è½¬ååè¿å33 TypeThenCombine34 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è½¬ååè¿å35 TypeThenCombineAsync36 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è¿è¡æ¶è37 TypeThenAcceptBoth38 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è¿è¡æ¶è39 TypeThenAcceptBothAsync40 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageé½å®æåæ§è¡41 TypeRunAfterBoth42 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageé½å®æåæ§è¡43 TypeRunAfterBothAsync44 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡è½¬å45 TypeApplyToEither46 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡è½¬å47 TypeApplyToEitherAsync48 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡æ¶è49 TypeAcceptEither50 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡æ¶è51 TypeAcceptEitherAsync52 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageä»»æä¸ä¸ªå®æåæ§è¡æä½53 TypeRunAfterEither54 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageä»»æä¸ä¸ªå®æåæ§è¡æä½55 TypeRunAfterEitherAsync56 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä½¿ç¨ä¸ä¸é¶æ®µç»æ转å为æ°çCompletionStage57 TypeThenCompose58 // å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä½¿ç¨ä¸ä¸é¶æ®µç»æ转å为æ°çCompletionStage59 TypeThenComposeAsync60 // æè·é¶æ®µå¼å¸¸ï¼è¿åè¡¥å¿ç»æ61 TypeExceptionally62 // é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,注æä¼ç»§ç»ä¼ épanic63 TypeWhenComplete64 // é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,注æä¼ç»§ç»ä¼ épanic65 TypeWhenCompleteAsync66 // é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,并转åç»æ67 TypeHandle68 // é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,并转åç»æ69 TypeHandleAsync70)71const (72 statusNormal = iota73 statusCancel74)75type stage struct {76 other completable.CompletionStage77 value interface{}78 fn interface{}79 executor executor.Executor80 cfType Type81}82type queuedCompletableFuture struct {83 origin completable.CompletionStage84 result completable.CompletionStage85 queueLocker sync.Mutex86 queue *list.List87 status int3288 once sync.Once89 interrupter interrupter90 interLocker sync.Mutex91}92type interrupter func(c completable.CompletionStage) bool93func (cf *queuedCompletableFuture) enqueue(stage *stage) {94 cf.queueLocker.Lock()95 defer cf.queueLocker.Unlock()96 if atomic.LoadInt32(&cf.status) == statusNormal {97 cf.queue.PushBack(stage)98 }99}100// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼è¿è¡ç±»ååæ¢101// Paramï¼åæ°å½æ°ï¼f func(o TYPE1) TYPE2åæ°ä¸ºä¸é¶æ®µç»æï¼è¿å为å¤çåçè¿åå¼102// Returnï¼æ°çCompletionStage103func (cf *queuedCompletableFuture) ThenApply(applyFunc interface{}) completable.CompletionStage {104 stage := &stage{105 cfType: TypeThenApply,106 fn: applyFunc,107 }108 cf.enqueue(stage)109 return cf110}111// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°112// Paramï¼åæ°å½æ°ï¼f func(o TYPE1) TYPE2åæ°ä¸ºä¸é¶æ®µç»æï¼è¿å为å¤çåçè¿åå¼113// Returnï¼æ°çCompletionStage114func (cf *queuedCompletableFuture) ThenApplyAsync(applyFunc interface{}, executor ...executor.Executor) completable.CompletionStage {115 stage := &stage{116 cfType: TypeThenApplyAsync,117 fn: applyFunc,118 executor: cf.chooseExecutor(executor...),119 }120 cf.enqueue(stage)121 return cf122}123// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»ææ¶è124// Paramï¼åæ°å½æ°ï¼f func(o TYPE)åæ°ä¸ºä¸é¶æ®µç»æ125// Returnï¼æ°çCompletionStage126func (cf *queuedCompletableFuture) ThenAccept(acceptFunc interface{}) completable.CompletionStage {127 stage := &stage{128 cfType: TypeThenAccept,129 fn: acceptFunc,130 }131 cf.enqueue(stage)132 return cf133}134// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»ææ¶è135// Paramï¼åæ°å½æ°ï¼f func(o TYPE)åæ°ä¸ºä¸é¶æ®µç»æ136// Returnï¼æ°çCompletionStage137func (cf *queuedCompletableFuture) ThenAcceptAsync(acceptFunc interface{}, executor ...executor.Executor) completable.CompletionStage {138 stage := &stage{139 cfType: TypeThenAcceptAsync,140 fn: acceptFunc,141 executor: cf.chooseExecutor(executor...),142 }143 cf.enqueue(stage)144 return cf145}146// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸å
³å¿ä¸ä¸æ¥ç»æ147// Paramï¼åæ°å½æ°: f func()148// Returnï¼æ°çCompletionStage149func (cf *queuedCompletableFuture) ThenRun(runnable interface{}) completable.CompletionStage {150 stage := &stage{151 cfType: TypeThenRun,152 fn: runnable,153 }154 cf.enqueue(stage)155 return cf156}157// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸å
³å¿ä¸ä¸æ¥ç»æ158// Paramï¼åæ°å½æ°: f func()159// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 160// Returnï¼æ°çCompletionStage161func (cf *queuedCompletableFuture) ThenRunAsync(runnable interface{}, executor ...executor.Executor) completable.CompletionStage {162 stage := &stage{163 cfType: TypeThenRunAsync,164 fn: runnable,165 executor: cf.chooseExecutor(executor...),166 }167 cf.enqueue(stage)168 return cf169}170// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è½¬ååè¿å171// Paramï¼otherï¼å½è¯¥CompletionStageä¹è¿ååè¿è¡ç»å转å172// Paramï¼åæ°å½æ°ï¼combineFunc func(TYPE1, TYPE2) TYPE3åæ°ä¸ºä¸¤ä¸ªCompletionStageçç»æï¼è¿å转åç»æ173// Returnï¼æ°çCompletionStage174func (cf *queuedCompletableFuture) ThenCombine(other completable.CompletionStage, combineFunc interface{}) completable.CompletionStage {175 cf.checkType(other)176 stage := &stage{177 cfType: TypeThenCombine,178 fn: combineFunc,179 other: other,180 }181 cf.enqueue(stage)182 return cf183}184// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è½¬ååè¿å185// Paramï¼otherï¼å½è¯¥CompletionStageä¹è¿ååè¿è¡ç»å转å186// Paramï¼åæ°å½æ°ï¼combineFunc func(TYPE1, TYPE2) TYPE3åæ°ä¸ºä¸¤ä¸ªCompletionStageçç»æï¼è¿å转åç»æ187// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 188// Returnï¼æ°çCompletionStage189func (cf *queuedCompletableFuture) ThenCombineAsync(other completable.CompletionStage, combineFunc interface{}, executor ...executor.Executor) completable.CompletionStage {190 cf.checkType(other)191 stage := &stage{192 cfType: TypeThenCombineAsync,193 fn: combineFunc,194 other: other,195 executor: cf.chooseExecutor(executor...),196 }197 cf.enqueue(stage)198 return cf199}200// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è¿è¡æ¶è201// Paramï¼otherï¼å½è¯¥CompletionStageä¹è¿ååè¿è¡æ¶è202// Paramï¼åæ°å½æ°ï¼acceptFunc func(TYPE1, TYPE2) åæ°ä¸ºä¸¤ä¸ªCompletionStageçç»æ203// Returnï¼æ°çCompletionStage204func (cf *queuedCompletableFuture) ThenAcceptBoth(other completable.CompletionStage, acceptFunc interface{}) completable.CompletionStage {205 cf.checkType(other)206 stage := &stage{207 cfType: TypeThenAcceptBoth,208 fn: acceptFunc,209 other: other,210 }211 cf.enqueue(stage)212 return cf213}214// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ç»å两个CompletionStageçç»æï¼è¿è¡æ¶è215// Paramï¼otherï¼å½è¯¥CompletionStageä¹è¿ååè¿è¡æ¶è216// Paramï¼åæ°å½æ°ï¼acceptFunc func(TYPE1, TYPE2) åæ°ä¸ºä¸¤ä¸ªCompletionStageçç»æ217// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 218// Returnï¼æ°çCompletionStage219func (cf *queuedCompletableFuture) ThenAcceptBothAsync(220 other completable.CompletionStage, acceptFunc interface{}, executor ...executor.Executor) completable.CompletionStage {221 cf.checkType(other)222 stage := &stage{223 cfType: TypeThenAcceptBothAsync,224 fn: acceptFunc,225 other: other,226 executor: cf.chooseExecutor(executor...),227 }228 cf.enqueue(stage)229 return cf230}231// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageé½å®æåæ§è¡232// Paramï¼otherï¼å½è¯¥CompletionStageä¹å®æåæ§è¡åæ°å½æ°233// Paramï¼åæ°å½æ° runnable func()234// Returnï¼æ°çCompletionStage235func (cf *queuedCompletableFuture) RunAfterBoth(other completable.CompletionStage, runnable interface{}) completable.CompletionStage {236 cf.checkType(other)237 stage := &stage{238 cfType: TypeRunAfterBoth,239 fn: runnable,240 other: other,241 }242 cf.enqueue(stage)243 return cf244}245// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageé½å®æåæ§è¡246// Paramï¼otherï¼å½è¯¥CompletionStageä¹å®æåæ§è¡åæ°å½æ°247// Paramï¼åæ°å½æ° runnable func()248// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 249// Returnï¼æ°çCompletionStage250func (cf *queuedCompletableFuture) RunAfterBothAsync(other completable.CompletionStage, runnable interface{}, executor ...executor.Executor) completable.CompletionStage {251 cf.checkType(other)252 stage := &stage{253 cfType: TypeRunAfterBothAsync,254 fn: runnable,255 other: other,256 executor: cf.chooseExecutor(executor...),257 }258 cf.enqueue(stage)259 return cf260}261// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡è½¬å262// Paramï¼otherï¼ä¸è¯¥CompletionStageæ¯è¾ï¼ç¨å
å®æçç»æè¿è¡è½¬åï¼æ³¨æ两个CompletionStageçè¿åç»æç±»åå¿
é¡»ç¸å263// Paramï¼åæ°å½æ° f func(o Type1) Type2åæ°ä¸ºå
å®æçCompletionStageçç»æï¼è¿å转åç»æ264// Returnï¼æ°çCompletionStage265func (cf *queuedCompletableFuture) ApplyToEither(other completable.CompletionStage, applyFunc interface{}) completable.CompletionStage {266 cf.checkType(other)267 stage := &stage{268 cfType: TypeApplyToEither,269 fn: applyFunc,270 other: other,271 }272 cf.enqueue(stage)273 return cf274}275// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡è½¬å276// Paramï¼otherï¼ä¸è¯¥CompletionStageæ¯è¾ï¼ç¨å
å®æçç»æè¿è¡è½¬åï¼æ³¨æ两个CompletionStageçè¿åç»æç±»åå¿
é¡»ç¸å277// Paramï¼åæ°å½æ° f func(o Type1) Type2åæ°ä¸ºå
å®æçCompletionStageçç»æï¼è¿å转åç»æ278// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 279// Returnï¼æ°çCompletionStage280func (cf *queuedCompletableFuture) ApplyToEitherAsync(other completable.CompletionStage, applyFunc interface{}, executor ...executor.Executor) completable.CompletionStage {281 cf.checkType(other)282 stage := &stage{283 cfType: TypeApplyToEitherAsync,284 fn: applyFunc,285 other: other,286 executor: cf.chooseExecutor(executor...),287 }288 cf.enqueue(stage)289 return cf290}291// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡æ¶è292// Paramï¼otherï¼ä¸è¯¥CompletionStageæ¯è¾ï¼ç¨å
å®æçç»æè¿è¡æ¶èï¼æ³¨æ两个CompletionStageçè¿åç»æç±»åå¿
é¡»ç¸å293// Paramï¼åæ°å½æ° f func(o Type)åæ°ä¸ºå
å®æçCompletionStageçç»æ294// Returnï¼æ°çCompletionStage295func (cf *queuedCompletableFuture) AcceptEither(other completable.CompletionStage, acceptFunc interface{}) completable.CompletionStage {296 cf.checkType(other)297 stage := &stage{298 cfType: TypeAcceptEither,299 fn: acceptFunc,300 other: other,301 }302 cf.enqueue(stage)303 return cf304}305// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStage使ç¨å
å®æçç»æè¿è¡æ¶è306// Paramï¼otherï¼ä¸è¯¥CompletionStageæ¯è¾ï¼ç¨å
å®æçç»æè¿è¡æ¶èï¼æ³¨æ两个CompletionStageçè¿åç»æç±»åå¿
é¡»ç¸å307// Paramï¼åæ°å½æ° f func(o Type)åæ°ä¸ºå
å®æçCompletionStageçç»æ308// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 309// Returnï¼æ°çCompletionStage310func (cf *queuedCompletableFuture) AcceptEitherAsync(other completable.CompletionStage, acceptFunc interface{}, executor ...executor.Executor) completable.CompletionStage {311 cf.checkType(other)312 stage := &stage{313 cfType: TypeAcceptEitherAsync,314 fn: acceptFunc,315 other: other,316 executor: cf.chooseExecutor(executor...),317 }318 cf.enqueue(stage)319 return cf320}321// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageä»»æä¸ä¸ªå®æåæ§è¡æä½322// Paramï¼otherï¼ä¸è¯¥CompletionStageæ¯è¾ï¼ä»»æä¸ä¸ªå®æåæ§è¡æä½ï¼æ³¨æ两个CompletionStageçè¿åç»æç±»åå¿
é¡»ç¸å323// Paramï¼åæ°å½æ°324// Returnï¼æ°çCompletionStage325func (cf *queuedCompletableFuture) RunAfterEither(other completable.CompletionStage, runnable interface{}) completable.CompletionStage {326 cf.checkType(other)327 stage := &stage{328 cfType: TypeRunAfterEither,329 fn: runnable,330 other: other,331 }332 cf.enqueue(stage)333 return cf334}335// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä¸¤ä¸ªCompletionStageä»»æä¸ä¸ªå®æåæ§è¡æä½336// Paramï¼otherï¼ä¸è¯¥CompletionStageæ¯è¾ï¼ä»»æä¸ä¸ªå®æåæ§è¡æä½ï¼æ³¨æ两个CompletionStageçè¿åç»æç±»åå¿
é¡»ç¸å337// Paramï¼åæ°å½æ°338// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 339// Returnï¼æ°çCompletionStage340func (cf *queuedCompletableFuture) RunAfterEitherAsync(other completable.CompletionStage, runnable interface{}, executor ...executor.Executor) completable.CompletionStage {341 cf.checkType(other)342 stage := &stage{343 cfType: TypeRunAfterEitherAsync,344 fn: runnable,345 other: other,346 executor: cf.chooseExecutor(executor...),347 }348 cf.enqueue(stage)349 return cf350}351// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä½¿ç¨ä¸ä¸é¶æ®µç»æ转å为æ°çCompletionStage352// Paramï¼åæ°å½æ°ï¼f func(o TYPE) completable.CompletionStage åæ°ï¼ä¸ä¸é¶æ®µç»æï¼è¿åæ°çCompletionStage353// Returnï¼æ°çCompletionStage354func (cf *queuedCompletableFuture) ThenCompose(f interface{}) completable.CompletionStage {355 stage := &stage{356 cfType: TypeThenCompose,357 fn: f,358 }359 cf.enqueue(stage)360 return cf361}362// å½é¶æ®µæ£å¸¸å®ææ¶æ§è¡åæ°å½æ°ï¼ä½¿ç¨ä¸ä¸é¶æ®µç»æ转å为æ°çCompletionStage363// Paramï¼åæ°å½æ°ï¼f func(o TYPE) completable.CompletionStage åæ°ï¼ä¸ä¸é¶æ®µç»æï¼è¿åæ°çCompletionStage364// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 365// Returnï¼æ°çCompletionStage366func (cf *queuedCompletableFuture) ThenComposeAsync(f interface{}, executor ...executor.Executor) completable.CompletionStage {367 stage := &stage{368 cfType: TypeThenComposeAsync,369 fn: f,370 executor: cf.chooseExecutor(executor...),371 }372 cf.enqueue(stage)373 return cf374}375// æè·é¶æ®µå¼å¸¸ï¼è¿åè¡¥å¿ç»æ376// Paramï¼f func(o interface{}) TYPEåæ°å½æ°ï¼åæ°ï¼æè·çpanicåæ°ï¼è¿åè¡¥å¿çç»æ377// Returnï¼æ°çCompletionStage378func (cf *queuedCompletableFuture) Exceptionally(f interface{}) completable.CompletionStage {379 stage := &stage{380 cfType: TypeExceptionally,381 fn: f,382 }383 cf.enqueue(stage)384 return cf385}386// é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,注æä¼ç»§ç»ä¼ épanic387// Paramï¼åæ°å½æ°ï¼f func(result Type, panic interface{}) åæ°resultï¼ç»æï¼åæ°panicï¼å¼å¸¸388// Returnï¼æ°çCompletionStage389func (cf *queuedCompletableFuture) WhenComplete(f interface{}) completable.CompletionStage {390 stage := &stage{391 cfType: TypeWhenComplete,392 fn: f,393 }394 cf.enqueue(stage)395 return cf396}397// é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,注æä¼ç»§ç»ä¼ épanic398// Paramï¼åæ°å½æ°ï¼f func(result Type, panic interface{}) åæ°resultï¼ç»æï¼åæ°panicï¼å¼å¸¸399// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 400// Returnï¼æ°çCompletionStage401func (cf *queuedCompletableFuture) WhenCompleteAsync(f interface{}, executor ...executor.Executor) completable.CompletionStage {402 stage := &stage{403 cfType: TypeWhenCompleteAsync,404 fn: f,405 executor: cf.chooseExecutor(executor...),406 }407 cf.enqueue(stage)408 return cf409}410// é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,并转åç»æ411// Paramï¼åæ°å½æ°ï¼f func(result TYPE1, panic interface{}) TYPE2 åæ°resultï¼ç»æï¼åæ°panicï¼å¼å¸¸ï¼è¿åï¼è½¬åçç»æ412// Returnï¼æ°çCompletionStage413func (cf *queuedCompletableFuture) Handle(f interface{}) completable.CompletionStage {414 stage := &stage{415 cfType: TypeHandle,416 fn: f,417 }418 cf.enqueue(stage)419 return cf420}421// é¶æ®µæ§è¡æ¶è·å¾ç»ææè
panic,并转åç»æ422// Paramï¼åæ°å½æ°ï¼f func(result TYPE1, panic interface{}) TYPE2 åæ°resultï¼ç»æï¼åæ°panicï¼å¼å¸¸ï¼è¿åï¼è½¬åçç»æ423// Paramï¼Executor: å¼æ¥æ§è¡çåç¨æ± ï¼å¦æä¸å¡«å使ç¨å
ç½®é»è®¤åç¨æ± 424// Returnï¼æ°çCompletionStage425func (cf *queuedCompletableFuture) HandleAsync(f interface{}, executor ...executor.Executor) completable.CompletionStage {426 stage := &stage{427 cfType: TypeHandleAsync,428 fn: f,429 executor: cf.chooseExecutor(executor...),430 }431 cf.enqueue(stage)432 return cf433}434// ç»äºgetçå¼å¹¶æ£å¸¸ç»æ435func (cf *queuedCompletableFuture) Complete(v interface{}) error {436 cf.setInterrupter(func(c completable.CompletionStage) bool {437 c.Complete(v)438 return true439 })440 return cf.origin.Complete(v)441}442// åépanicï¼å¼å¸¸ç»æ443func (cf *queuedCompletableFuture) CompleteExceptionally(v interface{}) error {444 cf.setInterrupter(func(c completable.CompletionStage) bool {445 c.CompleteExceptionally(v)446 return true447 })448 return cf.origin.CompleteExceptionally(v)449}450// åæ¶å¹¶ææstageé¾ï¼éåºä»»å¡451// å¦æä»»å¡å·²å®æè¿åfalseï¼æååæ¶è¿åtrue452func (cf *queuedCompletableFuture) Cancel() bool {453 cf.changeStatus(statusCancel)454 cf.setInterrupter(func(c completable.CompletionStage) bool {455 c.Cancel()456 return true457 })458 return cf.origin.Cancel()459}460func (cf *queuedCompletableFuture) changeStatus(status int32) {461 atomic.StoreInt32(&cf.status, status)462}463// æ¯å¦å¨å®æå被åæ¶464func (cf *queuedCompletableFuture) IsCancelled() bool {465 return cf.origin.IsCancelled()466}467// æ¯å¦ä»»å¡å®æ468// å½ä»»å¡æ£å¸¸å®æï¼è¢«åæ¶ï¼æåºå¼å¸¸é½ä¼è¿åtrue469func (cf *queuedCompletableFuture) IsDone() bool {470 return cf.join().IsDone()471}472// çå¾
并è·å¾ä»»å¡æ§è¡ç»æ473// Paramï¼ result ç®æ ç»æï¼å¿
须为åç±»åçæé474// Paramï¼ timeout çå¾
è¶
æ¶æ¶é´ï¼å¦æä¸ä¼ å¼åä¸ç´çå¾
475func (cf *queuedCompletableFuture) Get(result interface{}, timeout ...time.Duration) error {476 return cf.join().Get(result, timeout...)477}478func CompletedFuture(value interface{}) (retCf completable.CompletionStage) {479 ret := &queuedCompletableFuture{480 origin: completable.CompletedFuture(value),481 queue: list.New(),482 }483 return ret484}485func SupplyAsync(f interface{}, executor ...executor.Executor) (retCf completable.CompletionStage) {486 ret := &queuedCompletableFuture{487 origin: completable.SupplyAsync(f, executor...),488 queue: list.New(),489 }490 return ret491}492func RunAsync(f func(), executor ...executor.Executor) (retCf completable.CompletionStage) {493 ret := &queuedCompletableFuture{494 origin: completable.RunAsync(f, executor...),495 queue: list.New(),496 }497 return ret498}499func AllOf(cfs ...completable.CompletionStage) (retCf completable.CompletionStage) {500 if len(cfs) == 0 {501 return nil502 }503 origins := make([]completable.CompletionStage, len(cfs))504 for i := range cfs {505 if v, ok := cfs[i].(*queuedCompletableFuture); ok {506 origins[i] = v.join()507 } else {508 origins[i] = cfs[i]509 }510 }511 ret := &queuedCompletableFuture{512 origin: completable.AllOf(origins...),513 queue: list.New(),514 }515 return ret516}517func AnyOf(cfs ...completable.CompletionStage) (retCf completable.CompletionStage) {518 if len(cfs) == 0 {519 return nil520 }521 cs, _ := completable.GetAny(context.Background(), cfs...)522 return cs523}524func (cf *queuedCompletableFuture) JoinCompletionStage(ctx context.Context) completable.CompletionStage {525 return cf.join()526}527func (cf *queuedCompletableFuture) join() completable.CompletionStage {528 cf.once.Do(func() {529 cf.result = cf.convertOrigin()530 })531 return cf.result532}533func (cf *queuedCompletableFuture) chooseExecutor(executor ...executor.Executor) executor.Executor {534 if len(executor) > 0 {535 return executor[0]536 }537 return nil538}539func (cf *queuedCompletableFuture) checkType(completable completable.CompletionStage) {540 checkType(completable)541}542func checkType(completable completable.CompletionStage) {543 if _, ok := completable.(*queuedCompletableFuture); !ok {544 panic("expect queuedCompletableFuture, not match")545 }546}547func toQueued(completable completable.CompletionStage) *queuedCompletableFuture {548 if v, ok := completable.(*queuedCompletableFuture); ok {549 return v550 }551 panic("expect queuedCompletableFuture, not match")552}553func (cf *queuedCompletableFuture) setInterrupter(interrupter interrupter) {554 cf.interLocker.Lock()555 defer cf.interLocker.Unlock()556 cf.interrupter = interrupter557}558func (cf *queuedCompletableFuture) getInterrupter() interrupter {559 cf.interLocker.Lock()560 defer cf.interLocker.Unlock()561 return cf.interrupter562}563func runStage(completable completable.CompletionStage) completable.CompletionStage {564 if v, ok := completable.(*queuedCompletableFuture); ok {565 return v.convertOrigin()566 }567 panic("expect queuedCompletableFuture, not match")568}569func (cf *queuedCompletableFuture) convertOrigin() completable.CompletionStage {570 cf.queueLocker.Lock()571 defer cf.queueLocker.Unlock()572 cur := cf.origin573 for elem := cf.queue.Front(); elem != nil; elem = elem.Next() {574 inter := cf.getInterrupter()575 if inter != nil {576 inter(cur)577 }578 stage := elem.Value.(*stage)579 switch stage.cfType {580 case TypeThenApply:581 cur = cur.ThenApply(stage.fn)582 case TypeThenApplyAsync:583 cur = cur.ThenApplyAsync(stage.fn, stage.executor)584 case TypeThenAccept:585 cur = cur.ThenAccept(stage.fn)586 case TypeThenAcceptAsync:587 cur = cur.ThenAcceptAsync(stage.fn, stage.executor)588 case TypeThenRun:589 cur = cur.ThenRun(stage.fn)590 case TypeThenRunAsync:591 cur = cur.ThenRunAsync(stage.fn, stage.executor)592 case TypeThenCombine:593 cur = cur.ThenCombine(runStage(stage.other), stage.fn)594 case TypeThenCombineAsync:595 cur = cur.ThenCombineAsync(runStage(stage.other), stage.fn, stage.executor)596 case TypeThenAcceptBoth:597 cur = cur.ThenAcceptBoth(runStage(stage.other), stage.fn)598 case TypeThenAcceptBothAsync:599 cur = cur.ThenAcceptBothAsync(runStage(stage.other), stage.fn, stage.executor)600 case TypeRunAfterBoth:601 cur = cur.RunAfterBoth(runStage(stage.other), stage.fn)602 case TypeRunAfterBothAsync:603 cur = cur.RunAfterBothAsync(runStage(stage.other), stage.fn, stage.executor)604 case TypeApplyToEither:605 cur = cur.ApplyToEither(runStage(stage.other), stage.fn)606 case TypeApplyToEitherAsync:607 cur = cur.ApplyToEitherAsync(runStage(stage.other), stage.fn, stage.executor)608 case TypeAcceptEither:609 cur = cur.AcceptEither(runStage(stage.other), stage.fn)610 case TypeAcceptEitherAsync:611 cur = cur.AcceptEitherAsync(runStage(stage.other), stage.fn, stage.executor)612 case TypeRunAfterEither:613 cur = cur.RunAfterEither(runStage(stage.other), stage.fn)614 case TypeRunAfterEitherAsync:615 cur = cur.RunAfterEitherAsync(runStage(stage.other), stage.fn, stage.executor)616 case TypeThenCompose:617 cur = cur.ThenCompose(stage.fn)618 case TypeThenComposeAsync:619 cur = cur.ThenComposeAsync(stage.fn, stage.executor)620 case TypeExceptionally:621 cur = cur.Exceptionally(stage.fn)622 case TypeWhenComplete:623 cur = cur.WhenComplete(stage.fn)624 case TypeWhenCompleteAsync:625 cur = cur.WhenCompleteAsync(stage.fn, stage.executor)626 case TypeHandle:627 cur = cur.Handle(stage.fn)628 case TypeHandleAsync:629 cur = cur.HandleAsync(stage.fn, stage.executor)...
build_test.go
Source:build_test.go
...15 s.On("GetName").Return("script-shell")16 s.On("GenerateScript", mock.Anything, mock.Anything).Return("script", nil)17 RegisterShell(&s)18}19func TestBuildRun(t *testing.T) {20 e := MockExecutor{}21 defer e.AssertExpectations(t)22 p := MockExecutorProvider{}23 defer p.AssertExpectations(t)24 // Create executor only once25 p.On("CanCreate").Return(true).Once()26 p.On("GetDefaultShell").Return("bash").Once()27 p.On("GetFeatures", mock.Anything).Return(nil).Twice()28 p.On("Create").Return(&e).Once()29 // We run everything once30 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()31 e.On("Finish", nil).Return().Once()32 e.On("Cleanup").Return().Once()33 // Run script successfully34 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})35 e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()36 e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()37 e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()38 e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()39 e.On("Run", matchBuildStage(BuildStageUserScript)).Return(nil).Once()40 e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()41 e.On("Run", matchBuildStage(BuildStageArchiveCache)).Return(nil).Once()42 e.On("Run", matchBuildStage(BuildStageUploadOnSuccessArtifacts)).Return(nil).Once()43 RegisterExecutor("build-run-test", &p)44 successfulBuild, err := GetSuccessfulBuild()45 assert.NoError(t, err)46 build := &Build{47 JobResponse: successfulBuild,48 Runner: &RunnerConfig{49 RunnerSettings: RunnerSettings{50 Executor: "build-run-test",51 },52 },53 }54 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})55 assert.NoError(t, err)56}57func TestRetryPrepare(t *testing.T) {58 PreparationRetryInterval = 059 e := MockExecutor{}60 defer e.AssertExpectations(t)61 p := MockExecutorProvider{}62 defer p.AssertExpectations(t)63 // Create executor64 p.On("CanCreate").Return(true).Once()65 p.On("GetDefaultShell").Return("bash").Once()66 p.On("GetFeatures", mock.Anything).Return(nil).Twice()67 p.On("Create").Return(&e).Times(3)68 // Prepare plan69 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).70 Return(errors.New("prepare failed")).Twice()71 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).72 Return(nil).Once()73 e.On("Cleanup").Return().Times(3)74 // Succeed a build script75 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})76 e.On("Run", mock.Anything).Return(nil)77 e.On("Finish", nil).Return().Once()78 RegisterExecutor("build-run-retry-prepare", &p)79 successfulBuild, err := GetSuccessfulBuild()80 assert.NoError(t, err)81 build := &Build{82 JobResponse: successfulBuild,83 Runner: &RunnerConfig{84 RunnerSettings: RunnerSettings{85 Executor: "build-run-retry-prepare",86 },87 },88 }89 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})90 assert.NoError(t, err)91}92func TestPrepareFailure(t *testing.T) {93 PreparationRetryInterval = 094 e := MockExecutor{}95 defer e.AssertExpectations(t)96 p := MockExecutorProvider{}97 defer p.AssertExpectations(t)98 // Create executor99 p.On("CanCreate").Return(true).Once()100 p.On("GetDefaultShell").Return("bash").Once()101 p.On("GetFeatures", mock.Anything).Return(nil).Twice()102 p.On("Create").Return(&e).Times(3)103 // Prepare plan104 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).105 Return(errors.New("prepare failed")).Times(3)106 e.On("Cleanup").Return().Times(3)107 RegisterExecutor("build-run-prepare-failure", &p)108 successfulBuild, err := GetSuccessfulBuild()109 assert.NoError(t, err)110 build := &Build{111 JobResponse: successfulBuild,112 Runner: &RunnerConfig{113 RunnerSettings: RunnerSettings{114 Executor: "build-run-prepare-failure",115 },116 },117 }118 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})119 assert.EqualError(t, err, "prepare failed")120}121func TestPrepareFailureOnBuildError(t *testing.T) {122 e := MockExecutor{}123 defer e.AssertExpectations(t)124 p := MockExecutorProvider{}125 defer p.AssertExpectations(t)126 // Create executor127 p.On("CanCreate").Return(true).Once()128 p.On("GetDefaultShell").Return("bash").Once()129 p.On("GetFeatures", mock.Anything).Return(nil).Twice()130 p.On("Create").Return(&e).Times(1)131 // Prepare plan132 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).133 Return(&BuildError{}).Times(1)134 e.On("Cleanup").Return().Times(1)135 RegisterExecutor("build-run-prepare-failure-on-build-error", &p)136 successfulBuild, err := GetSuccessfulBuild()137 assert.NoError(t, err)138 build := &Build{139 JobResponse: successfulBuild,140 Runner: &RunnerConfig{141 RunnerSettings: RunnerSettings{142 Executor: "build-run-prepare-failure-on-build-error",143 },144 },145 }146 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})147 assert.IsType(t, err, &BuildError{})148}149func TestJobFailure(t *testing.T) {150 e := new(MockExecutor)151 defer e.AssertExpectations(t)152 p := new(MockExecutorProvider)153 defer p.AssertExpectations(t)154 // Create executor155 p.On("CanCreate").Return(true).Once()156 p.On("GetDefaultShell").Return("bash").Once()157 p.On("GetFeatures", mock.Anything).Return(nil).Twice()158 p.On("Create").Return(e).Times(1)159 // Prepare plan160 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).161 Return(nil).Times(1)162 e.On("Cleanup").Return().Times(1)163 // Succeed a build script164 thrownErr := &BuildError{Inner: errors.New("test error")}165 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})166 e.On("Run", mock.Anything).Return(thrownErr)167 e.On("Finish", thrownErr).Return().Once()168 RegisterExecutor("build-run-job-failure", p)169 failedBuild, err := GetFailedBuild()170 assert.NoError(t, err)171 build := &Build{172 JobResponse: failedBuild,173 Runner: &RunnerConfig{174 RunnerSettings: RunnerSettings{175 Executor: "build-run-job-failure",176 },177 },178 }179 trace := new(MockJobTrace)180 defer trace.AssertExpectations(t)181 trace.On("Write", mock.Anything).Return(0, nil)182 trace.On("IsStdout").Return(true)183 trace.On("SetCancelFunc", mock.Anything).Once()184 trace.On("Fail", thrownErr, ScriptFailure).Once()185 err = build.Run(&Config{}, trace)186 require.IsType(t, &BuildError{}, err)187}188func TestJobFailureOnExecutionTimeout(t *testing.T) {189 e := new(MockExecutor)190 defer e.AssertExpectations(t)191 p := new(MockExecutorProvider)192 defer p.AssertExpectations(t)193 // Create executor194 p.On("CanCreate").Return(true).Once()195 p.On("GetDefaultShell").Return("bash").Once()196 p.On("GetFeatures", mock.Anything).Return(nil).Twice()197 p.On("Create").Return(e).Times(1)198 // Prepare plan199 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).200 Return(nil).Times(1)201 e.On("Cleanup").Return().Times(1)202 // Succeed a build script203 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})204 e.On("Run", matchBuildStage(BuildStageUserScript)).Run(func(arguments mock.Arguments) {205 time.Sleep(2 * time.Second)206 }).Return(nil)207 e.On("Run", mock.Anything).Return(nil)208 e.On("Finish", mock.Anything).Return().Once()209 RegisterExecutor("build-run-job-failure-on-execution-timeout", p)210 successfulBuild, err := GetSuccessfulBuild()211 assert.NoError(t, err)212 successfulBuild.RunnerInfo.Timeout = 1213 build := &Build{214 JobResponse: successfulBuild,215 Runner: &RunnerConfig{216 RunnerSettings: RunnerSettings{217 Executor: "build-run-job-failure-on-execution-timeout",218 },219 },220 }221 trace := new(MockJobTrace)222 defer trace.AssertExpectations(t)223 trace.On("Write", mock.Anything).Return(0, nil)224 trace.On("IsStdout").Return(true)225 trace.On("SetCancelFunc", mock.Anything).Once()226 trace.On("Fail", mock.Anything, JobExecutionTimeout).Run(func(arguments mock.Arguments) {227 assert.Error(t, arguments.Get(0).(error))228 }).Once()229 err = build.Run(&Config{}, trace)230 require.IsType(t, &BuildError{}, err)231}232func matchBuildStage(buildStage BuildStage) interface{} {233 return mock.MatchedBy(func(cmd ExecutorCommand) bool {234 return cmd.Stage == buildStage235 })236}237func TestRunFailureRunsAfterScriptAndArtifactsOnFailure(t *testing.T) {238 e := MockExecutor{}239 defer e.AssertExpectations(t)240 p := MockExecutorProvider{}241 defer p.AssertExpectations(t)242 // Create executor243 p.On("CanCreate").Return(true).Once()244 p.On("GetDefaultShell").Return("bash").Once()245 p.On("GetFeatures", mock.Anything).Return(nil).Twice()246 p.On("Create").Return(&e).Once()247 // Prepare plan248 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)249 e.On("Cleanup").Return().Once()250 // Fail a build script251 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})252 e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()253 e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()254 e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()255 e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()256 e.On("Run", matchBuildStage(BuildStageUserScript)).Return(errors.New("build fail")).Once()257 e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()258 e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()259 e.On("Finish", errors.New("build fail")).Return().Once()260 RegisterExecutor("build-run-run-failure", &p)261 failedBuild, err := GetFailedBuild()262 assert.NoError(t, err)263 build := &Build{264 JobResponse: failedBuild,265 Runner: &RunnerConfig{266 RunnerSettings: RunnerSettings{267 Executor: "build-run-run-failure",268 },269 },270 }271 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})272 assert.EqualError(t, err, "build fail")273}274func TestGetSourcesRunFailure(t *testing.T) {275 e := MockExecutor{}276 defer e.AssertExpectations(t)277 p := MockExecutorProvider{}278 defer p.AssertExpectations(t)279 // Create executor280 p.On("CanCreate").Return(true).Once()281 p.On("GetDefaultShell").Return("bash").Once()282 p.On("GetFeatures", mock.Anything).Return(nil).Twice()283 p.On("Create").Return(&e).Once()284 // Prepare plan285 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)286 e.On("Cleanup").Return()287 // Fail a build script288 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})289 e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()290 e.On("Run", matchBuildStage(BuildStageGetSources)).Return(errors.New("build fail")).Times(3)291 e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()292 e.On("Finish", errors.New("build fail")).Return().Once()293 RegisterExecutor("build-get-sources-run-failure", &p)294 successfulBuild, err := GetSuccessfulBuild()295 assert.NoError(t, err)296 build := &Build{297 JobResponse: successfulBuild,298 Runner: &RunnerConfig{299 RunnerSettings: RunnerSettings{300 Executor: "build-get-sources-run-failure",301 },302 },303 }304 build.Variables = append(build.Variables, JobVariable{Key: "GET_SOURCES_ATTEMPTS", Value: "3"})305 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})306 assert.EqualError(t, err, "build fail")307}308func TestArtifactDownloadRunFailure(t *testing.T) {309 e := MockExecutor{}310 defer e.AssertExpectations(t)311 p := MockExecutorProvider{}312 defer p.AssertExpectations(t)313 // Create executor314 p.On("CanCreate").Return(true).Once()315 p.On("GetDefaultShell").Return("bash").Once()316 p.On("GetFeatures", mock.Anything).Return(nil).Twice()317 p.On("Create").Return(&e).Once()318 // Prepare plan319 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)320 e.On("Cleanup").Return()321 // Fail a build script322 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})323 e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()324 e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()325 e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()326 e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(errors.New("build fail")).Times(3)327 e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()328 e.On("Finish", errors.New("build fail")).Return().Once()329 RegisterExecutor("build-artifacts-run-failure", &p)330 successfulBuild, err := GetSuccessfulBuild()331 assert.NoError(t, err)332 build := &Build{333 JobResponse: successfulBuild,334 Runner: &RunnerConfig{335 RunnerSettings: RunnerSettings{336 Executor: "build-artifacts-run-failure",337 },338 },339 }340 build.Variables = append(build.Variables, JobVariable{Key: "ARTIFACT_DOWNLOAD_ATTEMPTS", Value: "3"})341 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})342 assert.EqualError(t, err, "build fail")343}344func TestArtifactUploadRunFailure(t *testing.T) {345 e := MockExecutor{}346 defer e.AssertExpectations(t)347 p := MockExecutorProvider{}348 defer p.AssertExpectations(t)349 // Create executor350 p.On("CanCreate").Return(true).Once()351 p.On("GetDefaultShell").Return("bash").Once()352 p.On("GetFeatures", mock.Anything).Return(nil).Twice()353 p.On("Create").Return(&e).Once()354 // Prepare plan355 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)356 e.On("Cleanup").Return()357 // Successful build script358 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"}).Times(8)359 e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()360 e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()361 e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(nil).Once()362 e.On("Run", matchBuildStage(BuildStageDownloadArtifacts)).Return(nil).Once()363 e.On("Run", matchBuildStage(BuildStageUserScript)).Return(nil).Once()364 e.On("Run", matchBuildStage(BuildStageAfterScript)).Return(nil).Once()365 e.On("Run", matchBuildStage(BuildStageArchiveCache)).Return(nil).Once()366 e.On("Run", matchBuildStage(BuildStageUploadOnSuccessArtifacts)).Return(errors.New("upload fail")).Once()367 e.On("Finish", errors.New("upload fail")).Return().Once()368 RegisterExecutor("build-upload-artifacts-run-failure", &p)369 successfulBuild, err := GetSuccessfulBuild()370 successfulBuild.Artifacts = make(Artifacts, 1)371 successfulBuild.Artifacts[0] = Artifact{372 Name: "my-artifact",373 Untracked: false,374 Paths: ArtifactPaths{"cached/*"},375 When: ArtifactWhenAlways,376 }377 assert.NoError(t, err)378 build := &Build{379 JobResponse: successfulBuild,380 Runner: &RunnerConfig{381 RunnerSettings: RunnerSettings{382 Executor: "build-upload-artifacts-run-failure",383 },384 },385 }386 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})387 assert.EqualError(t, err, "upload fail")388}389func TestRestoreCacheRunFailure(t *testing.T) {390 e := MockExecutor{}391 defer e.AssertExpectations(t)392 p := MockExecutorProvider{}393 defer p.AssertExpectations(t)394 // Create executor395 p.On("CanCreate").Return(true).Once()396 p.On("GetDefaultShell").Return("bash").Once()397 p.On("GetFeatures", mock.Anything).Return(nil).Twice()398 p.On("Create").Return(&e).Once()399 // Prepare plan400 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)401 e.On("Cleanup").Return()402 // Fail a build script403 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})404 e.On("Run", matchBuildStage(BuildStagePrepare)).Return(nil).Once()405 e.On("Run", matchBuildStage(BuildStageGetSources)).Return(nil).Once()406 e.On("Run", matchBuildStage(BuildStageRestoreCache)).Return(errors.New("build fail")).Times(3)407 e.On("Run", matchBuildStage(BuildStageUploadOnFailureArtifacts)).Return(nil).Once()408 e.On("Finish", errors.New("build fail")).Return().Once()409 RegisterExecutor("build-cache-run-failure", &p)410 successfulBuild, err := GetSuccessfulBuild()411 assert.NoError(t, err)412 build := &Build{413 JobResponse: successfulBuild,414 Runner: &RunnerConfig{415 RunnerSettings: RunnerSettings{416 Executor: "build-cache-run-failure",417 },418 },419 }420 build.Variables = append(build.Variables, JobVariable{Key: "RESTORE_CACHE_ATTEMPTS", Value: "3"})421 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})422 assert.EqualError(t, err, "build fail")423}424func TestRunWrongAttempts(t *testing.T) {425 e := MockExecutor{}426 p := MockExecutorProvider{}427 defer p.AssertExpectations(t)428 // Create executor429 p.On("CanCreate").Return(true).Once()430 p.On("GetDefaultShell").Return("bash").Once()431 p.On("GetFeatures", mock.Anything).Return(nil).Twice()432 p.On("Create").Return(&e)433 // Prepare plan434 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil)435 e.On("Cleanup").Return()436 // Fail a build script437 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})438 e.On("Run", mock.Anything).Return(nil).Once()439 e.On("Run", mock.Anything).Return(errors.New("Number of attempts out of the range [1, 10] for stage: get_sources"))440 e.On("Finish", errors.New("Number of attempts out of the range [1, 10] for stage: get_sources")).Return()441 RegisterExecutor("build-run-attempt-failure", &p)442 successfulBuild, err := GetSuccessfulBuild()443 assert.NoError(t, err)444 build := &Build{445 JobResponse: successfulBuild,446 Runner: &RunnerConfig{447 RunnerSettings: RunnerSettings{448 Executor: "build-run-attempt-failure",449 },450 },451 }452 build.Variables = append(build.Variables, JobVariable{Key: "GET_SOURCES_ATTEMPTS", Value: "0"})453 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})454 assert.EqualError(t, err, "Number of attempts out of the range [1, 10] for stage: get_sources")455}456func TestRunSuccessOnSecondAttempt(t *testing.T) {457 e := MockExecutor{}458 p := MockExecutorProvider{}459 // Create executor only once460 p.On("CanCreate").Return(true).Once()461 p.On("GetDefaultShell").Return("bash").Once()462 p.On("GetFeatures", mock.Anything).Return(nil).Twice()463 p.On("Create").Return(&e).Once()464 // We run everything once465 e.On("Prepare", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()466 e.On("Finish", mock.Anything).Return().Twice()467 e.On("Cleanup").Return().Twice()468 // Run script successfully469 e.On("Shell").Return(&ShellScriptInfo{Shell: "script-shell"})470 e.On("Run", mock.Anything).Return(nil)471 e.On("Run", mock.Anything).Return(errors.New("build fail")).Once()472 e.On("Run", mock.Anything).Return(nil)473 RegisterExecutor("build-run-success-second-attempt", &p)474 successfulBuild, err := GetSuccessfulBuild()475 assert.NoError(t, err)476 build := &Build{477 JobResponse: successfulBuild,478 Runner: &RunnerConfig{479 RunnerSettings: RunnerSettings{480 Executor: "build-run-success-second-attempt",481 },482 },483 }484 build.Variables = append(build.Variables, JobVariable{Key: "GET_SOURCES_ATTEMPTS", Value: "3"})485 err = build.Run(&Config{}, &Trace{Writer: os.Stdout})486 assert.NoError(t, err)487}488func TestDebugTrace(t *testing.T) {489 build := &Build{}490 assert.False(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be false if CI_DEBUG_TRACE is not set")491 successfulBuild, err := GetSuccessfulBuild()492 assert.NoError(t, err)493 successfulBuild.Variables = append(successfulBuild.Variables, JobVariable{"CI_DEBUG_TRACE", "false", true, true, false})494 build = &Build{495 JobResponse: successfulBuild,496 }497 assert.False(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be false if CI_DEBUG_TRACE is set to false")498 successfulBuild, err = GetSuccessfulBuild()499 assert.NoError(t, err)500 successfulBuild.Variables = append(successfulBuild.Variables, JobVariable{"CI_DEBUG_TRACE", "true", true, true, false})501 build = &Build{502 JobResponse: successfulBuild,503 }504 assert.True(t, build.IsDebugTraceEnabled(), "IsDebugTraceEnabled should be true if CI_DEBUG_TRACE is set to true")505}506func TestSharedEnvVariables(t *testing.T) {507 for _, shared := range [...]bool{true, false} {508 t.Run(fmt.Sprintf("Value:%v", shared), func(t *testing.T) {509 assert := assert.New(t)510 build := Build{511 ExecutorFeatures: FeaturesInfo{Shared: shared},512 }513 vars := build.GetAllVariables().StringList()514 assert.NotNil(vars)515 present := "CI_SHARED_ENVIRONMENT=true"516 absent := "CI_DISPOSABLE_ENVIRONMENT=true"517 if !shared {518 tmp := present519 present = absent520 absent = tmp521 }522 assert.Contains(vars, present)523 assert.NotContains(vars, absent)524 // we never expose false525 assert.NotContains(vars, "CI_SHARED_ENVIRONMENT=false")526 assert.NotContains(vars, "CI_DISPOSABLE_ENVIRONMENT=false")527 })528 }529}530func TestGetRemoteURL(t *testing.T) {531 testCases := []struct {532 runner RunnerSettings533 result string534 }{535 {536 runner: RunnerSettings{537 CloneURL: "http://test.local/",538 },539 result: "http://gitlab-ci-token:1234567@test.local/h5bp/html5-boilerplate.git",540 },541 {542 runner: RunnerSettings{543 CloneURL: "https://test.local",544 },545 result: "https://gitlab-ci-token:1234567@test.local/h5bp/html5-boilerplate.git",546 },547 {548 runner: RunnerSettings{},549 result: "http://fallback.url",550 },551 }552 for _, tc := range testCases {553 build := &Build{554 Runner: &RunnerConfig{555 RunnerSettings: tc.runner,556 },557 allVariables: JobVariables{558 JobVariable{Key: "CI_JOB_TOKEN", Value: "1234567"},559 JobVariable{Key: "CI_PROJECT_PATH", Value: "h5bp/html5-boilerplate"},560 },561 JobResponse: JobResponse{562 GitInfo: GitInfo{RepoURL: "http://fallback.url"},563 },564 }565 assert.Equal(t, tc.result, build.GetRemoteURL())566 }567}568type featureFlagOnTestCase struct {569 value string570 expectedStatus bool571 expectedError bool572}573func TestIsFeatureFlagOn(t *testing.T) {574 hook := test.NewGlobal()575 tests := map[string]featureFlagOnTestCase{576 "no value": {577 value: "",578 expectedStatus: false,579 expectedError: false,580 },581 "true": {582 value: "true",583 expectedStatus: true,584 expectedError: false,585 },586 "1": {587 value: "1",588 expectedStatus: true,589 expectedError: false,590 },591 "false": {592 value: "false",593 expectedStatus: false,594 expectedError: false,595 },596 "0": {597 value: "0",598 expectedStatus: false,599 expectedError: false,600 },601 "invalid value": {602 value: "test",603 expectedStatus: false,604 expectedError: true,605 },606 }607 for name, testCase := range tests {608 t.Run(name, func(t *testing.T) {609 build := new(Build)610 build.Variables = JobVariables{611 {Key: "FF_TEST_FEATURE", Value: testCase.value},612 }613 status := build.IsFeatureFlagOn("FF_TEST_FEATURE")614 assert.Equal(t, testCase.expectedStatus, status)615 entry := hook.LastEntry()616 if testCase.expectedError {617 require.NotNil(t, entry)618 logrusOutput, err := entry.String()619 require.NoError(t, err)620 assert.Contains(t, logrusOutput, "Error while parsing the value of feature flag")621 } else {622 assert.Nil(t, entry)...
runlevel.go
Source:runlevel.go
...9)10//å®ä¹runlevel å½ä»¤11const (12 //ç³»ç»èªå¨éæ©runlevelå½ä»¤13 CmdNameGetCurrentRunLevel = "GetCurRunLevel"14 CmdNameSetRunLevel = "SetRunLevel"15 CmdNameIsolateRunLevel = "IsolateRunLevel"16 //ç³»ç»è¿è¡çº§å« 使ç¨systemctlå½ä»¤17 CmdNameGetCurrentRunLevelSctrl = "GetCurRunLevelSctrl"18 CmdNameSetRunLevelSctrl = "SetRunLevelSctrl"19 CmdNameIsolateRunLevelSctrl = "IsolateRunLevelSctrl"20 //ç³»ç»è¿è¡çº§å« 使ç¨initå½ä»¤21 CmdNameGetCurrentRunLevelInit = "GetCurRunLevelInit"22 CmdNameSetRunLevelInit = "SetRunLevelInit"23 CmdNameIsolateRunLevelInit = "IsolateRunLevelInit"24)25//runlevel åæ°26const (27 CmdParamRunLevel = "runLevel"28)29//runlevel è¿åç»æ30const (31 ResultDataKeyStatus = "status"32)33var (34 gCentOs7RunLevelList = map[int]string{35 0: "poweroff.target",36 1: "rescue.target",37 2: "multi-user.target",38 3: "multi-user.target",39 4: "multi-user.target",40 5: "graphical.target",41 6: "reboot.target"}42)43func getRunLevelIndex(runLevel string) string {44 switch runLevel {45 case "poweroff.target":46 return "0"47 case "rescue.target":48 return "1"49 case "multi-user.target":50 return "3"51 case "graphical.target":52 return "5"53 case "reboot.target":54 return "6"55 }56 return ""57}58func getRunLevelIndexFromCmdInit(runLevel string) string {59 replaceFlag := "N "60 levelStr := strings.Replace(runLevel, replaceFlag, "", -1)61 if levelStr != "" {62 _, err := strconv.Atoi(levelStr)63 if err == nil {64 return levelStr65 }66 }67 return ""68}69//GetCurRunLevelSctrl centos7 è·åå½årunlevel å½ä»¤70func GetCurRunLevelSctrl(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {71 if e != nil {72 //@remark è·åå½årunlevel ä¸éè¦åæ°73 cmdStr := "systemctl get-default"74 es, err := e.ExecShell(cmdStr)75 if err != nil {76 return executor.ErrorExecuteResult(err)77 }78 if es.ExitCode == 0 && len(es.Stderr) == 0 {79 er := executor.SuccessulExecuteResult(es, false, "Runlevel get successful")80 var value string81 if len(es.Stdout) == 1 {82 value = es.Stdout[0]83 value = getRunLevelIndex(value)84 }85 resultData := map[string]string{ResultDataKeyStatus: value}86 er.ResultData = resultData87 return er88 }89 var errMsg string90 if len(es.Stderr) == 0 {91 errMsg = executor.ErrMsgUnknow92 } else {93 errMsg = es.Stderr[0]94 }95 return executor.NotSuccessulExecuteResult(es, errMsg)96 }97 return executor.ErrorExecuteResult(errors.New("executor is nil"))98}99//SetRunLevelSctrl centos7 设置runlevel å½ä»¤100func SetRunLevelSctrl(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {101 if e != nil {102 level, err := executor.ExtractCmdFuncIntParam(params, CmdParamRunLevel)103 if err != nil {104 return executor.ErrorExecuteResult(err)105 }106 var (107 runLevelStr string108 ok bool109 )110 runLevelStr, ok = gCentOs7RunLevelList[level]111 if !ok {112 return executor.ErrorExecuteResult(errors.New("unrecognized param"))113 }114 cmdStr := fmt.Sprintf("%s %s %s", "systemctl", "set-default", runLevelStr)115 log.Debug("cmdStr=%s", cmdStr)116 es, err := e.ExecShell(cmdStr)117 if err != nil {118 return executor.ErrorExecuteResult(err)119 }120 log.Debug("SetRunLevel Result %v", es)121 log.Debug("SetRunLevel es.Stdout %v", es.Stdout)122 log.Debug("SetRunLevel es.Stderr %v", es.Stderr)123 if es.ExitCode == 0 /*&& len(es.Stderr) == 0*/ {124 er := executor.SuccessulExecuteResult(es, true, fmt.Sprintf("Runlevel %d set successful", level))125 return er126 }127 var errMsg string128 if len(es.Stderr) == 0 {129 errMsg = executor.ErrMsgUnknow130 } else {131 errMsg = es.Stderr[0]132 }133 return executor.NotSuccessulExecuteResult(es, errMsg)134 }135 return executor.ErrorExecuteResult(errors.New("executor is nil"))136}137//IsolateRunLevelSctrl centos7 åæ¢å°æå®çè¿è¡çº§å«138func IsolateRunLevelSctrl(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {139 if e != nil {140 level, err := executor.ExtractCmdFuncIntParam(params, CmdParamRunLevel)141 if err != nil {142 return executor.ErrorExecuteResult(err)143 }144 var (145 runLevelStr string146 ok bool147 )148 runLevelStr, ok = gCentOs7RunLevelList[level]149 if !ok {150 return executor.ErrorExecuteResult(errors.New("unrecognized param"))151 }152 cmdStr := fmt.Sprintf("%s %s %s", "systemctl", "isolate", runLevelStr)153 es, err := e.ExecShell(cmdStr)154 if err != nil {155 return executor.ErrorExecuteResult(err)156 }157 if es.ExitCode == 0 && len(es.Stderr) == 0 {158 er := executor.SuccessulExecuteResult(es, true, "")159 return er160 }161 var errMsg string162 if len(es.Stderr) == 0 {163 errMsg = executor.ErrMsgUnknow164 } else {165 errMsg = es.Stderr[0]166 }167 return executor.NotSuccessulExecuteResult(es, errMsg)168 }169 return executor.ErrorExecuteResult(errors.New("executor is nil"))170}171//GetCurRunLevelInit éè¿initå½ä»¤è·årunlevel172func GetCurRunLevelInit(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {173 if e != nil {174 cmdStr := "runlevel"175 es, err := e.ExecShell(cmdStr)176 if err != nil {177 return executor.ErrorExecuteResult(err)178 }179 if es.ExitCode == 0 && len(es.Stderr) == 0 {180 er := executor.SuccessulExecuteResult(es, false, "get current run level init successful")181 var value string182 if len(es.Stdout) == 1 {183 value = es.Stdout[0]184 value = getRunLevelIndexFromCmdInit(value)185 }186 resultData := map[string]string{ResultDataKeyStatus: value}187 er.ResultData = resultData188 return er189 }190 var errMsg string191 if len(es.Stderr) == 0 {192 errMsg = executor.ErrMsgUnknow193 } else {194 errMsg = es.Stderr[0]195 }196 return executor.NotSuccessulExecuteResult(es, errMsg)197 }198 return executor.ErrorExecuteResult(errors.New("executor is nil"))199}200//SetRunLevelInit ä½äºcentos7 设置runlevel å½ä»¤201func SetRunLevelInit(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {202 if e != nil {203 //sed s/id:5:initdefault:/id:3:initdefault:/g204 initPath := "/etc/inittab"205 exist, err := fileExistCharacter(e, initPath, "id:3:initdefault")206 if err != nil {207 return executor.ErrorExecuteResult(err)208 }209 if exist {210 result := new(executor.ExecuteResult)211 result.Changed = false212 result.Successful = true213 result.ExecuteHasError = false214 result.Message = "no need to set runlevel to 3"215 return *result216 }217 cmdStr := fmt.Sprintf("%s %s %s", "sed -i ", " s/id:5:initdefault:/id:3:initdefault:/g", initPath)218 fmt.Println("cmdStr=", cmdStr)219 es, err := e.ExecShell(cmdStr)220 if err != nil {221 return executor.ErrorExecuteResult(err)222 }223 if es.ExitCode == 0 && len(es.Stderr) == 0 {224 er := executor.SuccessulExecuteResult(es, true, "set run level init successful")225 return er226 }227 var errMsg string228 if len(es.Stderr) == 0 {229 errMsg = executor.ErrMsgUnknow230 } else {231 errMsg = es.Stderr[0]232 }233 return executor.NotSuccessulExecuteResult(es, errMsg)234 }235 return executor.ErrorExecuteResult(errors.New("executor is nil"))236}237//IsolateRunLevelInit init N ç«å³åæ¢runlevel238func IsolateRunLevelInit(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {239 if e != nil {240 level, err := executor.ExtractCmdFuncIntParam(params, CmdParamRunLevel)241 if err != nil {242 return executor.ErrorExecuteResult(err)243 }244 if level < 0 || level > 6 {245 return executor.ErrorExecuteResult(errors.New("unrecognized param"))246 }247 cmdStr := "init " + strconv.Itoa(level)248 es, err := e.ExecShell(cmdStr)249 if err != nil {250 return executor.ErrorExecuteResult(err)251 }252 if es.ExitCode == 0 && len(es.Stderr) == 0 {253 er := executor.SuccessulExecuteResult(es, true, fmt.Sprintf("isolate run level init to %d successful", level))254 return er255 }256 var errMsg string257 if len(es.Stderr) == 0 {258 errMsg = executor.ErrMsgUnknow259 } else {260 errMsg = es.Stderr[0]261 }262 return executor.NotSuccessulExecuteResult(es, errMsg)263 }264 return executor.ErrorExecuteResult(errors.New("executor is nil"))265}266//GetCurRunLevel è·åå½årunlevel å½ä»¤267func GetCurRunLevel(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {268 return GetCurRunLevelInit(e, params)269}270//SetRunLevel 设置runlevel å½ä»¤ èªå¨éæ©çæ¬271func SetRunLevel(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {272 er := LinuxDist(e, params)273 if !er.ExecuteHasError {274 version, ok := er.ResultData[ResultDataKeyVersion]275 if ok {276 (*params)[CmdParamVersion] = version277 }278 } else {279 return executor.ErrorExecuteResult(errors.New(er.Message))280 }281 iVer, err := GetLinuxDistVer(params)282 if err != nil {283 return executor.ErrorExecuteResult(err)284 }285 log.Debug("Current Version %d", iVer)286 if iVer >= 7 {287 return SetRunLevelSctrl(e, params)288 }289 return SetRunLevelInit(e, params)290}291//IsolateRunLevel åæ¢å°æå®çè¿è¡çº§å«292func IsolateRunLevel(e executor.Executor, params *executor.ExecutorCmdParams) executor.ExecuteResult {293 return IsolateRunLevelInit(e, params)294}295//fileExistCharacter å¤ææ个å符串æ¯åå¨æ个æ件ä¸296func fileExistCharacter(e executor.Executor, fileName, text string) (bool, error) {297 containFlag := "contain"298 notContainFlag := "not contain"299 cmdStr := fmt.Sprintf("grep -wq %s %s && echo %s || echo %s ", text, fileName, containFlag, notContainFlag)300 es, err := e.ExecShell(cmdStr)301 if err == nil {302 err = executor.GetExecResult(es)303 if err == nil {304 if len(es.Stdout) > 0 {305 if containFlag == strings.TrimSpace(es.Stdout[0]) {306 return true, nil307 }...
executor_builder.go
Source:executor_builder.go
...25//CompileBuilder facet of ExecutorBuilder26type CompileBuilder struct {27 ExecutorBuilder28}29//RunBuilder facet of ExecutorBuilder30type RunBuilder struct {31 ExecutorBuilder32}33//ValidatorBuilder facet of ExecutorBuilder34type ValidatorBuilder struct {35 ExecutorBuilder36}37//PreparerBuilder facet of ExecutorBuilder38type PreparerBuilder struct {39 ExecutorBuilder40}41//UnitTestExecutorBuilder facet of ExecutorBuilder42type UnitTestExecutorBuilder struct {43 ExecutorBuilder44}45//NewExecutorBuilder constructor for Executor46func NewExecutorBuilder() *ExecutorBuilder {47 return &ExecutorBuilder{}48}49// WithCompiler - Lives chains to type *ExecutorBuilder and returns a *CompileBuilder50func (b *ExecutorBuilder) WithCompiler() *CompileBuilder {51 return &CompileBuilder{*b}52}53// WithRunner - Lives chains to type *ExecutorBuilder and returns a *CompileBuilder54func (b *ExecutorBuilder) WithRunner() *RunBuilder {55 return &RunBuilder{*b}56}57// WithValidator - Lives chains to type *ExecutorBuilder and returns a *CompileBuilder58func (b *ExecutorBuilder) WithValidator() *ValidatorBuilder {59 return &ValidatorBuilder{*b}60}61// WithPreparer - Lives chains to type *ExecutorBuilder and returns a *PreparerBuilder62func (b *ExecutorBuilder) WithPreparer() *PreparerBuilder {63 return &PreparerBuilder{*b}64}65// WithTestRunner - Lives chains to type *ExecutorBuilder and returns a *UnitTestExecutorBuilder66func (b *ExecutorBuilder) WithTestRunner() *UnitTestExecutorBuilder {67 return &UnitTestExecutorBuilder{*b}68}69//WithCommand adds compile command to executor70func (b *CompileBuilder) WithCommand(compileCmd string) *CompileBuilder {71 b.actions = append(b.actions, func(e *Executor) {72 e.compileArgs.commandName = compileCmd73 })74 return b75}76//WithWorkingDir adds dir path to executor77func (b *CompileBuilder) WithWorkingDir(dir string) *CompileBuilder {78 b.actions = append(b.actions, func(e *Executor) {79 e.compileArgs.workingDir = dir80 })81 return b82}83//WithArgs adds compile args to executor84func (b *CompileBuilder) WithArgs(compileArgs []string) *CompileBuilder {85 b.actions = append(b.actions, func(e *Executor) {86 e.compileArgs.commandArgs = compileArgs87 })88 return b89}90//WithFileName adds file name to executor91func (b *CompileBuilder) WithFileName(fileName string) *CompileBuilder {92 b.actions = append(b.actions, func(e *Executor) {93 e.compileArgs.fileName = fileName94 })95 return b96}97//WithExecutableFileName adds file name to executor98func (b *RunBuilder) WithExecutableFileName(name string) *RunBuilder {99 b.actions = append(b.actions, func(e *Executor) {100 e.runArgs.fileName = name101 })102 return b103}104//WithWorkingDir adds dir path to executor105func (b *RunBuilder) WithWorkingDir(dir string) *RunBuilder {106 b.actions = append(b.actions, func(e *Executor) {107 e.runArgs.workingDir = dir108 })109 return b110}111//WithCommand adds run command to executor112func (b *RunBuilder) WithCommand(runCmd string) *RunBuilder {113 b.actions = append(b.actions, func(e *Executor) {114 e.runArgs.commandName = runCmd115 })116 return b117}118//WithArgs adds run args to executor119func (b *RunBuilder) WithArgs(runArgs []string) *RunBuilder {120 b.actions = append(b.actions, func(e *Executor) {121 e.runArgs.commandArgs = runArgs122 })123 return b124}125//WithGraphOutput adds the need of graph output to executor126func (b *RunBuilder) WithGraphOutput() *RunBuilder {127 b.actions = append(b.actions, func(e *Executor) {128 //todo129 })130 return b131}132//WithCommand adds test command to executor133func (b *UnitTestExecutorBuilder) WithCommand(testCmd string) *UnitTestExecutorBuilder {134 b.actions = append(b.actions, func(e *Executor) {135 e.testArgs.commandName = testCmd136 })137 return b138}139//WithArgs adds test args to executor140func (b *UnitTestExecutorBuilder) WithArgs(testArgs []string) *UnitTestExecutorBuilder {141 b.actions = append(b.actions, func(e *Executor) {142 e.testArgs.commandArgs = testArgs143 })144 return b145}146// WithWorkingDir adds dir path to executor147func (b *UnitTestExecutorBuilder) WithWorkingDir(dir string) *UnitTestExecutorBuilder {148 b.actions = append(b.actions, func(e *Executor) {149 e.testArgs.workingDir = dir150 })151 return b152}153//WithGraphOutput adds the need of graph output to executor154func (b *UnitTestExecutorBuilder) WithGraphOutput() *UnitTestExecutorBuilder {155 b.actions = append(b.actions, func(e *Executor) {156 //todo157 })158 return b159}160//WithExecutableFileName adds file name to executor161func (b *UnitTestExecutorBuilder) WithExecutableFileName(name string) *UnitTestExecutorBuilder {162 b.actions = append(b.actions, func(e *Executor) {163 e.testArgs.fileName = name164 })165 return b166}167//WithSdkValidators sets validators to executor168func (b *ValidatorBuilder) WithSdkValidators(validators *[]validators.Validator) *ValidatorBuilder {169 b.actions = append(b.actions, func(e *Executor) {170 e.validators = *validators171 })172 return b173}174//WithSdkPreparers sets preparers to executor175func (b *PreparerBuilder) WithSdkPreparers(preparers *[]preparers.Preparer) *PreparerBuilder {176 b.actions = append(b.actions, func(e *Executor) {177 e.preparers = *preparers178 })179 return b180}181//WithPipelineOptions adds pipeline options to executor182func (b *RunBuilder) WithPipelineOptions(pipelineOptions []string) *RunBuilder {183 b.actions = append(b.actions, func(e *Executor) {184 e.runArgs.pipelineOptions = pipelineOptions185 })186 return b187}188//Build builds the executor object189func (b *ExecutorBuilder) Build() Executor {190 executor := Executor{}191 for _, a := range b.actions {192 a(&executor)193 }194 return executor195}...
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}10import (11func main() {12 cmd := exec.Command("ls", "-l")13 err := cmd.Start()14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println("Command Executed")18}19import (20func main() {21 cmd := exec.Command("ls", "-l")22 err := cmd.Start()23 if err != nil {24 fmt.Println(err)25 }26 fmt.Println("Command Executed")27 cmd.Wait()28 fmt.Println("Command Finished")29}30import (31func main() {32 cmd := exec.Command("ls", "-l")33 out, err := cmd.CombinedOutput()34 if err != nil {35 fmt.Println(err)36 }37 fmt.Println(string(out))38}39import (40func main() {41 cmd := exec.Command("ls", "-l")42 stdout, err := cmd.StdoutPipe()43 if err != nil {44 fmt.Println(err)45 }46 err = cmd.Start()47 if err != nil {48 fmt.Println(err)49 }50 output := make([]byte, 1000)51 stdout.Read(output)52 fmt.Println(string(output))53}54import (55func main() {56 cmd := exec.Command("ls", "-l")57 stderr, err := cmd.StderrPipe()58 if err != nil {59 fmt.Println(err)60 }61 err = cmd.Start()62 if err != nil {63 fmt.Println(err
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 fmt.Println(cmd.Run())5}6import (7func main() {8 cmd := exec.Command("ls", "-l")9 stdout, err := cmd.StdoutPipe()10 if err != nil {11 fmt.Println(err)12 }13 if err := cmd.Start(); err != nil {14 fmt.Println(err)15 }16 for {17 buf := make([]byte, 100)18 n, err := stdout.Read(buf)19 if err != nil && err != io.EOF {20 fmt.Println(err)21 }22 if n == 0 {
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 err := cmd.Run()5 if err != nil {6 fmt.Println(err)7 }8}9Example 2: Using Run() method of exec package to execute a command with arguments10import (11func main() {12 cmd := exec.Command("ls", "-l", "-a")13 err := cmd.Run()14 if err != nil {15 fmt.Println(err)16 }17}18Example 3: Using Run() method of exec package to execute a command with arguments and environment variables19import (20func main() {21 cmd := exec.Command("ls", "-l", "-a")22 cmd.Env = append(cmd.Env, "PATH=/bin:/usr/bin")23 err := cmd.Run()24 if err != nil {25 fmt.Println(err)26 }27}
Run
Using AI Code Generation
1func main() {2 executor := &executor.Executor{}3 executor.Run()4}5func main() {6 executor := &executor.Executor{}7 executor.Run()8}9func main() {10 executor := &executor.Executor{}11 executor.Run()12}13func main() {14 executor := &executor.Executor{}15 executor.Run()16}17func main() {18 executor := &executor.Executor{}19 executor.Run()20}21func main() {22 executor := &executor.Executor{}23 executor.Run()24}25func main() {26 executor := &executor.Executor{}27 executor.Run()28}29func main() {30 executor := &executor.Executor{}31 executor.Run()32}33func main() {34 executor := &executor.Executor{}35 executor.Run()36}37func main() {38 executor := &executor.Executor{}39 executor.Run()40}41func main() {42 executor := &executor.Executor{}43 executor.Run()44}45func main() {46 executor := &executor.Executor{}47 executor.Run()48}49func main() {50 executor := &executor.Executor{}51 executor.Run()52}53func main() {54 executor := &executor.Executor{}55 executor.Run()56}57func main() {58 executor := &executor.Executor{}59 executor.Run()60}61func main()
Run
Using AI Code Generation
1import (2func main() {3 fmt.Println("Starting the process...")4 time.Sleep(2 * time.Second)5 fmt.Println("Process ended")6}7import (8func main() {9 fmt.Println("Starting the process...")10 time.Sleep(2 * time.Second)11 fmt.Println("Process ended")12}13import (14func main() {15 fmt.Println("Starting the process...")16 time.Sleep(2 * time.Second)17 fmt.Println("Process ended")18}19import (20type Executor struct {21}22func (exec Executor) Execute() {23 fmt.Println("Starting the process...")24 time.Sleep(2 * time.Second)25 fmt.Println("Process ended")26}27func (exec Executor) Run() {28 fmt.Println("Starting the process...")29 time.Sleep(2 * time.Second)30 fmt.Println("Process ended")31}32import (33type Executor struct {34}
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls")4 output, err := cmd.Output()5 if err != nil {6 fmt.Println("Error: ", err)7 os.Exit(1)8 }9 fmt.Println("Output: ", string(output))10}11import (12func main() {13 cmd := exec.Command("ls")14 output, err := cmd.CombinedOutput()15 if err != nil {16 fmt.Println("Error: ", err)17 os.Exit(1)18 }19 fmt.Println("Output: ", string(output))20}21import (22func main() {23 cmd := exec.Command("ls")24 err := cmd.Start()25 if err != nil {26 fmt.Println("Error: ", err)27 os.Exit(1)28 }29 fmt.Println("Waiting for command to finish...")30 err = cmd.Wait()31 fmt.Println("Command finished with error: ", err)32}33import (34func main() {35 cmd := exec.Command("ls")36 stdout, err := cmd.StdoutPipe()37 if err != nil {38 fmt.Println("Error: ", err)39 os.Exit(1)40 }41 err = cmd.Start()42 if err != nil {43 fmt.Println("Error: ", err)44 os.Exit(1)45 }46 output, err := ioutil.ReadAll(stdout)47 if err != nil {48 fmt.Println("Error: ", err)49 os.Exit(1)50 }51 err = cmd.Wait()52 if err != nil {53 fmt.Println("Error: ", err)54 os.Exit(1)55 }56 fmt.Println("Output: ", string(output))57}58import (
Run
Using AI Code Generation
1import (2func main() {3 fmt.Println("Executing command: ls")4 cmd := exec.Command("ls")5 err := cmd.Run()6 if err != nil {7 fmt.Println("Error: ", err)8 }9}10import (11func main() {12 fmt.Println("Executing command: ls")13 cmd := exec.Command("ls")14 out, err := cmd.Output()15 if err != nil {16 fmt.Println("Error: ", err)17 }18 fmt.Println("Output: ", string(out))19}20import (21func main() {22 fmt.Println("Executing command: ls")23 cmd := exec.Command("ls")24 out, err := cmd.CombinedOutput()25 if err != nil {26 fmt.Println("Error: ", err)27 }28 fmt.Println("Output: ", string(out))29}30import (31func main() {32 fmt.Println("Executing command: ls")33 cmd := exec.Command("ls")34 err := cmd.Start()35 if err != nil {36 fmt.Println("Error: ", err)37 }38}39import (40func main() {41 fmt.Println("Executing command: ls")42 cmd := exec.Command("ls")43 err := cmd.Start()44 if err != nil {45 fmt.Println("Error: ", err)46 }47 err = cmd.Wait()48 if err != nil {49 fmt.Println("Error: ", err)50 }51}52import (53func main() {54 fmt.Println("Executing command: ls")55 cmd := exec.Command("ls")56 stdout, err := cmd.StdoutPipe()57 if err != nil {58 fmt.Println("Error: ", err)59 }60 err = cmd.Start()61 if err != nil {62 fmt.Println("Error: ", err)63 }64 io.Copy(os.Stdout, stdout)65}
Run
Using AI Code Generation
1import (2func main() {3 fmt.Println("Starting")4 ex := executor.New()5 ex.Run(func() {6 fmt.Println("Executing task 1")7 })8 ex.Run(func() {9 fmt.Println("Executing task 2")10 })11 ex.Run(func() {12 fmt.Println("Executing task 3")13 })14 ex.Run(func() {15 fmt.Println("Executing task 4")16 })17 ex.Run(func() {18 fmt.Println("Executing task 5")19 })20 ex.Run(func() {21 fmt.Println("Executing task 6")22 })23 ex.Run(func() {24 fmt.Println("Executing task 7")25 })26 ex.Run(func() {27 fmt.Println("Executing task 8")28 })29 ex.Run(func() {30 fmt.Println("Executing task 9")31 })32 ex.Run(func() {33 fmt.Println("Executing task 10")34 })35 ex.Run(func() {36 fmt.Println("Executing task 11")37 })38 ex.Run(func() {39 fmt.Println("Executing task 12")40 })41 ex.Run(func() {42 fmt.Println("Executing task 13")43 })44 ex.Run(func() {45 fmt.Println("Executing task 14")46 })47 ex.Run(func() {48 fmt.Println("Executing task 15")49 })50 ex.Run(func() {51 fmt.Println("Executing task 16")52 })53 ex.Run(func() {54 fmt.Println("Executing task 17")55 })
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!