1"use strict";(globalThis.webpackChunkkotestdocs=globalThis.webpackChunkkotestdocs||[]).push([[6260],{17338(e,t,s){s.r(t),s.d(t,{assets:()=>a,contentTitle:()=>c,default:()=>u,frontMatter:()=>r,metadata:()=>n,toc:()=>l});const n=JSON.parse('{"id":"framework/timeouts/blocking_tests","title":"Blocking Tests","description":"When specifying timeouts in tests, Kotest uses the withTimeout coroutine functions that the Kotlin coroutine","source":"@site/versioned_docs/version-6.0/framework/timeouts/blocking_tests.md","sourceDirName":"framework/timeouts","slug":"/framework/timeouts/blocking-tests.html","permalink":"/docs/6.0/framework/timeouts/blocking-tests.html","draft":false,"unlisted":false,"editUrl":"https://github.com/kotest/kotest/blob/master/documentation/versioned_docs/version-6.0/framework/timeouts/blocking_tests.md","tags":[],"version":"6.0","frontMatter":{"id":"blocking_tests","title":"Blocking Tests","slug":"blocking-tests.html","sidebar_label":"Blocking Tests"},"sidebar":"framework","previous":{"title":"Project Timeout","permalink":"/docs/6.0/framework/timeouts/project-timeouts.html"},"next":{"title":"Fail Fast","permalink":"/docs/6.0/framework/fail-fast.html"}}');var o=s(74848),i=s(28453);const r={id:"blocking_tests",title:"Blocking Tests",slug:"blocking-tests.html",sidebar_label:"Blocking Tests"},c=void 0,a={},l=[];function d(e){const t={admonition:"admonition",code:"code",p:"p",pre:"pre",...(0,i.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(t.p,{children:["When specifying timeouts in tests, Kotest uses the ",(0,o.jsx)(t.code,{children:"withTimeout"})," coroutine functions that the Kotlin coroutine\nlibrary provides. These timeouts are co-operative in nature, and a timeout is detected when a coroutine suspends, resumes, or calls ",(0,o.jsx)(t.code,{children:"yield"}),"."]}),"\n",(0,o.jsxs)(t.p,{children:["However when executing blocking code, the thread will be blocked and so the coperative approach will not work.\nIn this scenario we must revert to interrupting the thread using ",(0,o.jsx)(t.code,{children:"Thread.interrupt"})," or something similar. In order\nfor this interruption to work safely, we must execute the test on a dedicated thread."]}),"\n",(0,o.jsxs)(t.p,{children:["Therefore, it is up to the user to signify to Kotest that they want a particular test to execute on a dedicated\nthread that can be safely used for interruption. We do this by enabling the ",(0,o.jsx)(t.code,{children:"blockingTest"})," flag in test config."]}),"\n",(0,o.jsx)(t.p,{children:"For example:"}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-kotlin",children:'class MyBlockingTest : FunSpec() {\n init {\n\n test("interrupt me!").config(blockingTest = true, timeout = 10.seconds) {\n Thread.sleep(100000000)\n }\n\n test("uses suspension").config(timeout = 10.seconds) {\n delay(100000000)\n }\n }\n}\n'})}),"\n",(0,o.jsxs)(t.p,{children:["In the above example, the first test requires the ",(0,o.jsx)(t.code,{children:"blockingTest"})," flag because it uses a thread blocking operation.\nThe second test does not because it uses a suspendable operation."]}),"\n",(0,o.jsx)(t.admonition,{type:"note",children:(0,o.jsx)(t.p,{children:"This feature is only available on the JVM."})})]})}function u(e={}){const{wrapper:t}={...(0,i.R)(),...e.components};return t?(0,o.jsx)(t,{...e,children:(0,o.jsx)(d,{...e})}):d(e)}},28453(e,t,s){s.d(t,{R:()=>r,x:()=>c});var n=s(96540);const o={},i=n.createContext(o);function r(e){const t=n.useContext(i);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function c(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:r(e.components),n.createElement(i.Provider,{value:t},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.