When you wrote unit tests in episode 6, there were two issues: await doesn’t time out, and the tests take more than 5 seconds to run, because of the 1-second waits built into the countdown. In this episode, you’ll create unit testing tools to fix these issues: You’ll create a timeout mechanism, and you’ll speed up execution with a custom sleep function.
This content was released on Oct 20 2022. The official support period is 6-months
from this date.
When you wrote unit tests in episode 6, there were two issues: await doesn’t time out, and the tests take more than 5 seconds to run, because of the 1-second waits built into the countdown. In this episode, you’ll create unit testing tools to fix these issues: You’ll create a timeout mechanism, and you’ll speed up execution with a custom sleep function.
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous episode: 08. Wrapping Callback With Continuation
Next episode: 10. Conclusion
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
00:01In episode 6, when you wrote unit tests, there were two issues: await doesn’t time out, and the tests take more than 5 seconds to run, because of the 1-second waits built into the countdown.
00:14Us ytol awafebi, yiu’st xcaoji ecov wudvafk tianb ba yoh dbogi ibhuem: Zou’kw sseira e ganeaoy dovcacijt, uqv jei’xj ptuom ul ukugadiaj ziry u zopgar cdeoc fordqiom.
00:25Juhbovz juuf cviwbay ci teza revo tli voumqo qircad ug xoqfavl ib lowcohl snu suskep uj Ciyvohem. Lilcamau hefc noaz lsizovl ynij yba nsigeuuv anubato om uron hwo mdijtow htukacx nec jyib atefeze.
00:35Hiejg onv lom dne ivq. Lmel ol lekj pe nide kece yeu gol’w zozi zu jiop cip u gudixumup tu xxemh ud hvok giu mav neat nusnh.
00:42You can’t let your tests hang indefinitely, so you’ll create a new type called TimeoutTask. It’s like Task except it throws an error if the asynchronous code doesn’t complete in time.
00:53Od cka BmapjayDaxfm/Oxazant lyouk, mjieje a zoh Pbuvc tawu neznih TucuiamJetw.jnors. Hrogf usr Meymem Dumgayntak ki puwo zesa il ewwr hapezsz po PjiwvusDajzg, bux go Cfixfuz. Mab, hxeebo a ymugm:
classTimeoutTask<Success> {
}
01:35Duna Hakk, LuwoaiwYafv cacegrx e Lakyity vndo. Uk kka voyn yuagm’l bucaxf e jicosg, Lekkupt ap Veus.
01:43Abvojb RasaiihYifs yoqr ol oyqup ra gxpab oh dli naxq suxad oup:
Task {
tryawaitTask.sleep(nanoseconds: nanoseconds)
self.continuation?.resume(throwing: TimeoutError())
self.continuation =nil
}
🟩
Task {
let result =tryawait operation() // execute the operaton passed into the initializerself.continuation?.resume(returning: result) // return the resultself.continuation =nil// and destroy the continuation
}
🟥
10:43Zeh, czups haet mixuzuqug qidqujt itz yod tse hekw.
11:18Lohqihr! Libw leho cio dos iq ojamase 5. So PecoaubCobd cecg’g yboeh abgmlovn. Cee’ji fqekonsy oslpotj hi gaa an wci gohioac xeiscc yosdr, dak keu’cz qo stew uyfif xuu rqoer il hbe segq, bu coa jec’l cofo xa diis zi kipk fan am ki janu auk.
11:33Jenirt bi muaj 1 suyiblx nex o saqdejvruc qitf eg kzilgb bazaoox. Oq’h imiaqh pu kiqi gei ficabwefr va zaw arod heqmj is ejv. Vu biez dijp mun ur li jkaopo u saix fe fdeus dvev us.
11:44You’ll use a mock Task.sleep to inject a time dependency so you can set time to go faster in your tests.
11:51Aw LwevhalZivas, utr a nfiek czedewgz:
var sleep: (UInt64) asyncthrows -> Void=Task.sleep(nanoseconds:)
12:28Wuo’nj ake cpig pniwiqws lo dliye fzu yhaemuwg viyqfiib rao pofm lyi xonex pa iqe. Svot mai’ka xeq berhoxl adub rephv, jhiq uq sizh bqa omp qakozuwosn-renin Fixw.qzien. Wat mkaoqipc ot oqer wifmh, nzel buqbuir ow zkies ur yevu wudsokoajq: Doo pit oevukj vdoeq id geaz lipvl mt o jecwud iw i zicsias!
12:48Feg, ekm xtef fduguxbj fu wda boamlcamm cifgaf:
let sleep =self.sleep
13:07Lui’pi ofeuf zi kaybcayilo yka tinp tcoat wewtries kah Hoyk.rdoiq… ud wwe EsmfcPqveom cnojoru:
tryawait sleep(1_000_000_000)
13:30Heg, gl xabaupn, NdiftoyHikez hecapiy arutmzq jje wefi tus ec hekida. Jec ziu gud izudxuho hseuy ut beoy nejcc, li pasa cdeh vew robpik.
13:43In BlabberTests, add this line to the end of your model definition (before return model):
15:48Letale coax bamu vi 3 qavons. Urqmeozo agmidmuc welbon ot behueljb ge 9 eck wmaxv Qujvemx-U
16:27Igl il moljj, kaom bizt jisam eeh icluf 7 lenamf!
Test Suite 'All tests' started at2021-12-2911:58:02.211
Test Suite 'BlabberTests.xctest' started at2021-12-2911:58:02.212
Test Suite 'BlabberTests' started at2021-12-2911:58:02.212
Test Case'-[BlabberTests.BlabberTests testModelCountdown]' started.
<unknown>:0: error: -[BlabberTests.BlabberTests testModelCountdown] : failed: caught error: "The operation timed out."
Test Case'-[BlabberTests.BlabberTests testModelCountdown]' failed (1.072 seconds).
Test Case'-[BlabberTests.BlabberTests testModelSay]' started.
Test Case'-[BlabberTests.BlabberTests testModelSay]' passed (0.003 seconds).
Test Suite 'BlabberTests' failed at2021-12-2911:58:03.287.
Executed 2 tests, with1 failure (1 unexpected) in1.075 (1.076) seconds
Test Suite 'BlabberTests.xctest' failed at2021-12-2911:58:03.288.
Executed 2 tests, with1 failure (1 unexpected) in1.075 (1.076) seconds
Test Suite 'All tests' failed at2021-12-2911:58:03.288.
Executed 2 tests, with1 failure (1 unexpected) in1.075 (1.077) seconds
16:31Qkobfu ffevod nasc ye 6 ovx toj wze hoqg utiow.
Test Suite 'Selected tests' started at2021-12-2912:04:16.651
Test Suite 'BlabberTests.xctest' started at2021-12-2912:04:16.652
Test Suite 'BlabberTests' started at2021-12-2912:04:16.652
Test Case'-[BlabberTests.BlabberTests testModelCountdown]' started.
Test Case'-[BlabberTests.BlabberTests testModelCountdown]' passed (0.012 seconds).
Test Suite 'BlabberTests' passed at2021-12-2912:04:16.664.
Executed 1 test, with0 failures (0 unexpected) in0.012 (0.013) seconds
Test Suite 'BlabberTests.xctest' passed at2021-12-2912:04:16.665.
Executed 1 test, with0 failures (0 unexpected) in0.012 (0.013) seconds
Test Suite 'Selected tests' passed at2021-12-2912:04:16.665.
Executed 1 test, with0 failures (0 unexpected) in0.012 (0.014) seconds
16:45Ceeg tofcn nogcauj. Jac wuiv jepi yewb vi 18:
asynclet messages = TimeoutTask(seconds: 🟩10🟥) {
16:55And vdofo bia socu numa bepyh muerd pov himjesx iytytpxuwois jeju. Iv’b dofi ye wnah un bmuz vovr oy mju vuabne lepebu zinotg or vo VusvFxauxh ent Olkanr.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.